AmazonSES and custom mail headers


A user pinged me this morning asking whether or not it was possible to set the fail-to email address when a message was bounced when using the AmazonSES API wrapper.  This is accomplished by setting the Return-Path header in the AmazonSES API wrapper.  This header is left empty by default but setting it is quite easy.  Here is an example of setting it when the component is initialized:

 public any function init(required String pathToCredentials) hint = "I initialize the gateway" {
  variables.instance = {};
  instance['awsCredentials'] = createObject("java", "java.io.File").init(arguments.pathToCredentials);
  instance['credentials'] = createObject("java", "com.amazonaws.auth.PropertiesCredentials").init(instance.awsCredentials);
  instance['props'] = createObject("java", "java.util.Properties");
  instance.props.setProperty("mail.transport.protocol", "aws");
  instance.props.setProperty("mail.aws.user", instance.credentials.getAWSAccessKeyId());
  instance.props.setProperty("mail.aws.password", instance.credentials.getAWSSecretKey());
  instance['lastUpdated'] = now();
  instance['statsRefreshTimeout'] = 5;
  instance['supportedHeaders'] = listToArray("Accept-Language,Bcc,Cc,Comments,Comment-Type,Content-Transfer-Encoding,Content-ID,Content-Description,Content-Disposition,Content-Language,Date,DKIM-Signature,DomainKey-Signature,From,In-Reply-To,Keywords,List-Archive,List-Help,List-Id,List-Owner,List-Post,List-Subscribe,List-Unsubscribe,Message-Id,MIME-Version,Received,References,Reply-To,Return-Path,Sender,Subject,Thread-Index,Thread-Topic,To,User-Agent");
  instance['endPoint'] = "";
  instance['sendQuota'] = getSendQuotaService();
  instance['sendStats'] = getSendStatisticsService();
  instance['verifiedEmailAddresses'] = listVerifiedEmailAddressesService();
  instance['sendHeaders'] = {'Return-Path'='my@email.com'};
  return this;
 }

You can also call the setHeaders(required struct headers) method to pass the default header you want to use with all emails.  If you want bounces sent to the sender of the message it can also be handled in the sendEMail() method like so:

messageObj.addHeader("Return-Path", createObject("java", "javax.mail.internet.InternetAddress").init(messageFrom).toString());

AmazonSES supports quite a few headers out of the box as well as letting set your own custom headers as well.  If there is enough interest I will go ahead and integrate the Return-Path header into the code base.   Should it be set by default or would you rather set it yourself?  Let me know.

Advertisements

About Robert Zehnder

Web application developer specializing in ColdFusion/Railo and Open Source development.

Posted on May 23, 2011, in AmazonWS, ColdFusion, Railo and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: