To
send Email from Linux (Centos) shell using simple mail commands, you need to
setup sendmail and use G-MAIL as relay (comes in handy). This was also required
in radius manager setup where radius system sends various alerts to user like
expiry alerts, quota alerts and my own customized alerting events.
First
install required items.
yum -y install sendmail mailutils mailx sendmail-bin
sendmail-cf cyrus-sasl-plain
Now issue following command to create Gmail authentication
file in a folder in which you will add Gmail user name and
password.
# mkdir -m 700 /etc/mail/authinfo
# cd /etc/mail/authinfo/
# mkdir -m 700 /etc/mail/authinfo
# cd /etc/mail/authinfo/
Next we need to create an authentication file with following contents. You can name it as you like. In this example I have configured it gmail-pass:
Create File and add following:-
# vi gmail-pass
AuthInfo: "U:root" "I:email@gmail.com" "P:email_password"
[Note: Replace the above with your gmail id and password]
In the next step we will need to create a hash map for the above authentication file:
# makemap hash gmail-pass < gmail-pass
Configure your sendmail
Now add bellow lines into your /etc/mail/sendmail.mc configuration file. Make sure you add them at end, but right above first “MAILER” definition line:
Example your file may look like this before editing (last few
lines)
dnl MASQUERADE_DOMAIN(localhost)dnl
dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl
dnl MASQUERADE_DOMAIN(mydomain.lan)dnl
MAILER(smtp)dnl
MAILER(procmail)dnl
dnl MAILER(cyrusv2)dnl
You need to add the following lines above MAILER(smtp)dnl line
# vi /etc/mail/sendmail.mc
now paste following and save changes
# Adding config for gmail #
define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-pass.db')dnl
# End config for gmail #
Now your sendmail.mc file will look like as following after editing (last few lines)
dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
dnl MASQUERADE_DOMAIN(localhost)dnl
dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl
dnl MASQUERADE_DOMAIN(mydomain.lan)dnl
# Adding config for gmail #
define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-pass.db')dnl
# End config for gmail #
MAILER(smtp)dnl
MAILER(procmail)dnl
dnl MAILER(cyrusv2)dnl
Now we need to re-build sendmail’s configuration. Run following command to do so.
# make -C /etc/mail
Reload sendmail service:
# /etc/init.d/sendmail reload
In case, sendmail doesn’t restart successfully, ensure Postfix is not running. Stop it if required.
service postfix status
service postfix stop
Also, ensure/enable sendmail autostartup:-
# chkconfig --list sendmail
# chkconfig --add sendmail
TEST VIA SENDING MAIL FROM SHELL
Now you can send email from your command line using mail command:
$ echo "SendMail configuration - Test Mail" | mail -s "Testing Sendmail" email@ymail.com
TO check its status via Logs, you can view file
# tail -f /var/log/maillog
dnl MASQUERADE_DOMAIN(localhost)dnl
dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl
dnl MASQUERADE_DOMAIN(mydomain.lan)dnl
MAILER(smtp)dnl
MAILER(procmail)dnl
dnl MAILER(cyrusv2)dnl
You need to add the following lines above MAILER(smtp)dnl line
# vi /etc/mail/sendmail.mc
now paste following and save changes
# Adding config for gmail #
define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-pass.db')dnl
# End config for gmail #
Now your sendmail.mc file will look like as following after editing (last few lines)
dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
dnl MASQUERADE_DOMAIN(localhost)dnl
dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl
dnl MASQUERADE_DOMAIN(mydomain.lan)dnl
# Adding config for gmail #
define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-pass.db')dnl
# End config for gmail #
MAILER(smtp)dnl
MAILER(procmail)dnl
dnl MAILER(cyrusv2)dnl
Now we need to re-build sendmail’s configuration. Run following command to do so.
# make -C /etc/mail
Reload sendmail service:
# /etc/init.d/sendmail reload
In case, sendmail doesn’t restart successfully, ensure Postfix is not running. Stop it if required.
service postfix status
service postfix stop
Also, ensure/enable sendmail autostartup:-
# chkconfig --list sendmail
# chkconfig --add sendmail
TEST VIA SENDING MAIL FROM SHELL
Now you can send email from your command line using mail command:
$ echo "SendMail configuration - Test Mail" | mail -s "Testing Sendmail" email@ymail.com
TO check its status via Logs, you can view file
# tail -f /var/log/maillog
Hello,
ReplyDeleteThanks for posting these steps, but is there any way to check postfix/sendmail configuration when email sent from command line isn't appearing in GMail inbox ?
Please clarify.
Tks in adv.