为什么消息不会通过我的联系表单到我的电子邮件?

时间:2014-07-30 02:13:07

标签: php email send html-form contact

我正在尝试在我的网页上添加联系表单。没什么了不起的,只是一个简单的形式。我已经尝试过来自网络的多个联系表单,它们看起来都很好,它们在我的网站上填写时工作正常,甚至当你点击提交时它会带你到PHP页面很好,但是我从来没有得到过在我的电子邮件中留言我检查了我的垃圾邮件和一切,我只是没有得到消息。

这是表单html:

<form method="post" action="sendit.php">
    <label for="Name">Name:</label>
    <input type="text" name="Name" id="Name" />

    <label for="Phone">Phone:</label>
    <input type="text" name="Phone:" id="Phone" />

    <label for="Email">Email:</label>
    <input type="text" name="Email" id="Email" />

    <label for="Message">Message:</label>
    <textarea name="Message" rows="20" cols="20" id="Message"></textarea>

    <input type="submit" name="submit" value="Submit" class="submit-button" />
</form>

这是PHP:

<?php
$EmailFrom = "mjd8079@yahoo.com";
$EmailTo = "mjd8079@yahoo.com";
$Subject = "Contact";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=sendit.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

2 个答案:

答案 0 :(得分:0)

这段代码可以帮助您发送电子邮件,并确保邮件在收件箱中提供的约80%,而不是垃圾邮件< /强>

require_once "Mail.php";
require_once('Mail/mime.php');

$to = "mjd8079@yahoo.com";
$subject = "Contact";
$header = "from: Some Name <mjd8079@yahoo.com> \r"."<br >";
$header .= "Content-Type: text/html; charset=ISO-8859-1 \r"."<br >";
$header .= "Return-Path: <mjd8079@yahoo.com> \r"."<br >";
$header .= "X-Priority: 1 (Highest) \r"."<br >";
$header .= "X-MSMail-Priority: High \r"."<br >";
$header .= "Importance: High \r"."<br >"; 
$header .= "MIME-Version: 1.0 \r"."<br >";

$htmlbody = "<html>
    <table width='430' border='1' cellpadding='5' cellspacing='5' class='bg1' style='border-collapse:collapse;'>
      <tr>
        <td width='185' bgcolor='#003366'><font color='#ffffff' face='Verdana'>Your Name:</font></td>
        <td width='210' valign='middle' bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$name</font></td>
      </tr>
      <tr>
        <td bgcolor='#003366'><font color='#ffffff' face='Verdana'>E mail</font></td>
        <td bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$Email</font></td>
      </tr>
      <tr>
        <td bgcolor='#003366'><font color='#ffffff' face='Verdana'>Telephone Number</font></td>
        <td bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$Tel</font></td>
      </tr>
      <tr>
        <td bgcolor='#003366'><font color='#ffffff' face='Verdana'>Message</font></td>
        <td bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$Message</font></td>
      </tr>
    </table>
</html>";

$from = "Some Name <mjd8079@yahoo.com>";
$host = "smtp.yahoo.com";
$port = "587";
$username1 = "mjd8079@yahoo.com";
$password1 = "yourpassword";
$crlf = chr(10);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($htmlbody);
$body = $mime->getMessageBody();
$headers = array ('From' => $from,
                    'To' => $to,
                    'Subject' => $subject);
$headers = $mime->headers($headers);

$smtp = Mail::factory('smtp', 
        array ('host' => $host, 
                'port' => $port, 
                'auth' => true, 
                'username' => $username1, 
                'password' => $password1)
            );
$mail = $smtp->send($to, $headers, $body );

答案 1 :(得分:0)

您的代码可能没有问题,您可能需要确认运行PHP的服务器上有一个smtp服务器设置并且PHP知道它。

您必须在php.ini文件中检查以查看已在服务器中设置,可以看到设置听到http://php.net/manual/en/mail.configuration.php

如果您还可以访问服务器上的命令行,则可以通过执行命令测试服务器是否有权发送电子邮件

/usr/sbin/sendmail -t -i mjd8079@yahoo.com
This is a test
ctrl-d

如果你没有收到消息,那么它最像是服务器而不是代码

根据我的经验,雅虎在发送电子邮件时可能有点脾气暴躁,并且如果在很短的时间内有很多电子邮件,可以开始阻止电子邮件,所以请尝试另一个电子邮件地址