ColdFusion and Postmarkapp

March 1, 2010

We’re having fun playing with a lot of things with www.helpmeapp.com.  It’s turning out to be a huge mash-up of various apps, using all kinds of APIs.  Hopefully we’ll get to post about a bunch of stuff in the future, but for now, a quick bit about Postmark.

Postmark provides you with an API for sending mail from web applications.  You’re probably wondering why you’d want that, instead of just using an SMTP server — well, there’s a bunch of reasons, but not the least of which is that you don’t have to administer an SMTP server and getting blacklisted as a spammer, or worrying about patching software.  They’re in private beta at the moment, but if you ask nice on Twitter, they might just let you in.

The why isn’t the interesting part of this post though, the how is far more interesting.

HelpMeApp is built using Adobe’s ColdFusion.  No, ColdFusion isn’t open source, or the latest in trendy frameworks, but it is rock solid and has been delivering great performance for all of the API-related stuff we’re doing.  That said, Postmarkapp doesn’t have any sample code for ColdFusion developers (not that that’s a problem).  Postmark has one of the best APIs I’ve used in a while (they actually give you meaningful error messages when something goes wrong!), and it’s dead simple to use.

Enough fluff, here’s the code:


<cfset mailBody = #JSStringFormat(mailBody)#>
</cfset><cfset mailSubject = #JSStringFormat(mailSubject)#>
</cfset><cfset mailTo = #JSStringFormat(mailTo)#>

<cfsavecontent variable="jsonPacket">
<cfprocessingdirective suppressWhiteSpace="yes">
<cfoutput>
{From: 'noreply@helpmeapp.com', To: '#mailTo#', Subject: '#mailSubject#', HtmlBody: '#mailBody#'}
</cfoutput>
</cfprocessingdirective>
</cfsavecontent>

<cfhttp url="http://api.postmarkapp.com/email" method="POST">
<cfhttpparam type="header" name="Accept" value="application/json" />
<cfhttpparam type="header" name="Content-type" value="application/json" />
<cfhttpparam type="header" name="X-Postmark-Server-Token" value="*your token*" />
<cfhttpparam type="body" encoded="no" value="#jsonPacket#" />
</cfhttp>
</cfset>

That’s all there is to it. The only part that had us a little befuddled was the need to escape your text before sending it in JSON (not too experienced with JSON). But one of the great things with Postmark, is that they’ll send you back a meaningful error pointing you in the right direction as to where your error is (it will tell you something like Message: or To:). The rest is cake.

Update: Here’s a ColdFusion Component for doing the same thing. There’s a README file in the .zip archive too explaining how to use it.

Download PostmarkCFC (.zip, 4kb)

Tags: , ,

Comments are closed.