How to install postfix to relay mail through gmail in Ubuntu
Recommended Ubuntu book
Once I got my svnmanager working on Ubuntu, I still had problems sending mail. I pointed the domain mx record to the right direction, but for some reason my port 25 was refusing connections. So I decided to relay the mail through my gmail account.
First, install postfix if you haven’t already:
sudo apt-get install postfix
Now, to set up postfix, make sure that you have these files:
/etc/postfix/main.cf
/etc/postfix/generic
/etc/postfix/generic.db
/etc/postfix/sasl/passwd
/etc/postfix/sasl/passwd.db
which you can create via touch command like:
cd /etc/postfix
sudo touch generic
Next, edit /etc/postfix/main.cf (via command: sudo gedit /etc/postfix/main.cf), here is mine
# See /usr/share/postfix/main.cf.dist for a commented, more complete version # Debian specific: Specifying a file name will cause the first # line of that file to be used as the name. The Debian default # is /etc/mailname. #myorigin = /etc/mailname smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no # appending .domain is the MUA's job. append_dot_mydomain = no # Uncomment the next line to generate "delayed mail" warnings #delay_warning_time = 4h #readme_directory = no # TLS parameters smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key smtpd_use_tls=yes smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for # information on enabling SSL in the smtp client. myhostname = localhost alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases #myorigin = /etc/mailname mydestination = my-ubuntu, localhost.localdomain, localhost relayhost = [smtp.gmail.com]:587 mynetworks = 127.0.0.0/8 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = loopback-only inet_protocols = all ########################################## ##### non debconf entries start here ##### ##### client TLS parameters ##### smtp_tls_loglevel=1 smtp_tls_security_level=encrypt smtp_sasl_auth_enable=yes smtp_sasl_password_maps=hash:/etc/postfix/sasl/passwd smtp_sasl_security_options = noanonymous ##### map username@localhost to username@gmail.com ##### smtp_generic_maps=hash:/etc/postfix/generic
Next, edit /etc/postfix/generic
sudo gedit /etc/postfix/generic
and have replace user_name and email_address with your credentials, add all users that may send email through gmail:
user_name@localhost email_address@gmail.com
root@localhost email_address@gmail.com
svnmanager@192.168.1.5 email_address@gmail.com
Now lets generate generic.db from this file:
cd /etc/postfix
sudo postmap generic
Next, lets add your gmail credentials to password file:
cd /etc/postfix/sasl
sudo gedit passwd
Copy this, replace email_address with your gmail email username and gmail_password with your gmail password:
[smtp.gmail.com]:587 email_address@gmail.com:gmail_password
Save the file, lets generate passwd.db now:
cd /etc/postfix/sasl
sudo postmap passwd
chown root.root passwd passwd.db
chmod 600 passwd passwd.db
Restast postfix:
/etc/init.d/postfix restart
Test (replace user_name with your user name):
echo 'test email' | mail -s 'just a test' email_address@gmail.com
sudo sendmail -bv user_name
sudo sendmail -bv email_address@gmail.com
You should receive the email now, sent from your localhost.
If there are any problems, check you /var/log/mail.log
August 26th, 2011 21:43
[…] Starting From Lucid Lynx « How to install apache, php, mysql and phpmyadmin in Ubuntu How to install postfix to relay mail through gmail in Ubuntu […]
November 2nd, 2011 02:00
Probably tried 5 or more tutorials regarding gmail relay. Yours is the ONLY ONE that works.
Thank you very much for this. I can’t thank you enough!
– Ubuntu 11.04 November 2, 2011
November 3rd, 2011 01:54
Thanks. Yeah, I feel the pain. 🙂 That’s the reason I try to do everything on my box before I write how to do it for others. I’m glad I was able to help.
January 31st, 2012 16:36
[…] Great Postfix tutorial […]
February 2nd, 2012 06:42
Thank for the tutorial. Worked perfectly with Ubuntu 11.10.
February 18th, 2012 02:04
Thanks for letting us know. 🙂
March 13th, 2012 16:20
Excellent tutorial. I agree 100% with Annie Moussie. I tried several other tutorials and this was the only one to work.
Thanks!