Sending properly formatted multi-part email messages that obey the RFC standards for email.
Muti-part messages allows the end users mail client to display the message in the method that best works for the mail client. Multi-part can mean having two more more parts such as text and HTML or text and file attachment or text/html and file attachment. Also, many SPAM filters will flag or even completely block HTML only messages at SPAM. The solution is simple in ColdFusion. three tags, CFMAIL, CFMAILPARAM and CFMAILPART. Included in this recipe is a little function to simple convert HTML into text while preserving basic formatting such as line breaks.
Build your HTML formatted email and use CFSAVECONTENT to store the email body as a ColdFusion variable. I prefer this method in most cases because I only have to generate my email message once. In cases where you have very complex HTML formatting such as table columns you may opt to manually create separate text and HTML messages. One key thing to remember is that BOTH the CFMAIL and CFSAVECONTENT tags treat everything inside them literally. This means that all spacing, tabs, lines breaks ColdFusion Generated whitespace is saved as well and can alter your formatting of the email message.
<cfsavecontent variable= "mailmessage">
Dear Joe Doe, <br>
<br>
Thank you for registering with our site. <br>
<br>
You can log into your Profile at any time by pointing your web browser to: <br>
<a href= " http://www.mysite.com/profile/"> http://www.mysite.com/profile/ </a> <br>
<br>
Your User name is: <cfoutput>#username# </cfoutput> <br>
<br>
When you registered you indicated you wanted to receive these newsletters by email: <br>
<cfoutput query= "subscriptions">
<ul>
<li>#newsletter_name# <br> </li>
</ul>
</cfoutput>
<br>
Thank you for using our fine site, <br>
<br>
Mysite.com Management <br>
<br>
<cfoutput> <a href= " http://www.mysite.com/profile/unsubscribe/#username#">Click here to unsubscribe from future emails </a> </cfoutput>
</cfsavecontent>
<cffunction name= "textMessage" access= "public" returntype= "string" hint= "Converts an html email message into a nicely formatted with line breaks plain text message">
<cfargument name= "string" required= "true" type= "string">
<cfscript>
var pattern = " <br>";
var CRLF = chr( 13) & chr( 10);
var message = ReplaceNoCase(arguments.string, pattern, CRLF , "ALL");
pattern = "<[^>]*>";
</cfscript>
<cfreturn REReplaceNoCase(message, pattern, "" , "ALL")>
</cffunction>
<cfmail to= "#form.mailto#" from= "#form.mailFrom#" subject= "#form.subject#" type= "html">
<cfmailpart type= "text/plain" charset= "utf-8">#textmessage(mailmessage)# </cfmailpart>
<cfmailpart type= "text/html" charset= "utf-8">#mailmessage# </cfmailpart>
</cfmail>
<cfmail to= "#form.mailto#" from= "#form.mailFrom#" subject= "#form.subject#" type= "html">
<cfmailparam file= "/document/path/mypdf.pdf" contentID= "#createUUID()#" disposition= "attachment" type= "application/PDF">
<cfmailpart type= "text/plain" charset= "utf-8">#textmessage(mailmessage)# </cfmailpart>
<cfmailpart type= "text/html" charset= "utf-8">#mailmessage# </cfmailpart>
</cfmail>
+