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 (Comment Moderation is enabled. Your comment will not appear until approved.)
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
elbarto's Gravatar Hi! Could you do me a favor and change your example email address? Mine is "elbarto", and gmail ignores dots, so you're essentially broadcasting my email for all sorts of spam bots to pick up. :)

Thanks,
Bart
# Posted By elbarto | 4/3/10 8:48 AM
changwei  gao's Gravatar Thank you. It's useful to me
# Posted By changwei gao | 1/17/11 2:30 AM
Brandon's Gravatar Just what I needed :) I was missing the "Port" attribute in my cfmail tag. Thanks for posting this!!
# Posted By Brandon | 3/23/11 6:22 PM
Phillip Senn's Gravatar Is there a way to interact with the labels that are in GMail via ColdFusion? Maybe have an AIR application work with the archive flag as well?
# Posted By Phillip Senn | 4/4/11 10:39 AM
Jamie Krug's Gravatar @Phillip,

I've not done anything concerning interacting with Gmail labels from ColdFusion. Gmail does have good IMAP integration, including labels, so you might have a look at getting at Gmail via cfimap.
# Posted By Jamie Krug | 4/4/11 1:29 PM
Nils Thingvall's Gravatar Thanks! This helped a lot.
# Posted By Nils Thingvall | 1/5/12 4:41 PM
lharby's Gravatar Hi there

I am trying to configure this, I have been told that gmail smtp can be buggy.

Everything is being passed through to my confirm.cfm page but no emails are being sent through, is there a way to test this, get confirmation messages etc.

I've tried SSL and TLS
# Posted By lharby | 4/25/12 9:28 AM
Jamie Krug's Gravatar @lharby,

Gmail SMTP has never been buggy for me, but there is a daily sending limit. As for testing, did you at least confirm that your CFML engine sent the messages, by checking in the spool and undelivered folders (location varies by engine and install type)?
# Posted By Jamie Krug | 4/25/12 10:01 AM
Matt's Gravatar I have added all the gmail information into railo server administrator and it verifys ok. However mail is not being sent, does this mean I need to go through all my cfmail tags and add the smtp information? I hope not :-(
# Posted By Matt | 8/30/12 10:13 AM
Matt's Gravatar I tried SSL: NO (unticked) and this fixed my problem in railo server
# Posted By Matt | 8/30/12 10:22 AM
Jamie Krug's Gravatar @Matt: You should never need to place SMTP info in a cfmail tag if you can put the same information in the CFML engine's admin. I'm not sure why you had to use non-SSL, but note that you need to match the proper port with SSL or TLS, whichever you choose.

I'm not using Gmail accounts for SMTP much any more, due to sending limits. I'd suggest checking out Amazon's Simple Email Service (Amazon SES):
http://aws.amazon.com/ses/

Or, maybe something like http://postmarkapp.com/
# Posted By Jamie Krug | 8/30/12 11:53 AM
Carl Von Stetten's Gravatar @Jamie,
Thanks for this post. I just got CF10's cfmail working with GMail for a presentation I'm doing this week. It would have taken me hours if not for the information you provided! :)
# Posted By Carl Von Stetten | 9/24/12 11:20 AM
Jamie Krug's Gravatar @Carl,

My pleasure. Glad this is still pertinent :)
# Posted By Jamie Krug | 9/24/12 2:44 PM
Brien Malone's Gravatar Hey Jamie -- I stumbled across this blog trying to troubleshoot getting a CF MX7 server to work with Amazon SES. I'm not using TLS or SSL, so port 25. CF verifies the server connection, but the mail log is giving me an "Authentication Failed" error for every send. I've put the username and password in the mail server spot (user:pass@longamazonurl)... Do you have any ideas? Does MX7 has an issue with the long passwords and non-alpha characters that Amazon uses in its SES SMTP Username and Password? Sometimes a + or a / appear in the generated name. I've tried escaping them but it didn't help.
# Posted By Brien Malone | 3/13/13 10:10 PM
Jamie Krug's Gravatar @Brien,

Sorry, I haven't used CF 7 in ages, so I'm not sure if there are any specific issues with cfmail on that version. I also haven't tried out Amazon SES for SMTP yet. You might try using the `username` and `password` attributes instead of putting them in the `server` attribute's value?
# Posted By Jamie Krug | 3/14/13 10:11 AM
Brien Malone's Gravatar Yeah - I'm stuck in the dark ages over here LOL. I had the same thought re:attributes-- Same problem. Amazon tech support has been stumped, too. Guess I get to keep digging :) Thanks!
# Posted By Brien Malone | 3/14/13 11:51 AM
cmccowan's Gravatar I'm getting frustrated with missed emails in smartermail. I'm convinced there is nothing that can be done to stop this.

Considering using Google apps.. my coldfusion service sends out thousands of emails per day.. can anyone comment on deliverability.. I assume the google apps setup doesnt allow rdns and the ip wont match the http server so thats two strikes against you - am I wrong about this?
# Posted By cmccowan | 10/13/13 3:36 PM
Carol Chandler's Gravatar I've been searching for a way to use CF (Railo) to retrieve gmail, and all of the searches seem to eventually lead to this post, or else to ACF version of cfimap which is different. So I decided to just ask! Can this be done in Railo? Thanks!
# Posted By Carol Chandler | 11/11/13 3:17 PM
Jamie Krug's Gravatar @cmccowan: Getting the IP to match your server(s) is just a matter of configuring SPF in a DNS TXT record. Do keep in mind that Google Accounts limit the amount of outgoing mail sent per day (see earlier comments, and Google around for that).

@Carol: You can use CFML to retrieve mail from a Gmail account. Your options are POP or IMAP. I haven't used cfpop or cfimap in ages, so I'm not sure where the Railo support/compatibility is at. I know Railo had implemented cfimap before ACF ever did, so it may still have a slightly different implementation, but I'm sure there are some tips out there, and also forums to seek further help.

Hope that helps!
# Posted By Jamie Krug | 11/12/13 7:34 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002. Contact Blog Owner