简体   繁体   English

邮件标题导致失败

[英]Mail Headers Cause Failure

I am making a contact form. 我正在填写联系表格。 I've gotten a rudimentary version to work (gathering form info and sending an email from my site to my personal email) but I cannot seem to get the 'additional headers' to work. 我已经有了一个基本的版本(收集表格信息并将电子邮件从我的网站发送到我的个人电子邮件),但是我似乎无法使用“附加标题”。 It works fine if I have the following headers: 如果我具有以下标头,则可以正常工作:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

But if I try and add additional Mail headers such as: 但是,如果我尝试添加其他邮件标头,例如:

$headers .= 'To: Jack <Jack Johnson>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
$headers .= "\r\nX-Mailer: PHP/" ;

I get a 'fail' on the mail function. 我在邮件功能上遇到了“失败”。 I'm using PHP version 5.3.8. 我正在使用PHP版本5.3.8。 To make sure the mail function is working I am doing this: 为了确保邮件功能正常运行,我正在这样做:

$sendmail = mail($email_to, $email_subject, $email_message, $headers);

    if ($sendmail) {
        echo '<div>Thanks for submitting!</div>';
    } else {
        echo '<div>Fail</div>';
    }

Am I formatting this incorrectly? 我格式化不正确吗?

    $headers .= 'To: Jack <Jack Johnson>' . "\r\n";

As mentioned in comments, this doesn't need to be there. 正如评论中提到的那样,这里不必存在。 But it will almost certainly cause a problem because it doesn't contain an email address 但这几乎肯定会引起问题,因为它不包含电子邮件地址

Also as mentioned in the comments, you have a double \\r\\n by including it at the start of 另外,如注释中所述,您可以在\\r\\n的开头添加双\\r\\n

    $headers .= "\r\nX-Mailer: PHP/" ;

Finally, this shouldn't cause the problem, but you really shouldn't do it: 最后,这不应该引起问题,但您实际上不应该这样做:

    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

Bcc lines do not belong in the header. 密件抄送行不属于标题。 They will appear in the header for all recipients, undermining the point of Bcc. 它们将出现在所有收件人的标题中,从而破坏了密件抄送的意义。 You will find this is handled intermittently in different mail clients and services. 您会发现这是在不同的邮件客户端和服务中间歇地处理的。 Some will display it, some will just keep it, some will "kindly" hide it from the headers. 有些会显示它,有些会保留它,有些会从标题中“友好地”隐藏它。

PHP's mail() isn't really designed to handle Bcc, so you will probably need to call the mail() function and separately send to the Bcc recipient PHP的mail()并不是专门为处理密件抄送而设计的,因此您可能需要调用mail()函数并单独发送给密件抄送收件人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM