PHP邮件脚本实际上并不发送邮件

时间:2012-10-18 01:19:51

标签: php forms email

我在使用此电子邮件表单时遇到问题。没有php错误代码或任何东西,电子邮件永远不会被传递...这整个代码示例是我网站中几个页面上的包含文件。表单提交到它所在的任何页面,并且工作,除了我不知道它为什么不发送电子邮件的事实。它似乎不会处理包含发送电子邮件的代码的else语句。

<div class="green_box" id="contact_us">
<h3>CONTACT US</h3>
<div class="white_box">

<?php
if ($_POST['submitted']==1) {  
    $errormsg = ""; //Initialize errors  
    if ($_POST[your_name]){  
        $your_name = $_POST[your_name]; 
    }  
    else {  
        $errormsg = "You did not enter your Name";  
    }  
    if ($_POST[your_email]){  
        $your_email = $_POST[your_email];
    }  
    else {  
        if ($errormsg){ //If there is already an error, add next error  
            $errormsg = $errormsg . " or your Email";  
        }else{  
            $errormsg = "You did not enter your Email";  
        }  
    }
    if ($_POST[your_message]){  
        $your_message = $_POST[your_message];
    }  
    else {  
        if ($errormsg){ //If there is already an error, add next error  
            $errormsg = $errormsg . " or your Message";  
        }else{  
            $errormsg = "You did not enter your Message"; 
        }  
    }
    if (strlen($errormsg) > 1) {
        echo "<p><strong>" . $errormsg . ".</strong><br>Please try again.</p>";
    }
    else {
        $email_to = "willyfresh@gmail.com"; // recipient inbox
        $email_subject = "Fore A Partners Website Contact Form";
        $email_message = "Form details below.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($your_name)."\n";
        $email_message .= "Email: ".clean_string($your_email)."\n";
        $email_message .= "Comments: ".clean_string($your_message)."\n";

        $headers = 'From: '.$your_email."\r\n".
        'Reply-To: '.$your_email."\r\n" .
        'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers);  
        echo "<p>Thank you for contacting us. We will be in touch with you very soon.</p>";
    }
}
?>

<form name="contactform" method="post">
<p>Name<br><input type="text" name="your_name" maxlength="80" size="45"></p>
<p>Email<br><input type="text" name="your_email" maxlength="80"  size="45"></p>
<p>Message<br><textarea name="your_message" maxlength="1000" rows="6" cols="30"></textarea></p>
<input type="hidden" name="submitted" value="1">
<p><input type="image" src="../btn_submit.png" alt="Submit" name="submit"></p>
</form>
</div>
</div>

1 个答案:

答案 0 :(得分:1)

<强>更新

有几个原因导致它可能无法正常工作。你在运行什么类型的服务器?您可能需要在php.ini中配置SMTP属性,但如果不了解更多信息,很难说。

我个人更喜欢PEAR邮件解决方案,它更强大,可以让我轻松配置SMTP信息。我清理了你的代码并实现了一个适合我的样本PEAR邮件脚本。很可能你已经安装了PEAR邮件,但是,如果你需要下载并安装它,请访问:http://pear.php.net/package/Mail/download

<?php
if ($_POST) {
    if ($_POST['submitted']==1) {  
        $errormsg = ""; //Initialize errors  
        if ($_POST['your_name']){  
            $your_name = $_POST['your_name']; 
        }  
        else {  
            $errormsg = "You did not enter your Name";  
        }  
        if ($_POST['your_email']){  
            $your_email = $_POST['your_email'];
        }  
        else {  
            if ($errormsg){ //If there is already an error, add next error  
                $errormsg = $errormsg . " or your Email";  
            }else{  
                $errormsg = "You did not enter your Email";  
            }  
        }
        if ($_POST['your_message']){  
            $your_message = $_POST['your_message'];
        }  
        else {  
            if ($errormsg){ //If there is already an error, add next error  
                $errormsg = $errormsg . " or your Message";  
            }else{  
                $errormsg = "You did not enter your Message"; 
            }  
        }
        if (strlen($errormsg) > 1) {
            echo "<p><strong>" . $errormsg . ".</strong><br>Please try again.</p>";
        }
        else {
             // recipient inbox
            $email_subject = "Fore A Partners Website Contact Form";
            $email_message = "Form details below.\n\n";

            function clean_string($string) {
              $bad = array("content-type","bcc:","to:","cc:","href");
              return str_replace($bad,"",$string);
            }

            $email_message .= "Name: ".clean_string($your_name)."\n";
            $email_message .= "Email: ".clean_string($your_email)."\n";
            $email_message .= "Comments: ".clean_string($your_message)."\n";            

            // INCLUDE PEAR MAIL
            require_once "Mail.php";
            require_once('Mail\mime.php');

            // CONFIGURE SMTP SETTINGS
            $email_to = "willyfresh@gmail.com";
            $sender = "emailfrom@example.com";
            $host = "mail.example.com";
            $username = "emailfrom@example.com";
            $password = "password";

            $crlf = "\n";       
            $headers = array(
                'From' => $sender,
                'To' => $email_to,
                'Reply-To' => $your_email,
                'Subject' => $email_subject
            );  
            // Creating the Mime message
            $mime = new Mail_mime($crlf);   
            // Setting the body of the email
            $mime->setTXTBody($your_message);
            $mime->setHTMLBody($your_message);  
            $body = $mime->get();
            $headers = $mime->headers($headers);    
            // Sending the email
            $mail =& Mail::factory('smtp',
            array ('host' => $host,
                'auth' => true,
                'username' => $username,
                'password' => $password
            ));
            $mail->send($email_to,$headers,$body);

            echo "<p>Thank you for contacting us. We will be in touch with you very soon.</p>";
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div class="green_box" id="contact_us">
<h3>CONTACT US</h3>
<div class="white_box">
<form name="contactform" method="post">
<p>Name<br><input type="text" name="your_name" maxlength="80" size="45"></p>
<p>Email<br><input type="text" name="your_email" maxlength="80"  size="45"></p>
<p>Message<br><textarea name="your_message" maxlength="1000" rows="6" cols="30"></textarea></p>
<input type="hidden" name="submitted" value="1">
<p><input type="image" src="../btn_submit.png" alt="Submit" name="submit"></p>
</form>
</div>
</div>
</body>
</html>

我看到的第一件事就是你需要像$ _POST ['your_name']那样抓住你的帖子而不是$ _POST [你的名字]