简体   繁体   English

通过PHP向Outlook.COM发送DKIM签名的电子邮件

[英]Sending DKIM signed e-mail to Outlook.COM via PHP

I have a mail server working well with SPF, DKIM and reverse DNS configured. 我有一个邮件服务器与SPF,DKIM和反向DNS配置良好。 I can send e-mails to Outlook.com using something like: 我可以使用以下内容向Outlook.com发送电子邮件:

echo "This is only a test" | mail username@outlook.com

The problem occurs when I try to send e-mails via PHP using the same server: 当我尝试使用相同的服务器通过PHP发送电子邮件时,会出现问题:

$header .= "Return-Path: Some User <mailsender@mydomain.com>\r\n";
$header .= "From: Some User <mailsender@mydomain.com>\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "User-Agent: Some User Mail Sender\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";

mail("usernama@outlook.com","My title", "Message body", $header);

I tried to verify my message using appmaildev.com and the report says: 我尝试使用appmaildev.com验证我的消息,报告说:

DKIM result: fail (wrong body hash: <*** body hash ***>)

Even with this error, Outlook.com says it passed DKIM verification, but all messages sent by PHP mail function go to junk folder. 即使出现此错误,Outlook.com也表示已通过DKIM验证,但PHP邮件功能发送的所有邮件都会转到垃圾邮件文件夹。 Here are examples of messages sent directly via Bash and via PHP: http://pastebin.com/ndXJszic 以下是通过Bash和PHP直接发送的消息示例: http//pastebin.com/ndXJszic

Can anyone help me? 谁能帮我?

Thanks. 谢谢。

EDIT After removing \\r from headers, the DKIM body hash error is gone. 编辑从标题中删除\\ r \\ n后,DKIM正文哈希错误消失了。 But I still can't send e-mails to Outlook... 但我还是无法向Outlook发送电子邮件......

This could be a permission issue. 这可能是一个许可问题。

Your web server normally runs as a different user than when you use mail on the command line, so setting the From: header will create additional warning headers in the outgoing email. 您的Web服务器通常以与在命令行上使用mail时不同的用户身份运行,因此设置From:标头将在传出电子邮件中创建其他警告标头。

There's a file you can modify on your server called /etc/mail/trusted-users . 您可以在服务器上修改一个名为/etc/mail/trusted-users Make sure that the user apache (or whatever user your php script is running as) appears in that file; 确保用户apache (或运行php脚本的任何用户)出现在该文件中; if not, add the user name and then reload sendmail . 如果没有,请添加用户名,然后重新加载sendmail

Example contents of /etc/mail/trusted-users : /etc/mail/trusted-users示例内容:

# trusted-users - users that can send mail as others without a warning
# apache, mailman, majordomo, uucp, are good candidates
apache

First is impossible to be sure that an email will not be marked as spam, the only way is that the person who receive the email add the sender address in a white list. 首先不可能确定电子邮件不会被标记为垃圾邮件,唯一的方法是收到电子邮件的人将发件人地址添加到白名单中。

SPF and DKIM are only to guarantee that the email comes from that domain or email, but not to guarantee that it is not spam. SPF和DKIM只是为了保证电子邮件来自该域或电子邮件,但不保证它不是垃圾邮件。

The antispam system in outlook.com as many others check a lot of things, for example the sender ip address, how many email comes from that ip per hour, the content of the email (text, links), reputation, etc. 与许多其他人一样,outlook.com中的反垃圾邮件系统会检查很多内容,例如发件人IP地址,每小时发送的邮件数量,电子邮件内容(文本,链接),声誉等。

In your example you don't show the body content, maybe it's different and for this reason one email is marked as spam and the other it's not. 在您的示例中,您不显示正文内容,也许它不同,因此一封电子邮件被标记为垃圾邮件,另一封电子邮件则不标记为垃圾邮件。

Try using Pear mail , and making a wrapper class around that. 尝试使用Pear邮件 ,并围绕它做一个包装类。 I'm using that with DKIM, and have no problems. 我正在使用它与DKIM,没有问题。 I should mention that I am also using SpamAssassin (as previously mentioned), and ClamAV. 我应该提一下,我也在使用SpamAssassin(如前所述)和ClamAV。

<?php

// Include the Pear Mail header
require_once '/usr/share/php/Mail.php';


class MailWrapper {
    public static function Send($to, $subject, $body) {
        // Email details
        $from = 'No Reply <noreply@yourdomain.com>';
        $server = 'mail.yourdomain.com';
        $port = 25;
        $username = 'sending.account@yourdomain.com';
        $password = 'yourp4ssw0rd';
        // Formalize mail server connection info
        $headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject,
                 'Date' => date('r'),
                 'Return-Path' => $from,
                 'Content-Type' => 'text/html; charset=UTF-8',
                 'Content-Transfer-Encoding' => '7bit',
                 'Mime-Version' => '1.0',
                 'X-Mailer' => 'Your Company (https://yourdomain.com)',
                 'X-Accept-Language' => 'en',
                 'Message-ID' => sha1($body).'@yourdomain.com'
        );
        $connection = array('host' => $server,
                    'auth' => true,
                    'username' => $username,
                    'password' => $password
        );
        // Create the mail server connection 
        $smtp = Mail::factory('smtp', $connection);
        // Send the message
        $mail = $smtp->send($to, $headers, $body);
        // Check for errors
        if (PEAR::isError($mail)) {
            echo '! [email] ['.time().'] Failed sending mail to "'.$to.'"'.PHP_EOL;
            $result = false;
        } else {
            echo '  [email] ['.time().'] Mail sent to "'.$to.'"'.PHP_EOL;
            $result = true;
        }
        return $result;
    }
}

?>

When troubleshooting email deliverability issues, I use Port 25's email check. 在解决电子邮件可传递性问题时,我使用Port 25的电子邮件检查。

It will tell you what passes / does not pass and how SpamAssasin ranks your message. 它会告诉你通过/不通过的内容以及SpamAssasin如何对你的消息进行排名。

The URL is: http://www.port25.com/support/authentication-center/email-verification/ 该URL为: http//www.port25.com/support/authentication-center/email-verification/

To receive the results directly to any address, the address need to be added to the check-auth address. 要将结果直接接收到任何地址,需要将地址添加到check-auth地址。 For example, to send the results to: jsmith@yourdomain.com the sample message should be sent to check-auth-jsmith=yourdomain.com@verifier.port25.com. 例如,要将结果发送到:jsmith@yourdomain.com,应将示例消息发送到check-auth-jsmith=yourdomain.com@verifier.port25.com。

Using this you can find out if your DKIM is working properly/validated, and what your SpamAssasin score is. 使用此功能,您可以了解您的DKIM是否正常/有效,以及您的SpamAssasin得分是多少。

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

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