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


7 Responses to “How to install postfix to relay mail through gmail in Ubuntu