简体   繁体   English

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

[英]Sending mails via Zend Mail failing DKIM signature and going to spam in qq.com

I am struggling with sending email with ZF2. 我正在努力使用ZF2发送电子邮件。 The thing is they all work well in other mail clients I have tested but in qq.com all mail goes to spam folder. 问题是,它们在我测试过的其他邮件客户端中都能正常工作,但在qq.com中,所有邮件都进入了垃圾邮件文件夹。 Here is the code I am using to send the mail: 这是我用来发送邮件的代码:

$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);

And here are example headers from the recieved 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--

What I notice is the DKIM signature failing... I suspect it might be from the empty Sender: header line but I have tried couple of things but none could remove it... 我注意到的是DKIM签名失败...我怀疑它可能来自空的Sender:标头行,但是我尝试了几件事,但是没有人可以将其删除...

Thank you in advance! 先感谢您!

Whew, finally I have found a solution to my problem. 哇,终于我找到了解决我问题的方法。 It turns out that my version of ZF2 is far too old. 事实证明,我的ZF2版本太旧了。 In fact it is 2.2.4. 实际上是2.2.4。 The thing is that after I pass the email message to the transport, Zend internally (Smtp.php class, prepareHeaders() function) clones the Headers object and, for some reason, removes the Bcc header. 问题是,在我将电子邮件传递给传输工具之后,Zend在内部(Smtp.php类,prepareHeaders()函数)克隆了Headers对象,并出于某种原因删除了Bcc头。 Not only that but it also adds the empty Sender header (since I have not set any value for it in the client code). 不仅如此,它还会添加空的Sender标头(因为我尚未在客户端代码中为其设置任何值)。 Therefore, after passing the message to Postfix it has an empty Sender: header which it cannot be parsed in DKIM-filter. 因此,在将消息传递给Postfix之后,它具有一个空的Sender:标头,无法在DKIM过滤器中对其进行解析。 The PHP-side solution was to add a value for the Sender: header while constructing the message itself: PHP端的解决方案是在构造消息本身时为Sender:标头添加一个值:

$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']);

The final touch was to let the DKIM-filter know that the hosts, from which the emails were sent, were trusted. 最后一步是让DKIM过滤器知道从其发送电子邮件的主机是受信任的。

It took me a good while to figure this out. 我花了好一会儿才弄清楚这一点。 I hope it will be useful for someone else :) 我希望它对其他人有用:)

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

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