Set-up sending mail via gmail account


his tutorial works for CentOS 6 only. For your specific OS tutorial use the tags or serach function in the sidebar area.

This tutorial asumes a scenario that we bought a VPS for our own little business like online shopping or a portal,  that won't fit in shared hosting, therfore you don't need to setup full in-and-out mailserver software only for sending e-mails from your VPS box. I'm also assuming that you already have a Gmail acoount. Who doesn't have, right?


First we have to remove sendmail, which on our Hostinger VPS is already pre-installed MTA. Then we install postfix, which is much easier and better to configure and work than sendmail.

Type: yum install postfix

Type: yum remove sendmail


Now check if the postfix that you installed has been built with necessary dependencies:

Type: ldd `which postfix` | grep 'libsasl\|libssl'

This should return sth like this:

[root@vps ~]# ldd `which postfix` | grep 'libsasl\|libssl' => /usr/lib/ (0xb749c000) => /usr/lib/ (0xb7443000) => /usr/lib/ (0xb6f25000)
[root@vps ~]#

You also have to install openssl

Type: yum install openssl openssl-perl

Now, let's create a Certificate Authority.

Type: cd /etc/pki/tls/misc

Then execute the generator script

Type: ./ -newca

​You will have to specify some options now:

  1.  when it asks for filename just hit Enter
  2. PEM passphrase = enter something BUT remeber it
  3. Country Name, State, Locality Name, Organization Name (remember that also), and Organizational Unit Name to your liking (or take the defaults) . Organization Name (if not defaults)  must match one in a key we create later. For the Common Name, fill in “CA” (without quotes). Take defaults for everything else and when prompted to enter the pass phrase you need to enter the same one you did above.

Create a client key that is sent to GMail to start the SSL encryption:

Enter commands as follows:

  1. cd /etc/pki/tls
  2. mkdir gmail
  3. cd gmail
  4. openssl genrsa -out server.key 1024
  5. openssl req -new -key server.key -out server.csr
  6. openssl ca -out server.pem -infiles server.csr

The 5th command will ask you for the Country, State, Locality, etc parameters again. The only value that has to be the same and was entered when generating Certificate Authority is the Organization Name. You remeber it, right?

The last 6th command will prompt you for your Certificate Authority passphrase that you had to remeber also. When asked if you want to Sign the certificate, say yes. Also when asked if you should commit it, say yes.


Now, copy and paste below code to the /etc/postfix/ file (at the very bottom)

Type: nano /etc/postfix/

#### GMail START
relayhost = []:587
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_note_starttls_offer = yes
tls_random_source = dev:/dev/urandom
smtp_tls_scert_verifydepth = 5
smtpd_tls_ask_ccert = yes
smtpd_tls_req_ccert =no
smtp_tls_enforce_peername = no
#### GMail END

Save and exit.

Now create a file with your credentials to GMail account login:

Type: touch /etc/postfix/sasl_passwd

By the way, that will create an empty file.

Type: nano /etc/postfix/sasl_passwd

and copy and paste below code, next change user to your username and password to your gmail password:

Save and exit.

Next, type : 

  1. cd /etc/postfix
  2. postmap sasl_passwd
  3. chmod o-r /etc/postfix/sasl_passwd
  4. chmod o-r /etc/postfix/sasl_passwd.db
  5. service postfix restart

like that:

[root@vps postfix]# cd /etc/postfix
[root@vps postfix]# postmap sasl_passwd
[root@vps postfix]# chmod o-r /etc/postfix/sasl_passwd
[root@vps postfix]# chmod o-r /etc/postfix/sasl_passwd.db
[root@vps postfix]# service postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
[root@vps postfix]#

Send a mail to check if everything is ok:

Type: mail -s Test < /dev/null

Insted of '' enter your GMail address

[root@vps log]# mail -s Test < /dev/null
Null message body; hope that's ok
[root@vps log]#

Now check your e-mail, if it arrives - all is set!