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

Posted January 28, 2009 at 2:43 PM by Ben Nadel

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.

  • <!--- 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:

  • xmlRow.XmlChildren[ 1 ].XmlText

... we can simplify it to be:

  • xmlRow.Column1[ 1 ].XmlText

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

  • 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.



Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools