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 "radius.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
mydestination = radius.localhost, localhost.localhost, , localhost
mynetworks = [::ffff:]/104 [::1]/128
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 at user inbox.

chfn -f 'YOUR COMPANY NAME' root


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


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.


  1. i’m having this error:
    Jul 6 17:31:36 rafael32 postfix/smtp[28933]: 25F8F6CE132D: to=,[]:587, delay=0.47, delays=0/0/0.46/0.01, dsn=5.5.1, status=bounced (host[] said: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 e69sm577767ite.2 – gsmtp (in reply to MAIL FROM command))
    Jul 6 17:31:36 rafael32 postfix/qmgr[28908]: 25F8F6CE132D: removed

    I don’t know what happend.

    Comment by Rafael Francisco — July 7, 2016 @ 2:35 AM

  2. Syed, to resolved my problem i found that i need to install libsasl2-modules. After i installed everything goes right.


    Comment by Rafael Francisco — July 7, 2016 @ 2:52 AM

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: