Today i will show how to send an emails from CentOS using a GMAIL account. For this, we will use mailx and ssmtp.
Go to Gmail and enable this (ACCOUNT):
1. Disable POSTFIX (You can remove, i choose disable)
root@hostname]# service postfix stop ; chkconfig postfix off
2. Install SSMTP and MailX
[root@hostname]# yum install mailx ssmtp -y
in centos 7 i needed install manually: http://pkgs.org/download/ssmtp (centos6)
3. Configure SSMTP /etc/ssmtp/ssmtp.conf file to looks like
root=YOUREMAIL@gmail.com mailhub=smtp.gmail.com:587 rewriteDomain=gmail.com hostname=YOURMACHINEHOSTNAME; UseTLS=Yes UseSTARTTLS=Yes AuthUser=YOUREMAIL@gmail.com AuthPass=YOUR PASSWORD; FromLineOverride=YES # This solved if you get a ssmtp: Cannot open smtp.gmail.com:587 when try to send an email # if you enabled uncommenting DEBUG=Yes line and your /var/log/maillog show # SSL not working: certificate verify failed (20) Uncomment the following line but first VERIFY FILE EXISTS TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt #DEBUG=Yes
4. Configure SSMTP /etc/ssmtp/revaliases file to looks like
root:YOUREMAIL@gmail.com:smtp.gmail.com:587
5. Configure Alternatives (command mail)
[root@hostname]# alternatives --config mta
There are 2 programs which provide ‘mta’.
Selection Command ----------------------------------------------- + 1 /usr/sbin/sendmail.postfix * 2 /usr/sbin/sendmail.ssmtp
Enter to keep the current selection[+], or type selection number: 2 (press ENTER)
6. Test Email
[root@hostname]# echo Hi from command line | mail youremail@youremailhost.com
7. Allow more users to send emails using mail
If you want to allow to more users to send emails using it, just add user to the mail group
[root@hostname]# gpasswd -a USER mail
and add a line to the /etc/ssmtp/revaliases
USER:YOUR EMAIL@gmail.com:smtp.gmail.com:587