Sending emails with Amazon SES API SendRawEmail in ColdFusion

Thursday 28 February 2013 10:50 AM

It is quite easy to use Amazon SES (Simple Email Service) with ColdFusion, just use CFMAIL tag and send mails out thought Amazon SMTP. Also bit more difficulty we can use SES API to do the same thing too. The biggest advantage, probably the only advantage of using the API instead SMTP call is API call returns the "message-id" assigned to our emails. This simple ID is immensely useful for tracking down emails if they bounces. And Amazon SNS (Simple Notification Service) make it quite easier to keep track of bounces with the "message-id". I might write another post about how to handle bounces and complains with SNS.

SES API have two mail send functions, SendEmail and SendRawEmail. SendEmail is the simplest and it only accepts plain texts emails with no attachments. SendRawEmail accept multipart emails, means Html, attachments, inline images, custom headers and everything bright and beautiful. But we have to manually create and format the Multipart email message before we send to SES API.

Here I put together two custom tags <CF_Mail> and <CF_Mailparam> to use with Amazon SES SendRawEmail call. These two tags tries to mimic our beloved CFMAIL and CFMAILPARAM tags, create multipart emails with attach files, inline images, html or plan and most importantly will returns the variable "MessageId" with the messag- ID of your email.

Use this like CFMAIL tag. MAIL tag supports from,to,cc,replyto,priority,type and Subject attributes just like CFMAIL, but default "Type" is HTML. Also there are two other attributes "Key" and "SecretKey" to hold your AWS credentials.

MAILPARAM tags tries to behave similar to CFMAILPARAM tag and it supports name,value,file,disposition and contentID attributes.

Something else to consider - change from specifying Key and SecretKey in the tag to relying on the application variable. It's one less thing to update if those change, especially if the tag is used in several places.