Avg. Rating 4.5
Tags:



Problem

Sending properly formatted multi-part email messages that obey the RFC standards for email.

Solution

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.

Detailed explanation

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>
 
 
 
  
 
Next, make this function available to the email code. You can place in in the same CFML page or put it in a utility type  CFC that your application can access. This code strips HTML elements while preserving line breaks.
 
        <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>
 
 
 
  
 
Now use the CFMAIL tag to generate the email. Nested in the CFMAIL you will use CFMAILPART. CFMAILPART generates the email parts according the the RFC standards so that any email server and email client that follows the RFC standards can process and render your ColdFusion generated emails. You will use CFMAILPART for each part of the meail you create, in this case two parts text and HTML.
 
        <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>
 
 
 
  
 
To add a file attachment to your email message you need to use the CFMAILPARAM tag and nest that inside the CFMAIL tag. Your ColdFusion server needs to have physical file access to the attachment.  To add more than one file attachment simply include another CFMAILPARAM tag nested in the CFMAIL tags and make sure the file you want attached is available to ColdFusion server.
 
                <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>
   
   
   
    
 
For a compelte explanation visit my ColdFusion blog Trunkful.com.

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes