Last active
March 21, 2020 11:13
-
-
Save bennadel/9753371 to your computer and use it in GitHub Desktop.
Ask Ben: Sending Emails Based On XML Contact Data In ColdFusion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xmlRow.XmlChildren[ 1 ].XmlText |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xmlRow.Column1[ 1 ].XmlText |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xmlRow.Column1.XmlText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment