Amazon Simple Email Service API ColdFusion Wrapper

On November 17, 2011, in ColdFusion, AWS, by Anuj Gakhar

Amazon Simple Email Service is one of the services provided by Amazon Web Services. Directly quoting from their website :-

Amazon Simple Email Service (Amazon SES) is a highly scalable and cost-effective bulk and transactional email-sending service for businesses and developers. Amazon SES eliminates the complexity and expense of building an in-house email solution or licensing, installing, and operating a third-party email service. The service integrates with other AWS services, making it easy to send emails from applications being hosted on services such as Amazon EC2. With Amazon SES there is no long-term commitment, minimum spend or negotiation required – businesses can utilize a free usage tier and after that enjoy low fees for the number of emails sent plus data transfer fees.

I have written an API wrapper for their API which lets you perform all the actions provided by their API. The code is available on github if you want to download it. You are going to need your Amazon Access Key and Secret key to use this CFC. In order to start using the CFC, first of all, you initialise it passing in your credentials.

[cf]ACCESS_KEY = "access_key";
SECRET_KEY = "secret_key";

objSes = createObject("com.anujgakhar.AmazonSES").init(
accessKey="#ACCESS_KEY#",
secretKey="#SECRET_KEY#"
);
[/cf]

To send an email using the service, the sender’s email address must be verified with the service first. To verify an email address using the CFC, you do as follows. This sends out a confirmation email to the address and they must click the link in the email to get themselves verified.

[cf]objSes.verifyEmailAddress("your_email@address.com");[/cf]

In order to list all the verified email addresses :-

[cf]objSes.listVerifiedEmailAddresses();[/cf]

In order to delete a verified email address :-

[cf]objSes.deleteVerifiedEmailAddress("your_email@address.com");[/cf]

You can also look at your allowed sending quota using the CFC :-

[cf]objSes.getSendQuota();[/cf]

To look at your sending statistics. The result is a list of data points, representing the last two weeks of sending activity.

[cf]objSes.getSendStatistics();[/cf]

And finally, to send an email. You must have your account in Production mode to be able to send emails to anyone, otherwise they only allow sending emails to verified email addresses.

[cf]args = {};
args.to = [];
arrayAppend(args.to,"recipient1@address.com");
arrayAppend(args.to,"recipient2@address.com");
args.from = "your_verified@address.com";
args.subject = "Test Email via the API";
args.messagetext = "This is the body of the email. This email is going out via the API";
objSes.sendEmail(argumentCollection = args);[/cf]

Tagged with:  

15 Responses to Amazon Simple Email Service API ColdFusion Wrapper

  1. Cool, this will come in handy!

  2. TD says:

    Great job! We’ve looked at AWS for emailing for some time. This looks super useful. Thanks!

  3. JT says:

    I just tried your code out and appear to be getting “Invalid CFML construct found on line 323 at column 39” that is referencing the cfc that I downloaded from your website.

    • Anuj Gakhar says:

      Did you download from GitHub? And what version of CF are you on ?

      • JT says:

        I went to GitHub, pulled up the raw code and copied/pasted it to the same filename on my server. I am running CF8 Standard.

        • Anuj Gakhar says:

          I have just now pushed an update to Github. Try that and let me know. I don’t have access to CF8 so I could not try. I have only tried this on Railo as of yet and that seems to work. Let me know if any issues still.

          • JT says:

            Now I’m getting the following error message.

            Element INSTANCE.ENDPOINTURL is undefined in VARIABLES.

            The error occurred in C:\Inetpub\www\AmazonSES.cfc: line 339
            Called from C:\Inetpub\www\AmazonSES.cfc: line 168
            Called from C:\Inetpub\www\TestEmail.cfm: line 1

            337 :
            338 : <cfhttp method="#arguments.method#"
            339 : url="#variables.instance.endPointUrl#/"
            340 : charset="utf-8"
            341 : result="HTTPResults"

  4. Anuj Gakhar says:

    Hi JT, Get the latest version from Github – I have now fixed it for Adobe CF as well. Works fine on my CF 9 and Railo ….

  5. JT says:

    I have been testing it and have not seen any crashes since the last update.

  6. Tom Sucaet says:

    Is it possible to send attachments with this API CF Wrapper?

  7. […] way to send emails was either using Amazon’s SDK or using their REST API (which I wrote a ColdFusion wrapper for as well) but now, you can use the native cfmail tag to send out emails – which I think is […]

  8. Dan Moore says:

    This is fantastic and works exactly as advertised with CF9. Thanks for sharing your work!

    For those considering using Amazon’s SMTP interface instead of the API, consider that the API returns a UID which makes processing bounces/complaints much more reliable. Bounces include the following header: X-Original-To: messageIdReturnedByAPICall@amazonses.com

Leave a Reply to TD Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: