通过Zend Mail发送邮件失败DKIM签名并在qq.com上发送垃圾邮件

时间:2015-04-02 14:41:52

标签: php email zend-framework2 dkim

我正在努力用ZF2发送电子邮件。问题是它们在我测试过的其他邮件客户端都运行良好但在qq.com中所有邮件都转到垃圾邮件文件夹。这是我用来发送邮件的代码:

$mailTransportModel = $sl->get('MailTransportModel');

$transport = $mailTransportModel->getMailTransport($parameters['api_key']);
$messageIdHost = $config['mailServers'][$parameters['api_key']]['messageIdHost'];

$mail = new Zend\Mail\Message();
$mail->addTo($parameters['to'])
    ->setFrom($parameters['from_email'], $parameters['from_name'])
    ->setSubject(CommonLib::assignTemplateVariables($parameters['template']['subject'], $parameters['context_variables']));

if(!empty($parameters['replyto_email'])) {
    if(!empty($parameters['replyto_name'])) {
    $mail->addReplyTo($parameters['replyto_email'], $parameters['replyto_name']);
    } else {
    $mail->addReplyTo($parameters['replyto_email']);
    }
}

$headers = $mail->getHeaders();

$messageId = sha1($config['salt'] . microtime());
$headers->addHeaderLine('Message-Id', "<" . $messageId . "@" . $messageIdHost . ">");

$html = new Part(CommonLib::assignTemplateVariables($parameters['template']['body'], $parameters['context_variables']));

$body = new Zend\Mime\Message();

// Create HTML part
$html          = new Part(CommonLib::assignTemplateVariables($parameters['template']['body'], $parameters['context_variables']));
$html->type    = Mime::TYPE_HTML;
$html->charset = 'utf-8';

// Create plain text part
$stripTagsFilter   = new \Zend\Filter\StripTags();
$textContent       = str_ireplace(array("<br />", "<br>"), "\r\n", CommonLib::assignTemplateVariables($parameters['template']['body'], $parameters['context_variables']));
$textContent       = $stripTagsFilter->filter($textContent);
$textPart          = new Zend\Mime\Part($textContent);
$textPart->type    = Mime::TYPE_TEXT;

// Create separate alternative parts object
$alternatives           = new Zend\Mime\Message();
$alternatives->setParts(array($textPart, $html));
$alternativesPart       = new Zend\Mime\Part($alternatives->generateMessage());
$alternativesPart->type = Mime::MULTIPART_ALTERNATIVE . "; \n boundary=\"" . $alternatives->getMime()->boundary() . "\"";
$body->addPart($alternativesPart);
$mail->setBody($body);
$mail->setEncoding("UTF-8");

$headers->get('content-type')->setType('multipart/alternative');

$transport->send($mail);

以下是来自收到的邮件的示例标题:

Delivered-To: CUSTOMER_EMAIL
Received: by SOMEIP with SMTP id e9csp1233069oid;
        Thu, 2 Apr 2015 07:35:03 -0700 (PDT)
X-Received: by SOMEIP with SMTP id eg3mr25568854wib.11.1427985302528;
        Thu, 02 Apr 2015 07:35:02 -0700 (PDT)
Return-Path: <something@something.com>
Received: from something (something. [OTHERIP])
        by mx.google.com with ESMTP id ew1si9167339wjb.35.2015.04.02.07.35.01
        for <CUSTOMER_EMAIL>;
        Thu, 02 Apr 2015 07:35:02 -0700 (PDT)
Received-SPF: pass (google.com: domain of something@something.com designates OTHERIP as permitted sender) client-ip=OTHERIP;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of something@something.com designates OTHERIP as permitted sender) smtp.mail=something@something.com
Message-ID: <551d5394.4164c20a.5989.fffff01fSMTPIN_ADDED_BROKEN@mx.google.com>
X-Google-Original-Message-ID: =?UTF-8?Q?<LONGID@something.com>?=
Received: from HOST (OTHERHOST [SOMEIP])
    by HOST (Postfix) with ESMTP id 834E3195
    for <CUSTOMER_EMAIL>; Thu,  2 Apr 2015 17:35:01 +0300 (EEST)
X-DKIM: Sendmail DKIM Filter v2.8.3 HOST 834E3196
Date: Thu, 02 Apr 2015 17:35:01 +0300
To: CUSTOMER_EMAIL
From: =?UTF-8?Q?SOMETHING.COM?= <something@something.com>
Subject: =?UTF-8?Q?Daily=20Statement?=
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="=_c802aad96623e4d95c320e2cbceca602"
Content-Transfer-Encoding: =?UTF-8?Q?8bit?=
Sender: 

This is a message in Mime Format.  If you see this, your mail reader does not support this format.

--=_c802aad96623e4d95c320e2cbceca603
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

<SOME HTML THINGS HERE>

--=_c802aad96623e4d95c320e2cbceca603--

我注意到DKIM签名失败了...我怀疑它可能来自空的Sender:标题行,但我尝试了几件事,但没有人能删除它......

提前谢谢!

1 个答案:

答案 0 :(得分:0)

哇,最后我找到了解决问题的方法。事实证明,我的ZF2版本太旧了。实际上它是2.2.4。事情是,在我将电子邮件消息传递给传输之后,Zend内部(Smtp.php类,prepareHeaders()函数)克隆了Headers对象,并且由于某种原因,删除了Bcc头。不仅如此,它还添加了空的Sender头(因为我没有在客户端代码中为它设置任何值)。因此,在将消息传递给Postfix之后,它有一个空的Sender:标头,它无法在DKIM过滤器中解析。 PHP端解决方案是在构造消息本身时为Sender:header添加一个值:

$mail->addTo($parameters['to'])
    ->setFrom($parameters['from_email'], $parameters['from_name'])
    ->setSubject(CommonLib::assignTemplateVariables($parameters['template']['subject'], $parameters['context_variables']))
    ->setSender($parameters['from_email'], $parameters['from_name']);

最后一步是让DKIM过滤器知道发送电子邮件的主机是受信任的。

我花了很长时间来弄明白这一点。我希望它对其他人有用:)