邮件标头导致失败

发布于 2025-01-01 16:17:06 字数 1263 浏览 2 评论 0原文

我正在制作联系表。我已经得到了一个基本版本的工作(收集表单信息并将电子邮件从我的网站发送到我的个人电子邮件),但我似乎无法让“附加标题”工作。如果我有以下标头,它工作正常:

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

但如果我尝试添加其他邮件标头,例如:

$headers .= 'To: Jack <Jack Johnson>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";
$headers .= "\r\nX-Mailer: PHP/" ;

我在邮件功能上收到“失败”。我正在使用 PHP 版本 5.3.8。为了确保邮件功能正常工作,我正在这样做:

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

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

我的格式是否错误?

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 <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";
$headers .= "\r\nX-Mailer: PHP/" ;

I get a 'fail' on the mail function. I'm using PHP version 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2025-01-08 16:17:06
    $headers .= 'To: Jack <Jack Johnson>' . "\r\n";

正如评论中提到的,这不需要在那里。但它几乎肯定会导致问题,因为它不包含电子邮件地址

另外,正如评论中提到的,您可以通过将其包含在最后的开头来获得双 \r\n

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

,这应该不会导致问题,但您确实不应该这样做:

    $headers .= 'Bcc: [email protected]' . "\r\n";

密件抄送行不属于标头。它们将出现在所有收件人的标题中,从而破坏密件抄送的意义。您会发现不同的邮件客户端和服务会间歇性地处理此问题。有些会显示它,有些会保留它,有些会“友善地”将其隐藏在标题中。

PHP 的 mail() 并不是真正设计来处理密件抄送的,因此您可能需要调用 mail() 函数并单独发送给密件抄送收件人

    $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

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

Finally, this shouldn't cause the problem, but you really shouldn't do it:

    $headers .= 'Bcc: [email protected]' . "\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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文