ptesser ptesser - 3 months ago 20
PHP Question

Send Raw Email via SES using the AWS sdk php

I'm searching to send an HTML formatted email by AWS SES service.

I'm using PHP SDK e this is the code of the controller that I use:

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


The code of the EmailService is: http://pastebin.com/zhKDEsvp

If I use this code the email is not HTML formatted. This is what arrive:

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--


Other answers say to add base_64_encode here:

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


But if I use it, the email isn't send and gives a Delivery Status:

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=


Any idea?

Thank you so much for the help,

Paolo

Answer

I've found the solution.

The problem was a missing space between the end of the header and the body of the email.

It needs 2 spaces.

$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}

In this case the point is between From line and EOE line.