Ask Ben: Sending Emails Based On XML Contact Data In ColdFusion

Posted January 28, 2009 at 2:43 PM

Tags: ColdFusion, Ask Ben

Is it possible - and to how to do it - to use CFMAIL to send to multiple recipients using a list saved as an xml doc? I did it using CFQUERY to get a list from MS SQL but I wonder if I can do the same using XML.

XML is great. I love XML a whole lot; but, no doubt about it, when we start using XML, we start to add some complexity to our algorithms. But, once you understand the beauty of XML and the XML tools that ColdFusion provides, I think you'll learn to work with it as naturally as you do CFQuery.

When utilizing XML-persisted data, there's generally two phases involved:

  1. Querying the XML documenting for the target nodes.
  2. Extracting the relevant information from those nodes.

Because the description of XML utilities can be hard to picture and you probably haven't worked with it much, I'll post the sample code first and then we can review it afterwards.

 Launch code in new window » Download code as text file »

  • <!--- Parse XML data into a ColdFusion XML document. --->
  • <cfxml variable="xmlContacts">
  •  
  • <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  • <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  • <Row>
  • <Column1>Molly</Column1>
  • <Column2>Molly@HotBrunettes.com</Column2>
  • </Row>
  • <Row>
  • <Column1>Sarah</Column1>
  • <Column2>Sarah@CuteNCuddly.com</Column2>
  • </Row>
  • </Root>
  •  
  • </cfxml>
  •  
  •  
  • <!---
  • Query for row nodes. Because we are going to be sending out
  • emails with required information, ONLY return rows that have
  • Column1 and Column2 text() data (text node values).
  • --->
  • <cfset arrRowNodes = XmlSearch(
  • xmlContacts,
  • "//Row[ Column1/text() ][ Column2/text() ]"
  • ) />
  •  
  •  
  • <!--- Loop over row nodes. --->
  • <cfloop
  • index="xmlRow"
  • array="#arrRowNodes#">
  •  
  • <!---
  • Grab the name and email addresses. Be sure to use text()
  • method otherwise, we will get implicit xml-to-string
  • conversion which will add unwanted XML data.
  •  
  • When accessing the text nodes, we can use the psuedo
  • node named-collections provided by ColdFusion.
  • --->
  • <cfset strName = xmlRow.Column1.XmlText />
  • <cfset strEmail = xmlRow.Column2.XmlText />
  •  
  • <!--- Debugging information. --->
  • Sending to "#strName#" &lt;#strEmail#&gt;....<br />
  •  
  • <!--- Send out email. --->
  • <cfmail
  • to="""#strName#"" <#strEmail#>"
  • from="xxx@yyyyy.com"
  • subject="This is a test email."
  • type="html">
  •  
  • <p>
  • Dear #strName#.
  • </p>
  •  
  • </cfmail>
  •  
  • </cfloop>

When we run this code, the emails are sent out and we get the following debug output:

Sending to "Molly" <Molly@HotBrunettes.com>....
Sending to "Sarah" <Sarah@CuteNCuddly.com>....

For us, the first part requires us to get the Row nodes as each Row nodes represents one contact. For that, we are using the XPath:

//Row[ Column1/text() ][ Column2/text() ]

The "//Row" tells the XML search engine to find element nodes anywhere in the document whose name is "Row". The following part, with brackets, is known as the predicates or conditionals. These are requirements that must be true in order for the given node to be valid (and returned in the search). The first one requires the Row node to have a child node, Column1, that has text data within it. The second predicate does the same as the first, except for the child node, Column2.

If you know that these nodes will always be there, you can remove the predicates and simply use the XPath value:

//Row

I am only providing the additional predicates to demonstrate some of the XML power that ColdFusion provides.

The target Row element nodes are returned in an array (in which each index holds a single Row node). Once we have this array, we simply need to loop over it and send the emails. Of course, this is now part 2 of our XML data usage, in which we have to extract the text data from the collected XML nodes. To help us do this, ColdFusion provides convenience collection of nodes that can be accessed by name. So, for example, instead of getting the name with this:

 Launch code in new window » Download code as text file »

  • xmlRow.XmlChildren[ 1 ].XmlText

... we can simplify it to be:

 Launch code in new window » Download code as text file »

  • xmlRow.Column1[ 1 ].XmlText

... or, since we are using the first item in the "Column1" pseudo-collection, simply:

 Launch code in new window » Download code as text file »

  • xmlRow.Column1.XmlText

When accessing string data, we have to be careful to use the XmlText value. This is because in XML, all elements are actually nodes and trying to refer to a "text node" as string data will cause ColdFusion to implicitly convert it to a string and, in the process, add additional data that will cause errors in the CFMail tag.

If you are new to XML, this might be a lot to swallow, but hopefully it will point you in the right direction.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »