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

时间:2012-11-27 23:22:04

标签: php email outlook smtp dkim

我的邮件服务器运行良好,配置了SPF,DKIM和反向DNS。我可以使用以下内容向Outlook.com发送电子邮件:

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

当我尝试使用相同的服务器通过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);

我尝试使用appmaildev.com验证我的消息,报告说:

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

即使出现此错误,Outlook.com也表示已通过DKIM验证,但PHP邮件功能发送的所有邮件都会转到垃圾邮件文件夹。以下是通过Bash和PHP直接发送的消息示例:http://pastebin.com/ndXJszic

任何人都可以帮助我吗?

感谢。

修改 从标头中删除 \ r 后,DKIM正文哈希错误消失了。但我仍然无法向Outlook发送电子邮件......

4 个答案:

答案 0 :(得分:1)

这可能是一个许可问题。

您的Web服务器通常以与在命令行上使用mail时不同的用户身份运行,因此设置From:标头将在外发电子邮件中创建其他警告标题。

您可以在服务器上修改一个名为/etc/mail/trusted-users的文件。确保用户apache(或您的php脚本正在运行的任何用户)出现在该文件中;如果没有,请添加用户名,然后重新加载sendmail

/etc/mail/trusted-users的示例内容:

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

答案 1 :(得分:1)

首先不可能确定电子邮件不会被标记为垃圾邮件,唯一的方法是收到电子邮件的人将发件人地址添加到白名单中。

SPF和DKIM只是为了保证该电子邮件来自该域名或电子邮件,但不保证其不是垃圾邮件。

outlook.com中的反垃圾邮件系统和许多其他人一样检查很多东西,例如发件人IP地址,每小时有多少电子邮件来自该IP,电子邮件的内容(文本,链接),声誉等等

在你的例子中,你没有显示正文内容,也许它是不同的,因此一封电子邮件被标记为垃圾邮件而另一封电子邮件则没有。

答案 2 :(得分:0)

尝试使用Pear mail,然后围绕它创建一个包装类。我正在使用它与DKIM,没有问题。我应该提一下,我也在使用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;
    }
}

?>

答案 3 :(得分:0)

在解决电子邮件可传递性问题时,我使用了Port 25的电子邮件检查。

它将告诉您传递/未传递的内容以及SpamAssasin如何对您的信息进行排名。

网址是: http://www.port25.com/support/authentication-center/email-verification/

  

要将结果直接接收到任何地址,需要将地址添加到check-auth地址。例如,要将结果发送到:   jsmith@yourdomain.com示例消息应发送至check-auth-jsmith=yourdomain.com@verifier.port25.com。

使用此功能,您可以了解您的DKIM是否正常/有效,以及您的SpamAssasin得分是多少。