Php 验证表单不工作

Php 验证表单不工作,php,Php,我的验证联系人表单不起作用。当我尝试提交表单时,出现以下错误: 警告:mail()[function.mail]:无法连接到位于的邮件服务器 “localhost”端口25,请验证中的“SMTP”和“SMTP_端口”设置 php.ini或在中使用ini_set() 第53行的E:\CMS\u Site\wamp\www\sitename\contents\send\u mail.php WAMP没有SMTP服务器 解释 为了能够发送电子邮件,您需要一个传出电子邮件服务器。在大多数Linux系统中

我的验证联系人表单不起作用。当我尝试提交表单时,出现以下错误:

警告:mail()[function.mail]:无法连接到位于的邮件服务器 “localhost”端口25,请验证中的“SMTP”和“SMTP_端口”设置 php.ini或在中使用ini_set() 第53行的E:\CMS\u Site\wamp\www\sitename\contents\send\u mail.php


WAMP没有SMTP服务器

解释

为了能够发送电子邮件,您需要一个传出电子邮件服务器。在大多数Linux系统中,默认情况下都有一个,PHP将使用sendmail,这是一个Linux应用程序,用于向您安装的邮件传输代理提交邮件

在Windows中,为了能够从PHP发送邮件,您需要在某处安装一个传出邮件服务器,并且需要告诉PHP它的地址和端口。这是在php.ini中使用SMTP和SMTP_端口设置完成的。端口25上的默认值为“localhost”。除非您在该计算机上设置了邮件服务器,否则将失败

例如,如果您的ISP给您发送邮件服务器,您可以使用其地址和端口号。或者,如果您真的想发送邮件,您可以在本地计算机上或本地网络中的某个人上设置自己的邮件服务器


SOURCE

通过php发送邮件的代码完全没有问题。 如果您在windows中使用WAMP,他们将不会有任何SMTP服务器设置。因此,如果您需要从WAMP发送邮件,您需要使用任何SMTP服务器支持。您可以使用Mandrill Mailchimb从localhost发送电子邮件,只需在其中创建一个a/c,下载swift文件并将其添加到您的工作目录中以设置邮件服务器。要阅读有关如何通过Mandrill发送邮件的更多信息,请参阅下面的代码

您可以在支持SMTP设置的实时服务器中使用此代码。
在代码中通过Mandrill Swift文件发送邮件。您可以更改邮件主题等

send_mail.php

    <?php
    if(isset($_POST['name']) != NULL && isset($_POST['email']) != NULL  && isset($_POST['message']) != NULL ){ 
        $name = strtoupper (trim($_POST['name']));
        $address = trim($_POST['address']);
        $email = strtolower(trim($_POST['email']));
        $contact = trim($_POST['contact']);
        $country = trim($_POST['country']);
        $website = trim($_POST['website']);
        $subject = trim($_POST['subject']);
        $message = trim($_POST['message']);
        //---------------------------------------------
        error_reporting(E_ALL ^ E_NOTICE);
        $my_email = "kiranpahadi@gmail.com";
        $errors = array();
        // Remove $_COOKIE elements from $_REQUEST.
        if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}
        // Validate email field.
        if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])){
            $_REQUEST['email'] = trim($_REQUEST['email']);
            if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){
                $errors[] = "Email address is invalid";
            }
            else{
                $exploded_email = explode("@",$_REQUEST['email']);
                if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){
                    $errors[] = "Email address is invalid";
                }
                else{
                    if(substr_count($exploded_email[1],".") == 0){
                        $errors[] = "Email address is invalid";
                    }
                    else{
                        $exploded_domain = explode(".",$exploded_email[1]);
                        if(in_array("",$exploded_domain)){
                            $errors[] = "Email address is invalid";
                        }
                        else{
                            foreach($exploded_domain as $value){
                                if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){
                                    $errors[] = "Email address is invalid"; 
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        // Check referrer is from same site.
        if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){
            $errors[] = "You must enable referrer logging to use the form";
        }
        // Check for a blank form.
        function recursive_array_check_blank($element_value){
            global $set;
            if(!is_array($element_value)){
                if(!empty($element_value)){
                    $set = 1;
                }
            }
            else{
                foreach($element_value as $value){
                    if($set){
                        break;
                    } 
                    recursive_array_check_blank($value);
                }
            }
        }
        recursive_array_check_blank($_REQUEST);
        if(!$set){
            $errors[] = "You cannot send a blank form";
        }
        unset($set);
        // Display any errors and exit if errors exist.
        if(count($errors)){
            foreach($errors as $value){
                print "$value<br>";
            }
            exit;
        }
        if(!defined("PHP_EOL")){
            define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");
        }
        // Build message.
        function build_message($request_input){
            if(!isset($message_output)){
                $message_output ="";
            }
            if(!is_array($request_input)){
                $message_output = $request_input;
            }
            else{
                foreach($request_input as $key => $value){
                    if(!empty($value)){
                        if(!is_numeric($key)){
                            $message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
                        }
                        else{
                            $message_output .= build_message($value).", ";
                        }
                    }
                }
            }
            return rtrim($message_output,", ");
        }
        $message = build_message($_REQUEST);
        $message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."The Message has been submitted successfully ";
        $message = stripslashes($message);
        $subject = stripslashes($subject);
        if($email){
            $headers =  "From: {$name} <{$_REQUEST['email']}>";
            $headers .= PHP_EOL;
            $headers .= "Reply-To: " . $_REQUEST['email'];
        }
        else{
            if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){
                $from_name = stripslashes($_REQUEST['name']);
            }
            $headers = "From: {$name} <{$_REQUEST['email']}>";
        }
        include_once "swift/lib/swift_required.php";
        $from = array("frommail@mail.com" => "Your Name");
        $to="kiranpahadi@gmail.com";
        $message = "Hello  -This is a test mail";
        $subject = "Subject – Test";
        $transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
        $transport->setUsername('kiranpahadi@gmail.com');
        $transport->setPassword('eWTy3pUA1Okb-4lwUtk4dg');
        $swift = Swift_Mailer::newInstance($transport);
        sleep(2);
        $html = "" . $message . "";
        $maildetails = new Swift_Message($subject);
        $maildetails->setFrom($from);
        $maildetails->setBody($html, 'text/html');
        $maildetails->setTo($to);
        $maildetails->addPart($message, 'text/plain');

        if ($recipients = $swift->send($maildetails, $failures)) {
            echo 'Message successfully sent!';
        } else {
            echo "There was an error:n";
            //print_r($failures);
        }
        //mail($my_email,$subject,$message,$headers);
    ?>
    <b>Thank you <?php if(isset($_REQUEST['name'])){print stripslashes($_REQUEST['name']);} ?></b>
    <?php
    }
    else{
        ?>
        <form name = "mail_form" method="post" action ="send_mail.php"enctype="multipart/form-data">
            <div><label for="name"> Full Name: </label> <input name="name" type="text" size="50"  /> </div> 
            <div><label for="address"> Address: </label> <input name="address" type="text" size="50"   /></div>
            <div><label for="email"> Email:  </label><input name="email" type="email" size="50"   /></div>
            <div><label for="contact">Contact:</label>  <input name="contact" type="text"  size="50"  /></div>
            <div><label for="country">Country:</label> <input name="country" type="text"   size="50" /> </div>
            <div><label for="website">Website:</label> <input name="website" type="text"   size="50" /> </div>
            <div><label for="subject">Subject:</label> <input name="subject" type="text"   size="50" /> </div>
            <div><label for="message">Your Message:</label> 
            <textarea name="message" cols="40" rows="8"></textarea>
            </div>
            <div><p><input type="submit" name="btn_submit"  id="submit-go" value="Send Mail"/></p></div>
        </form>
    <?php
    }
    ?>

错误本身是不言自明的。您需要在php.ini文件中配置SMTP邮件服务。不,她只需要下载并包含
SwiftMailer
@Jklyn:访问以了解和下载有关mandrill的更多信息。它也可以避免常见的垃圾邮件。@Jklyn在这里查看文档和下载::你需要在他们的网站上创建一个a/c,然后只有你才能获得用户名和Mandrill密钥,你需要将其包含在你的邮件发送代码中。你解决了你的问题吗?没有。它将错误显示为:致命错误:在E:\CMS\u Site\wamp\www\sample\u Site\contents\mandrill api php\src\mandrill.php的第65行调用未定义的函数curl\u init()。我认为这应该没有任何问题。您应该等待并检查邮箱中的邮件。您是否在
$To=”行中设置了邮件地址toemail@mail.com";在上面的代码中。
    <?php
    if(isset($_POST['name']) != NULL && isset($_POST['email']) != NULL  && isset($_POST['message']) != NULL ){ 
        $name = strtoupper (trim($_POST['name']));
        $address = trim($_POST['address']);
        $email = strtolower(trim($_POST['email']));
        $contact = trim($_POST['contact']);
        $country = trim($_POST['country']);
        $website = trim($_POST['website']);
        $subject = trim($_POST['subject']);
        $message = trim($_POST['message']);
        //---------------------------------------------
        error_reporting(E_ALL ^ E_NOTICE);
        $my_email = "kiranpahadi@gmail.com";
        $errors = array();
        // Remove $_COOKIE elements from $_REQUEST.
        if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}
        // Validate email field.
        if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])){
            $_REQUEST['email'] = trim($_REQUEST['email']);
            if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){
                $errors[] = "Email address is invalid";
            }
            else{
                $exploded_email = explode("@",$_REQUEST['email']);
                if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){
                    $errors[] = "Email address is invalid";
                }
                else{
                    if(substr_count($exploded_email[1],".") == 0){
                        $errors[] = "Email address is invalid";
                    }
                    else{
                        $exploded_domain = explode(".",$exploded_email[1]);
                        if(in_array("",$exploded_domain)){
                            $errors[] = "Email address is invalid";
                        }
                        else{
                            foreach($exploded_domain as $value){
                                if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){
                                    $errors[] = "Email address is invalid"; 
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        // Check referrer is from same site.
        if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){
            $errors[] = "You must enable referrer logging to use the form";
        }
        // Check for a blank form.
        function recursive_array_check_blank($element_value){
            global $set;
            if(!is_array($element_value)){
                if(!empty($element_value)){
                    $set = 1;
                }
            }
            else{
                foreach($element_value as $value){
                    if($set){
                        break;
                    } 
                    recursive_array_check_blank($value);
                }
            }
        }
        recursive_array_check_blank($_REQUEST);
        if(!$set){
            $errors[] = "You cannot send a blank form";
        }
        unset($set);
        // Display any errors and exit if errors exist.
        if(count($errors)){
            foreach($errors as $value){
                print "$value<br>";
            }
            exit;
        }
        if(!defined("PHP_EOL")){
            define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");
        }
        // Build message.
        function build_message($request_input){
            if(!isset($message_output)){
                $message_output ="";
            }
            if(!is_array($request_input)){
                $message_output = $request_input;
            }
            else{
                foreach($request_input as $key => $value){
                    if(!empty($value)){
                        if(!is_numeric($key)){
                            $message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
                        }
                        else{
                            $message_output .= build_message($value).", ";
                        }
                    }
                }
            }
            return rtrim($message_output,", ");
        }
        $message = build_message($_REQUEST);
        $message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."The Message has been submitted successfully ";
        $message = stripslashes($message);
        $subject = stripslashes($subject);
        if($email){
            $headers =  "From: {$name} <{$_REQUEST['email']}>";
            $headers .= PHP_EOL;
            $headers .= "Reply-To: " . $_REQUEST['email'];
        }
        else{
            if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){
                $from_name = stripslashes($_REQUEST['name']);
            }
            $headers = "From: {$name} <{$_REQUEST['email']}>";
        }
        include_once "swift/lib/swift_required.php";
        $from = array("frommail@mail.com" => "Your Name");
        $to="kiranpahadi@gmail.com";
        $message = "Hello  -This is a test mail";
        $subject = "Subject – Test";
        $transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
        $transport->setUsername('kiranpahadi@gmail.com');
        $transport->setPassword('eWTy3pUA1Okb-4lwUtk4dg');
        $swift = Swift_Mailer::newInstance($transport);
        sleep(2);
        $html = "" . $message . "";
        $maildetails = new Swift_Message($subject);
        $maildetails->setFrom($from);
        $maildetails->setBody($html, 'text/html');
        $maildetails->setTo($to);
        $maildetails->addPart($message, 'text/plain');

        if ($recipients = $swift->send($maildetails, $failures)) {
            echo 'Message successfully sent!';
        } else {
            echo "There was an error:n";
            //print_r($failures);
        }
        //mail($my_email,$subject,$message,$headers);
    ?>
    <b>Thank you <?php if(isset($_REQUEST['name'])){print stripslashes($_REQUEST['name']);} ?></b>
    <?php
    }
    else{
        ?>
        <form name = "mail_form" method="post" action ="send_mail.php"enctype="multipart/form-data">
            <div><label for="name"> Full Name: </label> <input name="name" type="text" size="50"  /> </div> 
            <div><label for="address"> Address: </label> <input name="address" type="text" size="50"   /></div>
            <div><label for="email"> Email:  </label><input name="email" type="email" size="50"   /></div>
            <div><label for="contact">Contact:</label>  <input name="contact" type="text"  size="50"  /></div>
            <div><label for="country">Country:</label> <input name="country" type="text"   size="50" /> </div>
            <div><label for="website">Website:</label> <input name="website" type="text"   size="50" /> </div>
            <div><label for="subject">Subject:</label> <input name="subject" type="text"   size="50" /> </div>
            <div><label for="message">Your Message:</label> 
            <textarea name="message" cols="40" rows="8"></textarea>
            </div>
            <div><p><input type="submit" name="btn_submit"  id="submit-go" value="Send Mail"/></p></div>
        </form>
    <?php
    }
    ?>