使用AWS sdk php通过SES发送原始电子邮件

时间:2016-08-29 10:56:33

标签: php amazon-web-services html-email aws-sdk

我正在搜索通过AWS SES服务发送HTML格式的电子邮件。

我使用PHP SDK e这是我使用的控制器的代码:

    ....
    $to         = "dev@society.com";
    // You'll need to be able to send as this address in SES
    $from       = "marketing@society.com";

    // $html = file_get_contents("../resources/email/activation/activation.html");
    // $html = sprintf($html);
    // $html = mb_convert_encoding($html, "HTML-ENTITIES", "UTF-8");

    // our html message

    $html = <<<EOF
    <!DOCTYPE html>
    <html>
    <body>
    <h1>This text should be large, because it is formatted as a header in HTML.</h1>
    <p>Here is a formatted link: <a href="http://docs.aws.amazon.com/ses/latest/DeveloperGuide/Welcome.html">Amazon SES Developer Guide</a>.</p>
    </body>
    </html>
    EOF;

    // our attachments - these are relative to the current file
    $attachments    = array();

    $mailer         = new EmailServiceProvider();

    /** @var boolean|string $messageId Identify of the email sended or false if an expetion is thrown */
    $messageId      = $mailer->send($to, $subject, $from, $html, $attachments);
    ....

EmailService的代码是:http://pastebin.com/zhKDEsvp

如果我使用此代码,则电子邮件不是HTML格式的。这就是到货:

Subject: Registrazione servizio
MIME-Version: 1.0
Content-type: multipart/alternative; boundary="207873e15f9df22df8212df410ead01cfe1b9f7c"

--207873e15f9df22df8212df410ead01cfe1b9f7c
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE html>
<html>
<body>
<h1>This text should be large, because it is formatted as a header in HTML.</h1>
<p>Here is a formatted link: <a href="http://docs.aws.amazon.com/ses/latest/DeveloperGuide/Welcome.html">Amazon SES Developer Guide</a>.</p>
</body>
</html>

--207873e15f9df22df8212df410ead01cfe1b9f7c--

其他答案说要在此处添加 base_64_encode

    $args = array(
        'Source'        => $from,
        'Destinations'  => array($to),
        'RawMessage'    => array(
            'Data' => base_64_encode($rawEmail)
        )
    );

但如果我使用它,则电子邮件不会发送并提供递送状态:

An error occurred while trying to deliver the mail to the following recipients:
dev@society.com
Action: failed
Final-Recipient: rfc822; dev@society.com
Diagnostic-Code: smtp; 550-5.7.1 [54.240.4.25      11] Our system has detected that this message is not
 550-5.7.1 RFC 5322 compliant:
 550-5.7.1 'From' header is missing.
 550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been
 550-5.7.1 blocked. Please visit
 550-5.7.1  https://support.google.com/mail/?p=RfcMessageNonCompliant
 550 5.7.1 and review RFC 5322 specifications for more information. ud19si31267939wjb.199 - gsmtp
Status: 5.7.1



---------- Messaggio inoltrato ----------
From: 
To: 
Cc: 
Date: 
Subject: 
ClN1YmplY3Q6IFJlZ2lzdHJhemlvbmUgc2Vydml6aW8gSG9wc3RvawpNSU1FLVZlcnNpb246IDEuMApDb250ZW50LXR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjkzY2E3ZTcwZDk2OWNkMGU5NzEwNzBiMGQwMjllNzUwY2E1N2VjNzciCgotLTkzY2E3ZTcwZDk2OWNkMGU5NzEwNzBiMGQwMjllNzUwY2E1N2VjNzcKQ29udGVudC1UeXBlOiB0ZXh0L2h0bWw7IGNoYXJzZXQ9aXNvLTg4NTktMQoKPCFET0NUWVBFIGh0bWw+CjxodG1sPgo8Ym9keT4KPGgxPlRoaXMgdGV4dCBzaG91bGQgYmUgbGFyZ2UsIGJlY2F1c2UgaXQgaXMgZm9ybWF0dGVkIGFzIGEgaGVhZGVyIGluIEhUTUwuPC9oMT4KPHA+SGVyZSBpcyBhIGZvcm1hdHRlZCBsaW5rOiA8YSBocmVmPSJodHRwOi8vZG9jcy5hd3MuYW1hem9uLmNvbS9zZXMvbGF0ZXN0L0RldmVsb3Blckd1aWRlL1dlbGNvbWUuaHRtbCI+QW1hem9uIFNFUyBEZXZlbG9wZXIgR3VpZGU8L2E+LjwvcD4KPC9ib2R5Pgo8L2h0bWw+CgotLTkzY2E3ZTcwZDk2OWNkMGU5NzEwNzBiMGQwMjllNzUwY2E1N2VjNzctLQo=
Message-ID: <01020156d5c31991-07b878e4-bf3d-4d2c-85b2-d8ca1dcd9c9e-000000@eu-west-1.amazonses.com>
From: marketing@society.com
Date: Mon, 29 Aug 2016 10:05:48 +0000
X-SES-Outgoing: 2016.08.29-54.240.4.25
Feedback-ID: 1.eu-west-1.sSCOXf1mGsy1MFXDAp1+U0vN705joaXLM/BN/BVnYzE=:AmazonSES
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;
        s=ihchhvubuqgjsxyuhssfvqohv7z3u4hn; d=amazonses.com; t=1472465148;
        h=Message-ID:From:Date:Feedback-ID;
        bh=frcCV1k9oG9oKj3dpUqdJg1PxRT2RSN/XKdLCPjaYaY=;
        b=B+e/8N2Ko/nEtVP+Wj8zM9q4Cm1Zn6+UDn6VlIp7s5ncZ9YsuAI0O7Q+Qnxy2BZL
        Ek3HXFc+43z0H94PZQ8B0jCKFMu3T9q1Cv2mn+WtJsSYTZGvNqvlsyYhERbPBPbAWfw
        cw48LWRnyQO66q5tOt1wW8fUTlFVFqCEE9yiE2hQ=

有什么想法吗?

非常感谢您的帮助,

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

问题是标题末尾和电子邮件正文之间缺少空格。

它需要2个空格。

$rawEmail = <<<EOE
Subject: {$subject}
MIME-Version: 1.0
Content-type: multipart/alternative; boundary="{$boundary}"{$returnPath}
To: {$to}
From: {$from}


EOE;
    // if we have some html set, lets create a new part and add it
    if ($html)
    {
        $rawEmail .= <<<EOE

--{$boundary}
Content-Type: text/html; charset=iso-8859-1

{$html}

在这种情况下,该点位于来自行和 EOE 行之间。