As we saw in Example 7.1: Creating a Web-Based Email Form, <CFMAIL> will send its message body as plain text by default. However, there might be times when you want to send email in HTML format. Perhaps your company likes to email all employees a monthly newsletter, for example, or email all customers a regular product specials page outlining the latest sale items. You could email these things as plain text, but sending them in HTML format gives them a bit more punch and presents a slicker look and feel. We can send HTML email using the <CFMAIL> tag simply by including the TYPE attribute in our opening <CFMAIL> tag and setting its value to "HTML". With the TYPE attribute set to "HTML", you simply add HTML tags to the message body between the opening and closing <CFMAIL> tags. The HTML you use should be well structured, so do not forget to include the <HTML> and <BODY> tags within the email message body. The following code illustrates the use of <CFMAIL> to send HTML email: [View full width] <CFMAIL TO="customers@anysite.com" FROM="sales@beelze-bubba.com" SUBJECT="This Month's Special" TYPE="HTML"> <HTML> <BODY> <TABLE BORDER="0" WIDTH="400"> <TR ALIGN="center" BGCOLOR="##990033"> <TD COLSPAN="2"> <FONT FACE="Arial" COLOR="##FFCC00"><B>Check Out ThisMonth's Special</B></FONT> </TD> </TR> <TR> <TD> <IMG src="/books/4/16/1/html/2/http://localhost/NewSite/images/products_sml/gf002.jpg" ALT="" WIDTH="100" HEIGHT="100" BORDER="0"> </TD> <TD> <FONT COLOR="##990033"><B>Beelze-Bubba Holiday Gift Pack</B></FONT><BR> Chock full of the good stuff, it is a gift any 'heat lover' is bound to enjoy. The gift pack comes in three strengths;<BR> HOT<BR> REALLY HOT and<BR> MOTHER-IN-LAW!!! (outlawed in 3 states) This Month's Special Price: <B>$ 19.99</B> </TD> </TR> <TR ALIGN="center"> <TD COLSPAN="2"> <A HREF="http://localhost/NewSite/Index.cfm"><FONT SIZE="+1"><B>Visit Our Website</B></FONT></A> </TD> </TR> </TABLE> </BODY> </HTML> </CFMAIL> NOTE Because the <CFMAIL> tag operates in much the same fashion as the <CFOUTPUT> tag, don't forget to escape the # symbol in any hexadecimal color codes. You can see that we have used the TYPE attribute and set its value to "HTML". This tells ColdFusion Server to treat everything between the opening and closing <CFMAIL> tags as HTML (which means that now all line returns and extra spaces will be ignored). Also note that we are not using a SERVER attribute, which means that this mail message will be delivered using the SMTP server settings set up in ColdFusion Administrator. When a user with an HTML-enabled email client receives this message, he will see the resulting HTML output as shown in Figure 7.5. Figure 7.5. The HTML email message. Well, that seemed simple enough, didn't it? It is, but there are a few pitfalls you need to be aware of if you plan on using HTML email. First, not all email clients support messages formatted with HTML. As a matter of fact, in many programs this HTML message support is a user-configurable option, which means that even if the program supports HTML, the user might have disabled this function. If someone receives HTML email and his email client does not support it, he usually will be presented with plain text, HTML tags and all, as shown in Figure 7.6. Figure 7.6. A plain text view of HTML email. The second thing you need to take into consideration when using HTML email is that all links and file references need to be absolute references. For example, if you want to include an image in the email, you must use the full URL for that image; otherwise, it will not appear in the resulting output. Finally, what if the message recipient downloads her email and then disconnects from the Internet? When she opens your HTML email, because she is no longer connected to the Internet, none of the images will display and none of the links will function correctly. The moral of the story is that HTML email can certainly add some zip to your email, but certain aspects might not actually get to the recipients in the format you intended. Consequently, HTML email is probably best used sparingly and in situations in which you are reasonably sure it will reach the recipient in the form it was intended (such as intranet applications). Another option is this: When users sign up for things like newsletters, ask them in which format they would prefer to receive email. Additionally, you could send your users a plain text email that contains a link to a web page. |