Let Postfix send mail through your Gmail Account – Snow Leopard
First of all GRRRRRR!!
Second, this has been one of those things I randomly get sucked into between projects where I'll spend 5 hours on Google trying to figure it out and getting tiny fragments of info but never actually solving the issue. This is the worst! What the hell am I talking about? Say you use MAMP or whatever as a local testing server. You write some PHP and you need to use the mail() function. You test your new email function to your personal gmail account. Ok so you try it and it doesn't work, or even worse it works a couple times and then never again. So you go to Applications->Utilities and fire up the Console application. You're shocked to see that there's a message in there saying something about Gmail not accepting mail from your IP address because it's registered as a residential thingy and apparently a lot of spammers use their personal computers to send spam.
So you say no no there must be some mistake I'm a programmer, not a spammer, I'm just trying to test out my new app. But you quickly realize you're talking to a computer, pleading, and well it doesn't care. typical. After, you cry and try piece together a coherent step by step set of instructions to route all mail sent from your computer through your Gmail account - so it would be from you, and all go through. here's what you do.
note that $ is used to show a new terminal command, you don't actually type it in:
Open Terminal - found in Applications->Utilities and type in:
$ sudo nano /etc/postfix/relay_password
You'll now be editing a new file called relay_password in the nano Terminal editor, type in the following substituting your login info - it should work with google apps accounts as well:
smtp.gmail.com example@yourdomain.com:yourpassword
Press ctrl+o on your keyboard followed by Enter to save the file, then press ctrl+x to exit the editor.
Now type in:
$ sudo postmap /etc/postfix/relay_password
That should tell Postfix to use the relay file you just created. Gmail uses a secure connection so you need to head over to Verisign and download some root certificates. Go to the following url fill out your info and download the .zip file:
https://www.verisign.com/support/roots.html.
Now type in the following commands one after another. In the second command it wants the roots.zip you just downloaded, you can just drag the zip file onto the Terminal window and it will fill in it's location, don't do that for the 4th command though. Also note you may have some certificates already on your system, so after the second last command you may be prompted to replace existing certificates, type N so it doesn't replace the ones you have:
$ sudo mkdir /etc/postfix/certs
$ sudo cp roots.zip /etc/postfix/certs
$ cd /etc/postfix/certs/
$ sudo unzip -j roots.zip
$ sudo openssl x509 -inform der -in thawte\ Primary\ Root\ CA\ -\ G2_ECC.cer -out thawte\ Primary\ Root\ CA\ -\ G2_ECC.pem
$ sudo c_rehash /etc/postfix/certs
Now type in:
$ sudo nano /etc/postfix/main.cf
Go to the end of the document, you can delete the MAMP stuff, also note that if you have MAMP Pro and you edit postfix settings from there it'll fuck up what we're doing here. So remember this going into the future and don't do that.
Paste in the following at the end of the document - note: use the keyboard to get around the document, but use the mouse to right click and paste:
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
Save it like we did before by pressing ctrl+o, then Enter, then ctrl+x.
Now type in:
$ sudo nano /etc/postfix/master.cf
You're now editing master.cf, this is a different file to main.cf we just pasted stuff into. There should be a table in here, find the line in the table that looks something like this:
#tlsmgr fifo - - n - 1 tlsmgr
Make it look like this - note the comment is removed and fifo should be unix:
tlsmgr unix - - n - 1 tlsmgr
Save it like the other times pressing ctrl+o, then Enter, then ctrl+x.
Ok, so at this point you can put the following into terminal and see that it works - put your email address in there twice:
printf "Subject: blah" | sendmail -f user@gmail.com user@gmail.com
Postfix is working now. good. You go back to your PHP application and test the mail() function again. If it works then you're done, but if not you panic. You start feeling really hungry. You know that Postfix is working but maybe PHP or Apache haven't gotten the message yet. ok. So you see in Console that sendmail is crashing, you open the crash report in Terminal and it tells you there's an incompatible version of libxml. It wants 10 and you have 9. You begin questioning if any of this is worth it and maybe you should just go sit in front of a tv and forget about doing anything meaningful with the rest of your life.
After almost installing XCode and registering as an apple developer so you can make and install the newest version of libxml, you wonder if maybe MAMP comes with libxml and find that yes it does. So instead of spending 2 hours upgrading the system libxml only to find it doesn't do anything you just upgrade to the latest version of MAMP (1.8 at the time of writing) and it works. What?? it works? really? yup. so what do you do now?
REJOICE! with lunch.
Some of this was scoured from random forums and blogs in the midst of complete frustration and combined into steps that actually work. A chunk however was taken from this post: http://dejan.ranisavljevic.com/2009/05/28/enable-postfix-with-relay-outbound-to-your-gmail-account-on-os-x-leopard/ so check them out.
Tags: console, email, libxml, localhost, lunch, mail(), mamp, php, postfix, terminal, verisign



March 12th, 2010 at 6:12 pm
[...] 참고글: http://fightskillz.com/2009/10/let-postfix-send-mail-through-your-gmail-account-snow-leopard/ [...]
April 18th, 2010 at 9:12 am
Simply amazing – thank you!
May 17th, 2010 at 11:48 pm
Thanks a bunch. This all worked great.
May 22nd, 2010 at 9:29 pm
You spent HOW LONG on this? I just did it following your advice and I was ready to give up…It does work, though, and it works damn well. Thanks for the writeup.
May 23rd, 2010 at 2:09 am
Glad it worked! It was 40 long decades. BUT surprisingly most of that time was spent backpacking through Mussoorie trying to find the inner part of me that made solving this postfix problem so confounding.. To tactfully request of it to reveal the solution to me.
June 23rd, 2010 at 9:45 am
Works like charm. Thanks!
June 23rd, 2010 at 5:24 pm
Nice tutorial. It looks like it is almost working, I’m having a hangup and I believe it is a DNS problem. I’ve tried Googling for info and haven’t had much luck in the things I’ve tried, figured I’d see if you had some advice.
I try the mail command and it never shows up, so I check the log and see this:
Jun 23 10:13:42 MY-MAC-NAME postfix/smtp[638]: 097004114A1: to=, relay=none, delay=30, delays=0.03/0.03/30/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=smtp.gmail.com type=MX: Host not found, try again)
However I can telnet to smtp.gmail.com, port 587 just fine.
Is postfix off in it’s own little world with DNS, and is there a way to set it right?
June 23rd, 2010 at 5:33 pm
Update: I can get it to work with a hard coded IP address, which I don’t like so much, but at least it is working. Thanks for the great tutorial and the embellishments to make it work. Any thoughts on getting the DNS to work right are greatly appreciated.
June 23rd, 2010 at 5:41 pm
Hey Dano! glad it’s working. I’m thinking there might be something funky in your hosts file. DNS issues can be a huge pain since it’s cached at just about every level – browser, os, router, isp, etc..
If your hosts file is fine, you might want to try switching your DNS provider to Google’s public DNS service and see if that helps. If anyone has a vested interest in gmail’s IP resolving it’s Google, at the very least it’ll flush out or bypass the various DNS caches.
instructions here: http://code.google.com/speed/public-dns/
June 23rd, 2010 at 7:32 pm
That did the trick, thanks! Wa-hoo!
June 23rd, 2010 at 10:28 pm
It might also be this
http://www.renemoser.net/2009/09/postfix-host-not-found-try-again/
August 19th, 2010 at 8:38 am
how in the hell did you ever figure this out? what a pain in the ass, thank you!!!
September 2nd, 2010 at 10:13 pm
wow! thanks!