Linux服务器mail程序本身就是调用sendmail来进行邮件发送的,sendmail服务器提供对外的邮件发送功能。CentOS默认不能发送邮件,需要发送邮件的童鞋可以安装一个sendmail程序

1
2
3
4
5
6
7
8
9
10
11
12
[root@ProxyServer ~]# yum -y install sendmail
[root@ProxyServer ~]# /etc/init.d/sendmail start
Starting sendmail:                                         [  OK  ]
Starting sm-client:                                        [  OK  ]
[root@ProxyServer ~]# chkconfig sendmail on
[root@ProxyServer ~]# man mail
......
       mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account]
              [-S variable[=value]] to-addr . . .
       mailx [-BDdeEHiInNRv~] [-T name] [-A account] [-S variable[=value]] -f [name]
       mailx [-BDdeEinNRv~] [-A account] [-S variable[=value]] [-u user]
......

1、通过命令行发送邮件

1
2
3
4
5
6
[root@ProxyServer ~]# mail -s "test" justin@51cto.com 
dfdafd
dfadf
fadf
EOT
[root@ProxyServer ~]#

第一行是输入的命令,-s表示邮件的主题,后面的justin@51cto.com则是邮件的接收人,输入完这行命令后回车,会进入邮件正文的编写,可以输入任何文字,比如上面的三行。当邮件正文输入完成后,需要按CTRL+D结束输入,此时会提示你输入Cc地址,即邮件抄送地址,没有直接回车就完成了邮件的发送。

wKioL1Zi16XwUtfqAACiBqolEFA725.png

2、使用管道进行邮件发送

1
[root@ProxyServer ~]# echo "hello,echo"|mail -s "echo" justin@51cto.com

wKiom1Zi11_DEJCEAAAp24ESS4U059.png

使用管道直接敲入这行命令即可完成邮件的发送,其中echo后的是邮件正文。

3、使用文件进行邮件发送

1
[root@ProxyServer ~]# mail -s "password" justin@51cto.com < /etc/passwd

使用上面的命令后,我们就可以把/etc/passwd文件的内容作为邮件的内容发送给justin@51cto.com

wKiom1Zi133RwroAAAIOD5bPGIE048.png

  很多情况下,我们也需要使用邮件来发送附件,在linux下使用mail命令发送附件也很简单,不过首先需要安装uuencode软件包,这个程序是对二进制文件进行编码使其适合通过邮件进行发送,直接使用centos的yum源可能找不到uuencode命令的包sharutils,我这里使用了网易Yum源

1
2
3
[root@ProxyServer yum.repos.d]# yum -y install sharutils
[root@ProxyServer yum.repos.d]# uuencode CentOS6-Base-163.repo 163_yum_repo |mail -s "附件" pengyl2@finchina.com < /etc/passwd
[root@ProxyServer yum.repos.d]#

CentOS6-Base-163.repo 文件作为邮件的附件发送出去了。uuencode有两个参数,第一个是要发送的文件CentOS6-Base-163.repo,第二个是显示的文件名称163_yum_repo。

4、使用外部smtp(qq的SMTP)来发送邮件

bin/mail会默认使用本地sendmail发送邮件,这样要求本地的机器必须安装和启动Sendmail服务,而通过修改配置文件/etc/mail.rc(/etc/nail.rc)可以使用外部SMTP服务器,可以达到不使用sendmail而用外部的smtp服务器发送邮件的目的。

1
2
3
4
5
6
7
8
[root@ProxyServer ~]# vim /etc/mail.rc          #文末添加以下
 set from=justin@51cto.com 
 set smtp=smtp.51cto.com
 set smtp-auth-user=justin@51cto.com 
 set smtp-auth-password=51cto 
 set smtp-auth=login
[root@ProxyServer ~]# source /etc/mail.rc
[root@ProxyServer ~]# mail -s "51cto" 15001*****@139.com < /etc/passwd

from:发送的邮件地址(真实地址,我测试时候直接写justin发送邮件失败),对方显示的发件人

smtp:发生的外部smtp服务器的地址

smtp-auth-user:外部smtp服务器认证的用户名

smtp-auth-password:外部smtp服务器认证的用户密码

smtp-auth:邮件认证的方式

wKioL1ZKqgzBo0p-AAA0OtsRubo776.png


如果提示mail: command not found

1
2
[root@ProxyServer ~]# mail -s "password" justin@51cto.com < /etc/passwd
-bash: mail: command not found

那么就是没有安装mail命令,此时需要安装mail命令

1
2
3
4
5
6
7
8
[root@ProxyServer ~]# yum install mailx -y
[root@ProxyServer ~]# rpm -qa|grep mail
libreport-plugin-mailx-2.0.9-19.el6.x86_64
mailx-12.4-7.el6.x86_64
procmail-3.22-25.1.el6.x86_64
mailcap-2.1.31-2.el6.noarch
sendmail-8.14.4-8.el6.x86_64
[root@ProxyServer ~]#

5、mail日志在log下

1
2
3
4
[root@CHINFO3 ~]# cd /var/log/
[root@CHINFO3 log]# cat mail
mail/      maillog    maillog.1  maillog.2  maillog.3  maillog.4  
[root@CHINFO3 log]# cat mail

6、使用sendmail -bp 或者 mailq 可以查看到邮件发送队列,里边会列出有几个邮件等待发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@CHINFO3 mqueue]# mailq
        /var/spool/mqueue (5 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
tB36BqUi005128     1716 Thu Dec  3 14:11 <infa@chinfo3>
                 (Deferred: mx.263.net.: No route to host)
                     <pvbutler@51cto.com>
tB21TicN013397      415 Wed Dec  2 09:29 <infa@chinfo3>
                 (Deferred: mx.263.net.: No route to host)
                     <pvbutler@51cto.com>
......
[root@CHINFO3 mqueue]# sendmail -bp
        /var/spool/mqueue (5 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
tB36BqUi005128*    1716 Thu Dec  3 14:11 <infa@chinfo3>
                 (Deferred: mx.263.net.: No route to host)
                     <pvbutler@51cto.com>
tB21TicN013397      415 Wed Dec  2 09:29 <infa@chinfo3>
                 (Deferred: mx.263.net.: No route to host)
                     <pvbutler@51cto.com>
......
[root@CHINFO3 mqueue]#

邮件队列的文件都保存在 /var/spool/mqueue/ 文件夹内,查看此文件夹时,可以看到邮件队列内的邮件:

1
2
3
4
5
6
[root@CHINFO3 mqueue]# pwd
/var/spool/mqueue
[root@CHINFO3 mqueue]# ls
dftB111d4Q031164  dftB213omV012806  dftB36BqUi005128  qftB117J4b006323  qftB21TicN013397
dftB117J4b006323  dftB21TicN013397  qftB111d4Q031164  qftB213omV012806  qftB36BqUi005128
[root@CHINFO3 mqueue]#

如果需要删除队列,直接清空该文件夹即可:

1
2
3
4
5
6
7
8
9
10
11
[root@CHINFO3 mqueue]# pwd
/var/spool/mqueue
[root@CHINFO3 mqueue]# ls
dftB111d4Q031164  dftB213omV012806  dftB36BqUi005128  qftB117J4b006323  qftB21TicN013397
dftB117J4b006323  dftB21TicN013397  qftB111d4Q031164  qftB213omV012806  qftB36BqUi005128
[root@CHINFO3 mqueue]# rm -f *
[root@CHINFO3 mqueue]# ls
[root@CHINFO3 mqueue]# mailq
/var/spool/mqueue is empty
        Total requests: 0
[root@CHINFO3 mqueue]#


或者直接使用sendEmail来发送邮件,

sendEmail是一款轻量级,巧小,无需安装的邮件发送命令。只需下载,解压就可以使用,不要和sendmail搞混掉了。官网地址:http://caspian.dotconf.net/menu/Software/SendEmail/

1
2
3
4
5
6
[root@Zabbix_Server ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[root@Zabbix_Server ~]#  tar zxvf sendEmail-v1.56.tar.gz 
[root@Zabbix_Server ~]# cp -a sendEmail-v1.56/sendEmail /usr/local/bin/
[root@Zabbix_Server ~]# ll /usr/local/bin/sendEmail 
-rwxr-xr-x 1 root root 80213 Sep 30  2009 /usr/local/bin/sendEmail
[root@Zabbix_Server ~]#  sendEmail -h

-f 表示发送者的邮箱

-t 表示接收者的邮箱

-cc 表示抄送发给谁

-bcc 表示暗抄送给谁

-o message-content-type=html   邮件内容的格式,html表示它是html格式

-o message-charset=utf8        邮件内容编码

-s 表示SMTP服务器的域名或者ip

-u 表示邮件的主题

-xu 表示SMTP验证的用户名

-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)

-m 邮件的内容

-a 要发送的附件

1
2
3
4
[root@Zabbix_Server sendEmail-v1.56]# /usr/local/bin/sendEmail -s smtp.51cto.com -xu zabbix@51cto.com -xp 51cto2016 -f zabbix@51cto.com -t justin@51cto.com -u test -m 123456 -o message-content-type=text -o message-charset=gb2312 
 
Sep 20 10:20:19 zabbix_server sendEmail[6090]: Email was sent successfully!
[root@Zabbix_Server sendEmail-v1.56]#

发送成功之后会有如下提示:

Jul 29 15:02:53 e10074 sendEmail[26347]: Email was sent successfully!