PHP邮件头问题

时间:2014-05-30 03:08:10

标签: php email contact-form email-headers

我目前有两个问题:

  1. 在我的Gmail中,From:标题并不尊重我的联系表单'名称。它违反了其他内容。

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

  3. 我确定我错过了一些细节,所以如果我错过了某些内容,请告知我,我会添加它。

    这是我的代码:

    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" );
    

2 个答案:

答案 0 :(得分:1)

其中有一条线路缺少连接符号(.),导致$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"; 

答案 1 :(得分: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";