Relay outbound SMTP email to Gmail 12
Posted by Matt Sears on Friday August, 01 2008
Sending emails with Rails via Gmail is a snap with Marc Chung's excellent plugin action_mailer_tls. However, sometimes our production environment isn't using Gmail as a mail server and/or we just need an easy way to send email from our development environment for testing or demonstrating purposes.
Instead of installing the action_mailer_tls plugin and configuring each of our Rails apps, we can do a one-time setup of our local Postfix client to relay all SMTP outbound emails to our Gmail account. If your running a Mac OS Leopard or Linux, Postfix should already be installed. With a little configuration, we should be up and running in a couple minutes.
First create /etc/postfix/relay_password file with the server name, email account name and password as shown below. This configuration works with Gmail accounts as well as with Google Apps email accounts. I'm personally using my company's Google Apps with a special email account setup for outbound emails only.
smtp.gmail.com example@yourdomain.com:yourpassword
Then tell Postfix about our google accounts information so it knows how and where to relay the email to. This can be done with the postmap command:
$ postmap /etc/postfix/relay_password
Since Gmail requires a TLS (Transport Layer Security) connection for certificate-based authentication, we'll need to download a free root certificate from Verisign https://www.verisign.com/support/roots.html to authenticate our remote SMTP client.
$ mkdir /etc/postfix/certs
$ cd /etc/postfix/certs
$ sudo cp roots.zip /etc/postfix/certs
$ sudo unzip -j roots.zip
$ sudo openssl x509 -inform der -in ThawtePremiumServerCA.cer -out ThawtePremiumServerCA.pem
$ sudo c_rehash /etc/postfix/certs
Now we are ready to configure Postfix. Postfix needs to know what host to relay the email to, the username and password to authenticate the Gmail account, and the path to our certificates for the encrypted session. Add these lines to the bottom of /etc/postfix/main.cf
relayhost = smtp.gmail.com:587
# auth
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/relay_password
smtp_sasl_security_options = noanonymous
# tls
smtp_tls_security_level = may
smtp_tls_CApath = /etc/postfix/certs
smtp_tls_session_cache_database = btree:/etc/postfix/smtp_scache
smtp_tls_session_cache_timeout = 3600s
smtp_tls_loglevel = 1
tls_random_source = dev:/dev/urandom
Restart (or start) Postfix to pick up our new changes.
$ sudo postfix stop
$ sudo postfix start
That's it! Now we don't have to do any special installation or configuration to send email via Gmail for our Rails apps. We just need to set the delivery method to :smtp and we're ready to go.
Comments
nap Saturday September, 06 2008
Nice writeup! Although I believe (not 100% positive) that the smtp server address in relay_password would have to be written as [smtp.gmail.com]:587 for this to work...
clark Hulls Tuesday September, 09 2008
Hi, how can i really use gmail to run smtp and please can u help with list of email account that has smtp sever relay for free with out limit and how to set it up. Regards Clark
Matt Sears Saturday October, 11 2008
@nap - thanks for the feedback. I believe the relay_password setting could be written both ways (might depend on postfix version - not sure). I currently have it set without the brackets [] and it works, but have not tried it the other way.
Sarah Baker Tuesday December, 09 2008
When I set this up, it works just fine and I notice all the mail relayed thru had the From Address of the Google user I setup, not the original sender set in my application. Have I configured something wrong, or is this expected? Note on the brackets. The postfix.org site says... "specify a domain name, hostname, hostname:port, [hostname]:port, [hostaddress] or [hostaddress]:port. The form [hostname] turns off MX lookups."
Adam Tuesday December, 30 2008
Thanks, this worked for me.
Cecil Porter Wednesday January, 28 2009
This sounds good, but once I set this up my log file said: Jan 27 23:08:41 tooter postfix/smtp[21038]: 197352AD26A: to=, relay=gmail-smtp-msa.l.google.com[74.125.45.109], delay=3422789, status=bounced (host gmail-smtp-msa.l.google.com[74.125.45.109] said: 530 5.7.0 Must issue a STARTTLS command first. 4sm1741339yxq.26 (in reply to MAIL FROM command))
Sureshkumar Monday February, 16 2009
HI, Thank's for posted this configure. i was working nice but am facing problem in the /etc/postfix/relay_password file .... i need add near 100 users in the /etc/postfix/relay_password how can add it ......... please help me!!!!!!!!! Already i was tried for example : i was added 3 users in that file(/etc/postfix/relay_password). then restart the postfix service. then login as a sureshkumar@multivistaglobal.com in the local linux box i have send email to external user but in the mail status = send. but it not shown my mail id. it will customercare@multivistaglobal.com here i have pased /et
Steve Tuesday March, 31 2009
Anyone getting this error? One day I went to start postfix, and wham! postfix: fatal: config variable inet_interfaces: host not found: localhost
Danesh Monday May, 18 2009
Hey Matt, As of 18-May-2009, the link to action_mailer_tls (toward the beginning of your article) is broken. HTH
brian Never
i know this is a dumb newbie question but how do i make this file: First create /etc/postfix/relay_password file
brian Never
ok got past that hurdle, after i DL the certificates and try to do the cp line i get this brian-sabans-macbook-2:certs brian33433$ sudo cp roots.zip /etc/postfix/certs cp: roots.zip: No such file or directory
Monday December, 07 2009