DEV Community

Alex Gravely
Alex Gravely

Posted on • Updated on • Originally published at blog.antitcb.dev

ColdFusion - Display Names for CFMail

I had an interesting client request come in last week, they wanted the ability to have prettier names show up for their From: email addresses. E.G, having the from address be their company name, rather than displaying in inboxes as info@company.com. Surely, this would be a simple thing to accommodate, and ColdFusion definitely supported this out of the box, but I did run into a hiccup and hopefully I can save someone else the time I spent trying to figure this all out!

Generally when you're using the CFMail tag, you're using email addresses as is. If you're just using this tag as is, then you can follow this format to get display names to show up for the email.

<!--- Would show the email as from "Alex Gravely" in your inbox --->
<cfmail from='Alex Gravely<alex@example.com>' />

<!--- If you have special characters, you can wrap the display name in double quotes --->
<cfmail from='"Alex Jr. @ Freelance"<alex@example.com>' />
Enter fullscreen mode Exit fullscreen mode

Now here's where my pitfall was: we use a customized CFMail tag that ends up using C#'s System.Net.Mail.SmtpClient, and as part of that custom tag's validation, we use the IsValid function for validating the email format. Surprisingly enough, trying to do this display name format actually fails this function's validation, despite working properly in the CFMail tag.

If you do want validation support for this format as well, this regex pattern does the job: (?:"?([^"]*)"?\s)?(?:<?(.+@[^>]+)>?)/gi

Top comments (0)