Skip to content

Instantly share code, notes, and snippets.

@bennadel
Last active March 21, 2020 11:13
Show Gist options
  • Save bennadel/9753371 to your computer and use it in GitHub Desktop.
Save bennadel/9753371 to your computer and use it in GitHub Desktop.
Ask Ben: Sending Emails Based On XML Contact Data In ColdFusion
<!--- 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="xxx@yyyyy.com"
subject="This is a test email."
type="html">
<p>
Dear #strName#.
</p>
</cfmail>
</cfloop>
xmlRow.XmlChildren[ 1 ].XmlText
xmlRow.Column1[ 1 ].XmlText
xmlRow.Column1.XmlText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment