ptesser ptesser - 1 year ago 177
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 = "";
// You'll need to be able to send as this address in SES
$from = "";

// $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>
<h1>This text should be large, because it is formatted as a header in HTML.</h1>
<p>Here is a formatted link: <a href="">Amazon SES Developer Guide</a>.</p>

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

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"

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

<!DOCTYPE html>
<h1>This text should be large, because it is formatted as a header in HTML.</h1>
<p>Here is a formatted link: <a href="">Amazon SES Developer Guide</a>.</p>


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:
Action: failed
Final-Recipient: rfc822;
Diagnostic-Code: smtp; 550-5.7.1 [ 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 and review RFC 5322 specifications for more information. ud19si31267939wjb.199 - gsmtp
Status: 5.7.1

---------- Messaggio inoltrato ----------
Message-ID: <>
Date: Mon, 29 Aug 2016 10:05:48 +0000
X-SES-Outgoing: 2016.08.29-
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;
s=ihchhvubuqgjsxyuhssfvqohv7z3u4hn;; t=1472465148;

Any idea?

Thank you so much for the help,


Answer Source

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}

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

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


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download