on request.
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.
1
| yum -y install sendmail mailutils mailx sendmail-bin sendmail-cf cyrus-sasl-plain |
1
2
3
| mkdir -m 700 /etc/mail/authinfo/ cd /etc/mail/authinfo/ |
In this example I have configured it gmail-idpass:
Create file
1
| nano gmail-idpass |
1
| AuthInfo: "U:root" "I:YOURGMAILID@GMAIL.COM" "P:YOURGMAILPASS" |
Save and Exit.
In the next step we will need to create a hash map for the above authentication file:
1
| makemap hash gmail-idpass < gmail-idpass |
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)
1
2
3
4
5
6
7
8
| 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 |
nano /etc/mail/sendmail.mcnow paste following
1
2
3
4
5
6
7
8
9
| # 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-idpass.db')dnl # End config for gmail # |
Now your sendmail.mc file will look a like as following after editing (last few lines)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| dnl MASQUERADE_DOMAIN(localhost)dnl dnl MASQUERADE_DOMAIN(localhost.localdomain)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-idpass.db')dnl # End config for gmail # MAILER(smtp)dnl MAILER(procmail)dnl dnl MAILER(cyrusv2)dnl |
1
| make -C /etc/mail |
1
| /etc/init.d/sendmail reload |
TEST VIA SENDING MAIL FROM SHELL
Now you can send email from your command line using mail command:
1
| echo "Mail Body - Test Message from CENTOS Shell by zaib" | mail -s "Subjct is Mail Sending from CLI" -r yourgmailid to-address@hotmail.com |
TO check its status via Logs, you can view file
tail -f /var/log/mailllogand you may see something following on successfully sent message
1
2
3
4
5
6
| Aug 6 08:51:10 radius-master sendmail[2800]: t76FpAGJ002800: from=yourgmailid@gmail.com, size=379, class=0, nrcpts=1, msgid=<55c3826e.HFMzaRe3xPHSfUV4%yourgmailid@gmail.com>, relay=root@localhost Aug 6 08:51:11 radius-master sendmail[2801]: t76FpAS5002801: from=<yourgmailid@gmail.com>, size=527, class=0, nrcpts=1, msgid=<55c3826e.HFMzaRe3xPHSfUV4%yourgmailid@gmail.com>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1] Aug 6 08:51:11 radius-master sendmail[2800]: t76FpAGJ002800: to=to-address@hotmail.com, ctladdr=yourgmailid@gmail.com (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30379, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (t76FpAS5002801 Message accepted for delivery) Aug 6 08:51:12 radius-master sendmail[2803]: STARTTLS=client, relay=gmail-smtp-msa.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=ECDHE-RSA-AES128-GCM-SHA256, bits=128/128 Aug 6 08:51:13 radius-master sendmail[2803]: t76FpAS5002801: to=<to-address@hotmail.com>, delay=00:00:03, xdelay=00:00:02, mailer=relay, pri=120527, relay=gmail-smtp-msa.l.google.com. [64.233.167.108], dsn=2.0.0, stat=Sent (OK 1438920803 by17sm6409593wib.18 - gsmtp) |
You can also mail any command output like this
uptime | mail -s “UPTIME status for your server” -r yourgmailid@gmail.com to-address@hotmail.com
Done.
TIP: For radius manager mail function, as far as i remember, you dont need to modify any setting, rm will use localhost as mail sending feature which will afterwards relayed by sendmail using gmail.
0 comments:
Post a Comment