简体   繁体   English

带有From:标头的PHP mail()阻止传递

[英]PHP mail() with From: header prevents delivery

First things first... I know mail deliverability is effected by hundreds of factors and so it's never possible to say for SURE if one specific thing is the cause. 首先,第一件事...我知道邮件的传递能力受数百个因素影响,因此,绝对不能确定是由某件事引起的。 (I do not expect this.) (我希望这样。)

My question is whether is normal/expected for mail servers to block messages which have a different From: header. 我的问题是邮件服务器阻塞/ From:标题不同的邮件是否正常/是否正常。 My contact form handler uses this so that the reciptient can reply to the sender (code below). 我的联系表单处理程序使用此格式,以便收件人可以回复发件人(下面的代码)。 Gmail accounts are able to receive the messages. Gmail帐户能够接收邮件。 But my client has an institutional .edu email address and the emails aren't getting delivered. 但是我的客户有一个机构的.edu电子邮件地址,并且电子邮件未送达。

Here's a minimal version of my code: 这是我的代码的最低版本:

<?php 

$first_name = 'John';
$last_name = 'Smithy';
$email = "myemail@whyisntthisworking.com";
$msg = "This is a test email. If you received it things are working.";

$test_email = (isset($_POST['test_email'])) ? $_POST['test_email'] : FALSE;

$to;
$from = $email;
$subject = "Contact from JamesGregoryMD.com";
$body = wordwrap( "{$first_name} {$last_name}:\n\n{$msg}\n\n{$email}\n{$phone}", 70);
$headers = "From: {$from}" . "\r\n";

if ($test_email) {
    # send email
    $return = mail($test_email,"Test email",$body, $headers);

    if ($return) {
        echo "<h1>MAIL SENT</h1>";
    } else {
        echo "<h1>MAIL NOT SENT</h1>";
    }
}
?>

<form id="contact" method="post">
    <input type="email" name="test_email" placeholder="email" />
    <input type="submit" value="Send" />
</form>

What you are effectively doing is "spoofing" the From address. 您实际上正在做的是“欺骗”发件人地址。 This is perfectly acceptable according to the definition of SMTP, but only because when the Internet was young, nobody thought that it would be abused. 根据SMTP的定义,这是完全可以接受的,但这只是因为当Internet成立之初,没有人认为它将被滥用。 Unfortunately, this leaves the door wide open to spammers, so various checks are generally implemented which you may be running into: 不幸的是,这给垃圾邮件发送者敞开了大门,因此通常会进行各种检查,您可能会遇到以下问题:

  • When connecting to an SMTP server (to deliver mail to a user on that domain), the client sends a "HELO" (or extended "EHLO") message with their identity. 当连接到SMTP服务器(以将邮件传递给该域上的用户)时,客户端将发送带有其身份的“ HELO”(或扩展的“ EHLO”)消息。 Some servers will reject messages where this doesn't match the From header. 一些服务器将拒绝与From头不匹配的消息。 Depending on the setup, you may get some joy be adding "-f$from_address" as the 5th parameter to the mail() function. 根据设置的不同,将"-f$from_address"作为第5个参数添加到mail()函数中可能会带来一些乐趣。
  • Other servers may verify that you're actually connecting from the domain mentioned in that parameter, so still won't like it. 其他服务器可能会验证您实际上是从该参数中提到的域进行连接,因此仍然不喜欢它。
  • More sophisticated systems will check DNS records on the domain of the from address for allowed sender information. 更复杂的系统将检查发件人地址域上的DNS记录,以查找允许的发件人信息。 Look up "SPF" and "DKIM" online and you should find info on how to check and set them up. 在线查找“ SPF”和“ DKIM”,您应该找到有关如何检查和设置它们的信息。

使用Reply-To:标头而不是From: ,您可能不会遇到此问题(但将获得相同的功能)。

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

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