You are here: Home > Articles > Article Display

Working with cfpop email headers

Using the cfpop tag to retrieve emails returns the headers as one long text string, which makes it difficult to get specific values. In this article we'll see first how to get the headers, and then how to work with them to get the name-value pair we want.

Published: Mar 31, 2005
Tested with: Coldfusion 6.1
Category: ColdFusion
10,567 views

Introduction

It's actually very easy to retrieve POP emails using the cfpop tag as we can see from the tag's reference on Macromedia. This tag is capable of returning all the headers that were sent along with the email. However, the headers are usually all cramped together in one long string. We might run into a situation where we want to get specific name-value pairs, like for example the value of Reply-To, and so we'll see how to do that.

Getting the email headers

We can do this in two ways:

  1. Using the GetAll option in the tag
  2. Using the GetHeaderOnly option (default)

Let's work with the GetAll option. Calling the tag should look something like this:

1 <cfpop server="mail.youremailserver.com" username="username@youremailserver.com" password="yourpassword" action="GetAll" name="qMail">

What we get back is a query whose name we have assigned as qMail. Using the GetAll option in this tag actually requires that you assign a name to the returned query, just like we have done. The headers are stored inside the returned query in a variable called simply headers. So let's loop through all the returned email headers:

1 <cfoutput>
2     <cfloop query="qMail">
3         #qMail.header#
4         <hr>
5     </cfloop>
6 </cfoutput>

And the results from the browser:

Email headers through the browser

As you can see, each email header looks like a long string and it's not that easy to get a specific value.

Searching for a specific name in the header

Each name-value pair is actually separated by a line break. Knowing this, we can create a function that will search for a specific name inside this string and get its value.

1 <cffunction name="GetHeaderValue" access="public" output="false" returntype="string">
2     <cfargument name="aHeader" required="yes" type="string">
3     <cfargument name="aHeaderName" required="yes" type="string">
4     
5     <cfset var arrHeaderValues = ArrayNew(1)>
6     <cfset var cHeaderValue = "">
7     
8     <cfset arrHeaderValues = ListToArray(arguments.aHeader, chr(10))>
9     <cfloop from="1" to="#ArrayLen(arrHeaderValues)#" index="value">
10         <cfif Left(arrHeaderValues[value], Len(arguments.aHeaderName)) IS arguments.aHeaderName>
11             <cfset cHeaderValue = Mid(arrHeaderValues[value], Len(arguments.aHeaderName) + 3, Len(arrHeaderValues[value]))>
12             <cfbreak>
13         </cfif>
14     </cfloop>
15     
16     <cfreturn cHeaderValue>
17 </cffunction>

The function accepts two arguments: the whole header text and the name of the header that we want to get (e.g. From, To, etc.). When passing in the name of header, lettercase does not really matter. First it creates an array of elements based on the line breaks it finds (i.e. chr(10)). Then it loops through this array and tries to find the name that you are looking for by matching the beginning of each array element to the actual name. Once it finds it, it simply gets its value, breaks out of the loop and returns it.

So, for example to get the value of Content-Type we can call this function like so:

1 <cfoutput>
2     <cfloop query="qMail">
3          #GetHeaderValue(qMail.header, "Content-Type")#
4         <hr>
5     </cfloop>
6 </cfoutput>

Running this code now will display the content type of each email:

Email header broken up

Conclusion

We have seen how to retrieve email headers using the cfpop tag and then how to break them up to retrieve specific values. This is a relatively simple solution and there are some potential issues with this that I will leave up to the reader to pick up from here. :-) For example:

  1. Some emails could have a specific header declared more than once inside the header. My function will return the first occurence.
  2. Some header values may span over several lines, e.g. Received. My function will return the first line only.

 



Other articles in this category
  1. A ColdFusion weather custom tag
    September 15, 2005
    This custom tag will allow you to easily add a weather control to your website. Using the XML data feeds from weather.com we can produce owesome looking weather controls, both for current conditions and for forecasts. This control has a multitude of attributes, is fully customizable, easy to use, comes with example templates, and full source code provided.
  2. A ColdFusion color picker custom tag
    June 27, 2005
    We'll learn how to create and use a ColdFusion custom tag, that will add a color picker field to a form and make it easy to select a web safe color. No popups were harmed during the making of this article!
  3. Firefox download counter in ColdFusion
    June 21, 2005
    We'll see how to create and use a ColdFusion custom tag that will make it easy for us to display the Firefox download counter.