Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 11:18
Show Gist options
  • Save bennadel/9759694 to your computer and use it in GitHub Desktop.
Save bennadel/9759694 to your computer and use it in GitHub Desktop.
Using Postmark To Track User Data Through Email Bounce Backs
<!---
Define the outgoing email properties. We are going to be using
CFHTTP post post the JSON (Javascript Object Notation) version
of these propreties to the PostMark API.
--->
<cfset emailSettings = {
to = "tricia@triciatacular.com",
from = "ben+from@bennadel.com",
subject = "PostMark Bounce Back Testing",
htmlBody = "Hello, this is a custom header test.",
headers = [
{
name = "X-Customer-ID",
value = "C12345"
}
]
} />
<!--- Post the email to the PostMark server. --->
<cfhttp
result="post"
method="post"
url="http://api.postmarkapp.com/email">
<!---
Alert the server that the we can accept JSON as the type of
data returned in the response.
--->
<cfhttpparam
type="header"
name="accept"
value="application/json"
/>
<!---
Alert the server that the email content will be serialized
in the post body as JSON text.
--->
<cfhttpparam
type="header"
name="content-type"
value="application/json"
/>
<!--- Define the API key to authorize post. --->
<cfhttpparam
type="header"
name="X-Postmark-Server-Token"
value="#request.apiKey#"
/>
<!---
Post the serialized JSON email properties as the HTTP
message body.
--->
<cfhttpparam
type="body"
value="#serializeJSON( emailSettings )#"
/>
</cfhttp>
<!---
Get the bounce-back information from the PostMark server. When
doing this, we have a number of possible filters. For our use,
we're just gonna filters on email LIKE'ness.
--->
<cfhttp
result="get"
method="get"
url="http://api.postmarkapp.com/bounces">
<!---
Alert the server that the we can accept JSON as the type of
data returned in the response.
--->
<cfhttpparam
type="header"
name="accept"
value="application/json"
/>
<!--- Define the API key to authorize post. --->
<cfhttpparam
type="header"
name="X-Postmark-Server-Token"
value="#request.apiKey#"
/>
<!--- Pass in the email filter. --->
<cfhttpparam
type="url"
name="emailFilter"
value="tricia@triciatacular.com"
/>
<!---
Pass in the number of bounce backs that we want to list
(PostMark provides implicit pagination of all bounce-back
records, starting with the most recent first).
--->
<cfhttpparam
type="url"
name="count"
value="1"
/>
<!---
Pass in the paging offset (which bounce back index will
start the given page) - zero is the first page.
--->
<cfhttpparam
type="url"
name="offset"
value="0"
/>
</cfhttp>
<!---
Deserialize the response JSON. This should give us a structure
that contains the returned bounces plus the total number of
bounces in the system (returned or otherwise) that match the
given set of filtering criteria.
--->
<cfset response = deserializeJSON( toString( get.fileContent ) ) />
<!--- Output the bounce back response. --->
<cfdump
var="#response#"
label="PostMark Bounce Backs"
/>
<br />
<br />
<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->
<!---
Get the unique ID of the email from the bounce back data. This
is an ID genreated by Postmark - it is not *our* ID value.
--->
<cfset bounceID = response.bounces[ 1 ].id />
<!---
Now that we have the ID, we can get the full bounce-back
dump - this is the raw source that Postmark recieved in the
bounce back email.
--->
<cfhttp
result="getDump"
method="get"
url="http://api.postmarkapp.com/bounces/#bounceID#/dump">
<!---
Alert the server that the we can accept JSON as the type of
data returned in the response.
--->
<cfhttpparam
type="header"
name="accept"
value="application/json"
/>
<!--- Define the API key. --->
<cfhttpparam
type="header"
name="X-Postmark-Server-Token"
value="#request.apiKey#"
/>
</cfhttp>
<!---
Postmark returns a JSON structure containing one key - BODY -
which contains the raw source of the bounce back email. Let's
deserialize this JOSN response.
--->
<cfset dumpResponse = deserializeJSON( getDump.fileContent ) />
<!--- Get the raw source of the bounce back email. --->
<cfset source = dumpResponse.body />
<!---
The source is a raw string; so, what we need to do now is
extract our custom header (X-Customer-ID) from the body.
--->
<cfset customHeader = reMatchNoCase(
"X-Customer-ID[^\r\n]+",
source
) />
<!---
Output the customer ID (we can think of this as a list
delimitted by the colon and space characters. Our ID will be
the last item in that list.
--->
<cfoutput>
Customer ID: #listLast( customHeader[ 1 ], ": " )#
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment