Attach custom email header for Amazon SES service
-
03-07-2021 - |
Question
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: myemail@hotmail.co.uk <YES>
From: developer@mysite.com <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: myemail@hotmail.co.uk <YES>
Should be (reversed):
To: YES <myemail@hotmail.co.uk>
Double quotes should be used for names with spaces.
OTHER TIPS
Fast-forward to 2017, the answer now is (according to Amazon):
(ref: Send an Email by Accessing the Amazon SES SMTP Interface Programmatically)
<?php
// Modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require 'path_to_sdk_inclusion';
// Instantiate a new PHPMailer
$mail = new PHPMailer;
// Tell PHPMailer to use SMTP
$mail->isSMTP();
// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
$mail->setFrom('sender@example.com', 'Sender Name');
// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
// Also note that you can include several addAddress() lines to send
// email to multiple recipients.
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Replace smtp_username with your Amazon SES SMTP user name.
$mail->Username = 'smtp_username';
// Replace smtp_password with your Amazon SES SMTP password.
$mail->Password = 'smtp_password';
// Specify a configuration set. If you do not want to use a configuration
// set, comment or remove the next line.
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', 'ConfigSet');
// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
$mail->Host = 'email-smtp.us-west-2.amazonaws.com';
// The port you will connect to on the Amazon SES SMTP endpoint.
$mail->Port = 465;
// The subject line of the email
$mail->Subject = 'Amazon SES test (SMTP interface accessed using PHP)';
// The HTML-formatted body of the email
$mail->Body = '<h1>Email Test</h1>
<p>This email was sent through the
<a href="https://aws.amazon.com/ses">Amazon SES</a> SMTP
interface using the <a href="https://github.com/PHPMailer/PHPMailer">
PHPMailer</a> class.</p>';
// Tells PHPMailer to use SMTP authentication
$mail->SMTPAuth = true;
// Enable SSL encryption
$mail->SMTPSecure = 'ssl';
// Tells PHPMailer to send HTML-formatted email
$mail->isHTML(true);
// The alternative email body; this is only displayed when a recipient
// opens the email in a non-HTML email client. The \r\n represents a
// line break.
$mail->AltBody = "Email Test\r\nThis email was sent through the
Amazon SES SMTP interface using the PHPMailer class.";
if(!$mail->send()) {
echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
} else {
echo "Email sent!" , PHP_EOL;
}
?>