首页 » 系统相关 » How To send email From a shell on linux

How To send email From a shell on linux

usually,we do not want to setup an email serve,but just  want to send an email  from a Linux Shell script easily   when got  any  alert or exptions.then let’s know what happend with the event on the Host,then to solve this problem.

ssmtp is quite easy to configure.the following was  configuration ssmtp on centos5(or  redhat el5)
[oracle@ggsdb ~]$ lsb_release -a
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-

ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 5.6 (Final)
Release:        5.6
Codename:       Final

1,download and install epel, addition yum repository

[root@ggsdb ~]# wget dl.iuscommunity.org/pub/ius/stable/CentOS/5/x86_64/epel-release-5-4.noarch.rpm
如果此链接失效,请自己去 http://rpm.pbone.net 上搜索

[root@ggsdb ~]# rpm -ivh epel-release-5-4.noarch.rpm

2, yum install ssmtp(be)
[root@ggsdb ~]# yum -y install ssmtp

3, edit the ssmtp configuration file.
[root@ggsdb ~]# cd /etc/ssmtp
[root@ggsdb ssmtp]# cp ssmtp.conf ssmtp.conf_bak
[root@ggsdb ssmtp]# vi ssmtp.conf

Update file with the following settings:

mailhub=smtp.mail.anbob.com
RewriteDomain=mail.anbob.com
Hostname=mail.anbob.com
AuthUser=zhangweizhao@mail.anbob.com
AuthPass=123456

4,configure the Aliases,default Sender show root@mail.anbob.com, sender show robot@mail.anbob.com affected by alias
[root@ggsdb ~]# vi  /etc/ssmtp/revaliases
root:Robot@mail.anbob.com:smtp.mail.anbob.com:25

5, make sure you disable Sendmail
Move or replace the sendmail and thencreate a symbolic link for sSMTP to sendmail with authentication
[root@ggsdb ~]# mkdir /root/.bakup
[root@ggsdb ~]# mv /usr/sbin/sendmail       /root/.bakup/
[root@ggsdb ~]# which ssmtp
/usr/sbin/ssmtp
[root@ggsdb ~]# ln -s /usr/sbin/ssmtp      /usr/sbin/sendmail

Before we start using sSMTP lets stop the sendmail which is default installed on all Linux distribution.
[root@ggsdb ~]# service sendmail stop
[root@ggsdb ~]# chkconfig –levels 2345 sendmail off

6,Now, you can use mail / mailx command to send email messages.ou can test settings using

following syntax:
[root@ggsdb ~]# echo “This is a test” | mail -s “Test” zhangweizhao@mail.anbob.com

打赏

,

对不起,这篇文章暂时关闭评论。