【问题标题】:Why wont messages go through my contact form to my email? [duplicate]为什么消息不会通过我的联系表发送到我的电子邮件? [复制]
【发布时间】:2014-09-21 13:24:06
【问题描述】:

我正在尝试在我的页面上设置联系表。没有什么疯狂的,只是一个简单的形式。我已经尝试了来自网络的多个联系表格,它们看起来都很好,在我的网站上填写它们时它们都工作正常,即使你点击提交它也会让你进入 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\">";
}
?>

【问题讨论】:

  • 您在实时服务器上吗?如果你在本地服务器上,PHP 邮件功能需要大量配置。

标签: php email send forms contact


【解决方案1】:

这是帮助您发送电子邮件的代码,它可以确保大约 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 );

【讨论】:

    【解决方案2】:

    您的代码可能没有问题,您可能需要确认运行 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
    

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

    根据我的经验,雅虎在发送到可能的电子邮件时可能会有点脾气暴躁,并且如果在短时间内有很多电子邮件,则可能会开始阻止电子邮件,因此请尝试其他电子邮件地址

    【讨论】:

      猜你喜欢
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2014-03-29
      • 2019-04-24
      • 1970-01-01
      相关资源
      最近更新 更多