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  |  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
Mar 22, 2010 at 7:43 AM
Terms Of Service / Privacy Policy Document Generator
Thankyou for this very helpful form. You've made my life much easier today. I'll have a look around your site... I'm sure there's some more good stuff here..Thanks Dave ... read »
Mar 22, 2010 at 7:21 AM
Encountered "(. Incorrect Select Statement, Expecting a 'FROM', But Encountered '(' Instead, A Select Statement Should Have a 'FROM' Construct.
I got this exception now. In case you're using var-es local struct, CF gives you couple of "new" exceptions: Encountered "local. and Encountered "id. Incorrect Select List, Incorrect select colum ... read »
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »