Question

I'm trying to send a raw email with image attachments via AmazonSES using PHP. It works great when I send the email to a gmail account but hotmail accounts are receiving empty attached images. In other words, hotmail seems to recognize that there are attachments and these attachments have the correct name that i specified, it's just that they are always empty, sized 0 bytes. Googling is not helping... Thanks in advance!

$amazonSES = new AmazonSES();

// if (empty($attach)==0) {
    // $response = $amazonSES->send_email(AWS_SES_FROM_EMAIL,
        // array('ToAddresses' => array($to)),
        // array('Subject.Data' => $subject,'Body.Text.Data' => $messagein,)
    // );
// } else {
    $rstring = 'ajfas90lsjhntlen89y34oi598';

    $message= "To: ".$to."\n";
    $message.= "From: " . AWS_SES_FROM_EMAIL . "\n";
    $message.= "Subject: " . $subject . "\n";
    $message.= "MIME-Version: 1.0\n";
    $message.= 'Content-Type: multipart/mixed; boundary="ARandomString'.$rstring.'"';
    $message.= "\n\n";
    $message.= "--ARandomString$rstring\n";
    $message.= 'Content-Type: text/plain; charset="utf-8"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: 7bit\n";
    $message.= "Content-Disposition: inline\n";
    $message.= "\n";
    $message.= $messagein;
    $message.= "\n\n";
    $message.= "--ARandomString$rstring\n";

    foreach ($attach as $attachment) {
        // $message.= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_@domain.com_IS_ADDED\>\n";
        $message.= "Content-ID: \<". md5(uniqid(rand(), true)) ."@biomechanico.com\>\n";
        $message.= 'Content-Type: application/zip; name="shell.zip"';
        $message.= "\n";
        $message.= "Content-Transfer-Encoding: base64\n";
        $message.= 'Content-Disposition: attachment; filename="' . $attachment["name"] . '"';
        $message.= "\n" . base64_encode(file_get_contents($attachment["file"])) . "\n";
        $message.= "--ARandomString$rstring\n";
    }

    $response = $amazonSES->send_raw_email(array(
                    'Data'=> base64_encode($message)),
                         array('Source'=>AWS_SES_FROM_EMAIL, 'Destinations'=> $to));
Was it helpful?

Solution

You are producing a malformed message.

Consider using a proper library for generating mail messages, instead of stitching them together from raw strings.

Otherwise, here’s what I notice right away.

  1. The final multipart boundary must be terminated by an extra --, i.e. the last line must be:

--ARandomStringajfas90lsjhntlen89y34oi598--

rather than

--ARandomStringajfas90lsjhntlen89y34oi598

  1. In the attachment parts, you do not have a blank line between the Content-Disposition header line and the body.

  2. Message lines must not be longer than 998 characters, but your Base64-encoded attachment data, however long, is always on a single line.

  3. As far as I understand PHP, your syntax for Content-ID in the attachment parts is wrong, in that it produces Content-ID: \<whatever\> but should produce Content-ID: <whatever>

  4. Lines must be terminated by CR LF (\r\n), but you have LF (\n).

A good way to debug message problems is to take the actual full generated message source ($message) and run it through Message Lint. If the above suggestions do not help, please post the generated message source rather than the PHP code.

For the Internet message format, please refer to RFC 5322. For the multipart message syntax, please refer to RFC 2046.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top