Sending emails with Amazon SES API SendRawEmail in ColdFusion

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.

   1: <cf_mail
   2:  from = " from address goes here - mandatory"
   3:  to = " to address goes here - mandatory"
   4:  cc = " CC address list "
   5:  replyto = " Replay to Email address if needed "
   6:  priority = " A value between 1 to 5"
   7:  type = "html"
   8:  Subject = "My Mail Subject"
   9:  Key = "<Your Access Key Goes Here>"
  10:  SecretKey = "<Your SecretKey Goes Here>">
  11:  <cf_mailparam name="X-Header-Custom" value="Value">
  12:  <cf_mailparam file="#ExpandPath('./images/inlineImage.gif')#" disposition="inline" contentID="image1">
  13:  <cf_mailparam file="#ExpandPath('./images/AttachedImage.gif')#">
  14: Hello<p />
  15: This is my <i>Test</i> Email <p>
  16: Here is a inline image <img src="cid:image1">
  17: </cf_mail>
  18: This tag returns : <cfoutput>#MessageId#</cfoutput>
Show/Hide Line Numbers . Full Screen . Plain

Now here are the two custom tags:

Monday 25 November 2013 09:49 PM
Yes, please on the SES to SNS for notifying of bounces!!
Tuesday 26 November 2013 05:26 PM
I have another post on how to do that
Joseph LaForest
Friday 14 June 2013 01:19 PM
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.
Joseph LaForest
Friday 14 June 2013 12:29 PM
This is great when there are no errors from Amazon. I've changed a section to process the error. Not pretty but effective.

<cfif StructKeyExists(result,'Filecontent') and IsXML(result.Filecontent)>
<cfif result.Statuscode eq '200 OK'>
<cfset this.MessageId = XMLParse(result.Filecontent).SendRawEmailResponse.SendRawEmailResult.MessageId.XmlText>
<cfset this.MessageId = 'ERROR: '& XMLParse(result.Filecontent).ErrorResponse.Error.Message.XmlText>
Tuesday 17 September 2013 02:18 PM
Thanks Joseph. I updated the Gist.
Thursday 30 May 2013 09:26 PM
I have an error on mail.cfm on this line..
148 : <cfset local.key = local.key.init(local.jKey,"HmacSHA256") />

Object instantiation exception.
An exception occurred while instantiating a Java object. The class must not be an interface or an abstract class. Error: ''.
Friday 31 May 2013 12:45 AM
What CF version you are on?