Ask Ben: Sending Emails Based On XML Contact Data In ColdFusion
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:
- Querying the XML documenting for the target nodes.
- 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@girls-who-code.com</Column2> </Row> <Row> <Column1>Sarah</Column1> <Column2>Sarah@girls-who-code.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#" <#strEmail#>....<br /> <!--- Send out email. ---> <cfmail to="""#strName#"" <#strEmail#>" from="firstname.lastname@example.org" 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@girls-who-code.com>....
Sending to "Sarah" <Sarah@girls-who-code.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:
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:
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.
Want to use code from this post? Check out the license.