Wednesday, August 10, 2016

How to Send Email from Gmail E-mail's account from command Linux CentOS

This is how to blog, Send Email from your External Email account i.e. G-mail address. Linux machine command or put it into Shell Script.


Requirement - 

1- One  Linux Machine (Centos 6.x)
2- One  G-Mail account.

Step 1 -

Download Epel repository and install it.

[root@cent backupdir]# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

[root@cent backupdir]# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
--2016-08-10 16:04:29--  http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
Resolving dl.fedoraproject.org... 209.132.181.27, 209.132.181.24, 209.132.181.23, ...
Connecting to dl.fedoraproject.org|209.132.181.27|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14540 (14K) [application/x-rpm]
Saving to: “epel-release-6-8.noarch.rpm”

100%[==================================================================================>] 14,540      14.2K/s   in 1.0s

2016-08-10 16:04:37 (14.2 KB/s) - “epel-release-6-8.noarch.rpm” saved [14540/14540]


Install package using rpm command

[root@cent backupdir]# rpm -ivh epel-release-6-8.noarch.rpm
warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@cent backupdir]#

Step 2 -

Install Ssmtp package using yum command.
[root@cent backupdir]# yum install ssmtp
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
epel/metalink                                                                                                                                                         | 3.8 kB     00:00
 * base: mirrors.vhost.vn
 * epel: epel.mirror.angkasa.id
 * extras: mirrors.vhost.vn
 * updates: mirrors.vhost.vn
epel                                                                                                                                                                  | 4.3 kB     00:00
epel/primary_db                                                                                                                                                       | 5.9 MB     00:39
Resolving Dependencies
--> Running transaction check
---> Package ssmtp.x86_64 0:2.61-22.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================
 Package                                     Arch                                         Version                                           Repository                                  Size
=============================================================================================================================================================================================
Installing:
 ssmtp                                       x86_64                                       2.61-22.el6                                       epel                                        53 k

Transaction Summary
=============================================================================================================================================================================================
Install       1 Package(s)

Total download size: 53 k
Installed size: 96 k
Is this ok [y/N]: y
Downloading Packages:
ssmtp-2.61-22.el6.x86_64.rpm                                                                                                                                          |  53 kB     00:00
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
Importing GPG key 0x0608B895:
 Userid : EPEL (6) <epel@fedoraproject.org>
 Package: epel-release-6-8.noarch (installed)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : ssmtp-2.61-22.el6.x86_64                                                                                                                                                  1/1
  Verifying  : ssmtp-2.61-22.el6.x86_64                                                                                                                                                  1/1

Installed:
  ssmtp.x86_64 0:2.61-22.el6

Complete!


Step 3 - 

Enable required changes in ssmtp.conf file
[root@cent backupdir]# vi /etc/ssmtp/ssmtp.conf

Made highlighted changes in your ssmtp.conf file as below. and append two line if doesn't exist.
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
USESTARTTLS=YES


# /etc/ssmtp.conf -- a config file for sSMTP sendmail.
#
# See the ssmtp.conf(5) man page for a more verbose explanation of the
# available options.
#

# The person who gets all mail for userids < 500
# Make this empty to disable rewriting.
root=username@gmail.com

# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and your mailhub is so named.
mailhub=smtp.gmail.com:587

# Example for SMTP port number 2525
# mailhub=mail.your.domain:2525
# Example for SMTP port number 25 (Standard/RFC)
# mailhub=mail.your.domain
# Example for SSL encrypted connection
# mailhub=mail.your.domain:465

# Where will the mail seem to come from?
#RewriteDomain=gmail.com

# The full hostname
Hostname=gmail.com
AuthUser=username@gmail.com
AuthPass=Password
# Set this to never rewrite the "From:" line (unless not given) and to
# use that address in the "from line" of the envelope.
#FromLineOverride=YES

# Use SSL/TLS to send secure messages to server.
#UseTLS=YES

# Use SSL/TLS certificate to authenticate against smtp host.
#UseTLSCert=YES

# Use this RSA certificate.
#TLSCert=/etc/pki/tls/private/ssmtp.pem
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
# Get enhanced (*really* enhanced) debugging information in the logs
# If you want to have debugging of the config file parsing, move this option
# to the top of the config file and uncomment
#Debug=YES
USESTARTTLS=YES

Step 4 -

Now login using your username@gmail.com account and enable access for less secure app access using below url:
https://www.google.com/settings/security/lesssecureapps










Looks like above image.

Step 5 -

Let's test configuration using below command. If everything goes well command output should like below.

[root@cent backupdir]# echo "test" | ssmtp -vvv gmailaddress@gmail.com
[<-] 220 smtp.gmail.com ESMTP ya6sm63613641pab.22 - gsmtp
[->] EHLO gmail.com
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO gmail.com
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6
[->] bWV0cm9sYW5kYW1hckBnbWFpbC5jb20=
[<-] 334 UGFzc3dvcmQ6
[<-] 235 2.7.0 Accepted
[->] MAIL FROM:<root@gmail.com>
[<-] 250 2.1.0 OK ya6sm63613641pab.22 - gsmtp
[->] RCPT TO:<gmailaddress@gmail.com>
[<-] 250 2.1.5 OK ya6sm63613641pab.22 - gsmtp
[->] DATA
[<-] 354  Go ahead ya6sm63613641pab.22 - gsmtp
[->] Received: by gmail.com (sSMTP sendmail emulation); Wed, 10 Aug 2016 17:21:27 +0530
[->] From: "root" <root@gmail.com>
[->] Date: Wed, 10 Aug 2016 17:21:27 +0530
[->] test
[->]
[->] .
[<-] 250 2.0.0 OK 1470829891 ya6sm63613641pab.22 - gsmtp
[->] QUIT
[<-] 221 2.0.0 closing connection ya6sm63613641pab.22 - gsmtp
[root@cent backupdir]#


That's ALL, SSMTP have been configured successfully and sending email.

That's All
!!!Cheers!!!

No comments:

Post a Comment