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:
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.
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.
Thanks for the comment. I'm happy to share the solution and glad you found it useful.
Best,
Jamie
Used your example and was up and running in 2 minutes.
-C
Thanks,
Bart
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.
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
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)?
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/
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! :)
My pleasure. Glad this is still pertinent :)
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?
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?
@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!