Set-up sending mail via gmail account

T

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?

Alright.

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'
libsasl2.so.2 => /usr/lib/libsasl2.so.2 (0xb749c000)
libssl.so.10 => /usr/lib/libssl.so.10 (0xb7443000)
libssl3.so => /usr/lib/libssl3.so (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: ./CA.pl -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.

Done.

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

Type: nano /etc/postfix/main.cf

#### GMail START
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable=yes
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
smtp_tls_key_file=/etc/pki/tls/gmail/server.key
smtp_tls_cert_file=/etc/pki/tls/gmail/server.pem
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:

gmail-smtp.l.google.com user@gmail.com:password
smtp.gmail.com user@gmail.com: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 yournick@gmail.com < /dev/null

Insted of 'yournick@gmail.com' enter your GMail address

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

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