cf.Objective() 2010: The Only Enterprise ColdFusion Conference
cf.Objective() 2010 Pre-Conference Sessions: Secure CFML, ColdBox, ORM, Flex, Mach-II, Model-Glue

cfmail using Gmail SMTP

I wanted to have a cfmail tag use my Gmail SMTP to send mail, which was relatively straightforward, but there is some vague language in the Google help doc that didn't immediately translate to cfmail attribute settings. I did find plenty of search results on the topic, but most threads were a little confusing and noted differing experiences between developers. So I've taken a little time to experiment and thought I'd share the results.

From both my gmail.com account and my Google Apps for Your Domain mail (gmail for custom domain name) accounts I was able to find the same help doc for standard POP/SMTP configuration.

We can quickly deduce that the username attribute should be your full e-mail address (including @gmail.com or @your_domain.com), and the password is of course your Gmail password.

The help doc provides the following information for the outgoing/SMTP server configuration:

smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587

So, how does the above information translate to relevant cfmail tag attributes (server, port, username, password, useSSL, useTLS)? It's a bit more specific than the Gmail help doc suggests...

Well, the server attribute is easy, that should be: smtp.gmail.com

"Use Authentication: Yes" suggests we need to set either useSSL or useTLS to true, right? It also looks like we can set the port attribute to either 465 or 587, right? Well, these inferences are both half correct, but there is a dependency between the three attributes. By the way, I did notice the mention of "requires TLS" in the Gmail help doc, but the "some clients call this SSL" confuses the issue a bit. In any case, I've performed some thorough testing and learned that port 465 will work with SSL only enabled or with both SSL and TLS enabled; port 587 will work with TLS only enabled. Here are examples of all three variations that worked in my testing:

<cfset mailAttributes = {
server="smtp.gmail.com",
username="el.barto@gmail.com",
password="eatmyshorts",
from="el.barto@gmail.com",
to="homer@thesimpsons.com",
subject="Eat my shorts, man!"
}
/
>


<cfmail port="465" useSSL="true"
attributeCollection="#mailAttributes#"
>
port 465, SSL enabled</cfmail>

<cfmail port="587" useTLS="true"
attributeCollection="#mailAttributes#"
>
port 587, TLS enabled</cfmail>

<cfmail port="465" useSSL="true" useTLS="true"
attributeCollection="#mailAttributes#"
>
port 465, SSL and TLS enabled</cfmail>

So, which one should I use? I can only claim to know as much about SSL vs. TLS as I recently read on Wikipedia (http://en.wikipedia.org/wiki/Secure_Sockets_Layer). Since TLS appears to be the successor to SSL, I guess I'll go with that for now. I'm not sure exactly what happens when both useSSL and useTLS are true, but I'm guessing that one of them is being ignored? Is it possible to implement both in one request? I'd love to hear feedback if anyone has more knowledge/experience or a reason for a strong preference here.

For what it's worth, I also confirmed that you can leave off "@gmail.com" in the username attribute, but you must include the domain name for custom domains (Apps for Your Domain accounts).

Finally, I should also note that the useSSL and useTLS attributes were introduced to cfmail in ColdFusion 8. I don't believe it's possible to use Gmail SMTP for cfmail in ColdFusion MX7 or earlier.

UPDATE (2009-05-07): I have also tested this with the Railo 3.1.0.012 CFML engine, and all three examples above work well with Railo. When configuring the mail server settings in the Railo Server/Web Admin, I was only able to get a successful verification when using TLS and port 587 -- when I tried SSL and port 465, it just hung (probably would have timed out trying to verify, but I waited a good minute before giving up).

UPDATE (2009-10-02): It's important to note that there are daily sending limits when using Gmail SMTP, as noted in this Gmail Help page and in this Google Apps forum answer. It's also important to keep in mind all points outlined in Google's Bulk Senders Guidelines.

Comments
Kris's Gravatar Thank you Jamie,

This is the solution I've been looking for. We actually made a "mission" to beat the spam assassin - and no matter what we tried (an worked for others) we could not get the results.

With this method and a couple quick setups with Google Apps, we had it up and running in hours. All three email types worked for me.. so I'm going with the SSL+TLS until I learn more about it.

Awesome code, works perfectly! Thanks again.
# Posted By Kris | 3/24/09 5:32 PM
Jamie Krug's Gravatar @Kris,

Thanks for the comment. I'm happy to share the solution and glad you found it useful.

Best,
Jamie
# Posted By Jamie Krug | 3/24/09 8:00 PM
Jason McKinney's Gravatar Thanks Jamie,

Used your example and was up and running in 2 minutes.
# Posted By Jason McKinney | 4/30/09 4:17 PM
malhotra's Gravatar thanks man..
# Posted By malhotra | 5/7/09 12:45 PM
greenlight247's Gravatar Nice! I was pulling my hair out trying to get this work.
# Posted By greenlight247 | 6/24/09 6:59 PM
Matt Quackenbush's Gravatar @Jamie - Thanks for the concise - and accurate - post! It took me literally about 30 seconds to get this setup after reading your post. :-)
# Posted By Matt Quackenbush | 8/4/09 3:18 PM
Olivier Bridgeman's Gravatar Thanks Jamie! This really helped with me a lot :D
# Posted By Olivier Bridgeman | 8/14/09 8:42 PM
Clint Goyette's Gravatar Thanks Jamie. Great stuff.

-C
# Posted By Clint Goyette | 8/22/09 8:56 PM
Brad's Gravatar Thanks James, Great job! I've been looking for this all over the net. Keep up the great work!
# Posted By Brad | 9/3/09 1:36 AM
Adam Tuttle's Gravatar I'm working on something that will be sending email to approximately 300 people, currently. Based on the "sending limits" link, I assume that it's currently acceptable to have a message sent with a single TO address and 300 BCCs; but that 500+ BCCs and the message will be denied. Does that mean that instead I should loop and send 300 unique emails directly TO the recipient? I don't see anything akin to rate-limiting, e.g. only 200 messages per hour can be sent. Do you have any more information on this particular dynamic of SMTPing via Gmail?
# Posted By Adam Tuttle | 1/8/10 10:11 AM
Adam Tuttle's Gravatar Nevermind, the forum answer link answered my question. Great post, thanks for the info.
# Posted By Adam Tuttle | 1/8/10 10:50 AM
Jamie Krug's Gravatar Hi Adam, thanks for the comments. Glad you found what you were looking for. Do you have any details/links to share? I'm curious about the finer details of Google Mail SMTP limits.
# Posted By Jamie Krug | 1/8/10 11:31 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002. Contact Blog Owner