将表单的值发送到电子邮件

时间:2013-03-09 17:00:40

标签: php html

您好我的表单发送到电子邮件时遇到了问题。我已经创建了一个表单来向我的emal发送值,当按下发送按钮它告诉我邮件已发送但我无法看到我的yahoo或gmail电子邮件,我什么都没有收到...

这是我的PHP代码表单:

<?php 
$ToEmail = 'mr_sergios@yahoo.com'; 
$EmailSubject = 'Site contact form'; 
 $mailheader = "From: ".$_POST["email"]."\r\n"; 
 $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
 $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]).""; 
  mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>

<?php 
if ($_POST["email"]<>'') { 
$ToEmail = 'mr_sergios@yahoo.com'; 
$EmailSubject = 'Site contact form'; 
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]).""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?> 
   Your message was sent
      <?php 
   } else { 
  ?> 
    <form action="test.php" method="post">
   <table width="400" border="0" cellspacing="2" cellpadding="0">
  <tr>
     <td width="29%" class="bodytext">Your name:</td>
  <td width="71%"><input name="name" type="text" id="name" size="32"></td>
    </tr>
    <tr>
     <td class="bodytext">Email address:</td>
    <td><input name="email" type="text" id="email" size="32"></td>
     </tr>
      <tr>
   <td class="bodytext">Comment:</td>
        <td><textarea name="comment" cols="45" rows="6" id="comment"  
  class="bodytext">      
   </textarea></td>
  </tr>
  <tr>
   <td class="bodytext"> </td>
    <td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
    </tr>
       </table>
   </form> 
 <?php 
  }; 
  ?>

4 个答案:

答案 0 :(得分:0)

您可能没有在php.ini中设置正确的SMTP设置。我推荐使用mail()之外的其他东西,因为它更可能是可靠的。尝试使用类似this的内容。

要么就是这样,要么只是落在垃圾邮件文件夹中。

答案 1 :(得分:0)

你是从Linux发送的吗?如果是,请检查/var/log/mail.err并查看SMTP是否会引发任何错误。

对于Windows,可能需要安装SMTP服务器,例如Mercury SMTP

答案 2 :(得分:0)

您是否考虑过使用PHP Mailer Class?

以下示例是在同一页面上提交给自己的表单,只需将下方粘贴到联系人页面的最顶部即可。下载并加入您的课程。

require_once('class.phpmailer.php');
$address = "you@youremail.com";
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$comment = htmlspecialchars($_POST['comment']);

$mail  = new PHPMailer(); // defaults to using php "mail()"
$body = $comment;
$mail->AddReplyTo($email,$name);
$mail->SetFrom($email,$name);
$mail->AddReplyTo($email,$name);
$mail->AddAddress($address, "Your Name");
$mail->Subject = $subject;
$mail->MsgHTML($body);
if(isset($_POST['submit']))
{
$mail->Send();
}

答案 3 :(得分:0)

考虑使用此库:http://code.google.com/a/apache-extras.org/p/phpmailer/

设置和使用非常简单,并在出现错误时为您提供详细信息。