Syed Jahanzaib Personal Blog to Share Knowledge !

July 1, 2016

Postfix as ‘GMAIL’ relay to send Email

Filed under: Linux Related — Tags: , — Syed Jahanzaib / Pinochio~:) @ 1:45 PM


This post was made to illustrate howto send emails via postfix mail server using Gmail. Previously I was using sendmail with gmail in combination but few times sendmail made troubles in new installation, therefore i switched to postfix which is quite simple as compared to sendmail complexity. I have tested it at various networks and so far found it reliable.

It can be used by other mail applications like mail utility, radius manager web bulk mail function, or any other you like.  We will use GMAIL as relay to send our emails using our gmail account. You need functional gmail account for this purpose, and make sure ‘allow less secure application’ is turned on to availe this function.

We can also use this as centralized email server gateway so that all of our devices on the LAN like Mikrotik Router, Cisco Switches, Mobile Devices and others can send via this email gateway so that we can get rid of configuring email services at each system separately.

That’s why in some specific situation, I say “Work Smarter, Not Harder” / Za!b

Software Used:

OS : Ubuntu 12.4 / 32bit
Email Server : POSTFIX 2.9.6

Let’s Start …

First we need to update ubuntu apt-get and then install the postfix mail server application


apt-get update && apt-get install postfix mailutils libsasl2-modules


  • When prompted for “General type of mail configuration” choose Internet Site.
  • When prompted for a “Mail name,” you can use default name.

Once above installation is done,

Create and edit new file which will store the Gmail ID and Password

touch /etc/mailname
# Change the radius.local to match your hostname FQDN name
echo "pcname.local" > /etc/mailname
# create File which will contain user id and password
touch /etc/postfix/sasl_passwd
nano /etc/postfix/sasl_passwd

and paste following [Make sure you replace YOURGMAILID+YOURPASS with valid gmail credentials.


Save & Exit.


Now Make it accessible for root

chmod 600 /etc/postfix/sasl_passwd


Edit postfix main configuration File by

nano /etc/postfix/

Remove all previous lines and paste following ….

#Postfix main configuration file / Syed Jahanzaib / aacable at hotmail dot com / http:// aacable . wordpress . com
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
myhostname = radius.localhost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
# change following with your local config
mydestination = pcname.localhost, localhost.localhost, , localhost
# Relay hosts entries as below
mynetworks =
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
relayhost = []:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Save & Exit.


Use postmap command to compile and hash the contents of sasl_passwd. The results will be stored in your Postfix configuration directory in the file sasl_passwd.db.

postmap /etc/postfix/sasl_passwd


Create folder to hold mails

mkfifo /var/spool/postfix/public/pickup

: If above folder already exists, ignore and continue


Change the FROM address. It will be displayed in user inbox from section.

chfn -f 'YOUR COMPANY NAME' root


Make sure you have Enable “Less Secure Apps” In Gmail

Step#8 [ required only if you have SENDMAIL installed previously, probably you dont need this to do in new installation. This was a point in which I got stuck for hour]

IF sendmail was previously installed, then remove it and stop its service

apt-get remove sendmail
service sendmail stop


Finally Restart POSTFIX service

sudo /etc/init.d/postfix restart

FINAL Step#10 / TESTiNG the Ride !

Now try to send email by using command in the terminal, change the email address to your email address

mail -s "Test subject from postfix by Syed.Jahanzaib"

After this it will ask cc:  just press enter
it will show blank cursor where you can type the email body, type it any text or leave it blank , then press press CTRL+D to finally send the email

Now at the same time in other window, Check mail log for any error

tail -f /var/log/mail.log

psotfix log



Tip: i had some bad experience with the IPV6 enabled in my Ubuntu box. so its recommended to disable it. just my experience.

%d bloggers like this: