Search code examples
phpemail-headersamazon-ses

Attach custom email header for Amazon SES service


I'm using Amazon SES to send emails, I would like to attach a custom header to it before it gets sent to users since I'm making a proxy email system, for replying to threads on my site, so the ID is kept for tracking which thread to post to with the email.

I cannot see how I can attach a custom header from Amazon SES' documentation, apart from this page which explains on what headers they accept but doesn't say how to bind it, I am using this SES wrapper made for PHP.

I want to be able to inject a header named X-Thread-ID with a number, how would I go on with this?


EDIT: For Jack's answer, I cannot send an email properly, it keeps giving me this error:

CFSimpleXML Object
(
    [Type] => Sender
    [Code] => InvalidParameterValue
    [Message] => Missing final '@domain'
)

My headers is exactly like this

To: [email protected] <YES>
From: [email protected] <MySite>
X-Thread-ID: 429038

Solution

  • I'm not sure how attached you are to your current wrapper, but I just use the one that comes with Amazon SDK for PHP that can be downloaded from Amazon itself.

    $ses = new AmazonSES(AWS_ACCESS_KEY, AWS_ACCESS_SECRET);
    
    $headers = join("\r\n", array(
        "To: $recipient",
        "X-Thread-ID: 123test",
    ));
    $body = "<html><body> ... </body></html>";
    
    $res = $ses->send_raw_email(array(
        'Data' => chunk_split(base64_encode("$headers\r\n\r\n$body"))
    ), array());
    
    // check API result
    if (!$res->isOK()) {
        throw new Exception(print_r($res->body->Error, true));
    }
    // inspect message id
    $messageId = (string)$res->body->SendRawEmailResult->MessageId
    

    Edit

    This email header:

    To: [email protected] <YES>
    

    Should be (reversed):

    To: YES <[email protected]>
    

    Double quotes should be used for names with spaces.