【问题标题】:PHP mail header problemsPHP邮件头问题
【发布时间】:2014-07-19 17:30:12
【问题描述】:

我目前有两个问题:

  1. 在我的 gmail 中,发件人:标头不尊重“我的联系表单”名称。它默认为其他内容。

  2. 当我在 gmail 中收到电子邮件时,它当然是从myemail@mydomain.com 发送的(以避免垃圾邮件文件夹),但是当我点击回复时,我希望它使用联系表单用户的 $email,不是myemail@mydomain.com

我确定我遗漏了一些细节,所以如果我遗漏了什么,请告诉我,我会添加它。

这是我的代码:

if (!isset($_REQUEST['name']) || !isset($_REQUEST['email']) || !isset($_REQUEST['message'])) {
  die();
}

// PHP parameters
$to = "myemail@gmail.com";
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

// Subject
$email_subject = $_POST['name'] . ' ' . 'is contacting you from mywebsite.com' . '!';


// body of email
$body = ""

$body = wordwrap($body, 60, "\n");

// Headers
$headers .= "From: My contact form <myemail@mydomain.com>" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Return-Path: myemail@mydomain.com" . "\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n"; 

// Mail it
mail( $to, $email_subject, $body, $headers, "-fmyemail@mydomain.com" );

【问题讨论】:

    标签: php email contact-form email-headers


    【解决方案1】:

    对于第 1 部分,= 设置变量,然后 .= 添加到变量。因此,您正在覆盖先前声明的变量。它应该是:

    $headers = "From: My contact form <myemail@mydomain.com>" . "\r\n";
    $headers .= "Reply-To: $email" . "\r\n";
    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers .= "Return-Path: myemail@mydomain.com" . "\r\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n"; 
    

    对于第 2 部分,试试这个: reply-to address in php contact form

    换句话说,改变

    $headers .= "Reply-To: $email" . "\r\n";
    

    $headers .= "Reply-To: " . $email . "\r\n";
    

    【讨论】:

    • 天才!这解决了我的 2 个问题......但是......不幸的是,我重新引入了以前的问题,我在标题中遇到了一个软失败,说它不是允许的发件人......有什么想法吗?
    • 我对那个错误不太熟悉,但看起来这个问题可能是相关的并且有一些很好的回答:stackoverflow.com/questions/6201396/…希望有帮助!
    • 我想我会发帖说我终于通过了我的 SPF。此链接为我指明了正确的方向:(x-pose.org/2013/10/…)
    【解决方案2】:

    其中有一行缺少连接符号 (.),这导致 $headers 丢失值 FromReply-To,因为它们设置在重新初始化 $header 的行之前。

    在您的代码中,重新初始化 $headers 的行是:

    $headers = "MIME-Version: 1.0" . "\r\n";
    

    应该改为:

    $headers  = "From: My contact form <myemail@mydomain.com>" . "\r\n";
    $headers .= "Reply-To: $email" . "\r\n";
    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers .= "Return-Path: myemail@mydomain.com" . "\r\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n"; 
    

    【讨论】:

    • 感谢您的回复!
    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2021-07-19
    • 2015-11-12
    • 2011-12-02
    • 2015-12-06
    • 2017-06-05
    相关资源
    最近更新 更多