1.开启qq邮箱的SMTP,并获取授权码,如下所示:
1.进入qq邮箱的设置
2.进入账户
3.开启SMTP服务,并且获取到授权码
2.关闭Linux机器的sendmail服务,开启postfix服务
[root@hadoop001 ~]# service sendmail stop
[root@hadoop001 ~]# service postfix start
[root@hadoop001 ~]# chkconfig --list
显示如下:
3.创建证书文件
[root@hadoop001 ~]# mkdir -p /root/.certs/
[root@hadoop001 ~]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
[root@hadoop001 ~]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@hadoop001 ~]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@hadoop001 ~]# certutil -L -d /root/.certs
[root@hadoop001 ~]# cd /root/.certs
[root@hadoop001 ~]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
4.修改/etc/mail.rc文件
[root@hadoop001 ~]# vi /etc/mail.rc
添加如下内容
set from=xxxxxxx@qq.com #你的qq邮箱
set smtp=smtp.qq.com
set smtp-auth-user=xxxxxxxx #你的qq
#授权码
set smtp-auth-password=xxxxxxx #授权码
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs
5.测试是否成功
[root@hadoop001 ~]# echo "hello word" | mail -s "title" xxxxxxx@qq.com
此时去自己的qq邮箱查看
成功。
6.发送邮件带附件
[root@hadoop001 data]# vi test.log
test1
test2
test3
[root@hadoop001 data]# vi attachment.sh
#!/bin/bash
FROM_EMAIL="xxxxxxx@qq.com" #你的qq
TO_EMAIL="xxxxxxx@qq.com" #你的qq
LOG=/root/data/test.log
echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the test attachement." | mailx \
-r "From: Admin <${FROM_EMAIL}>" \
-a ${LOG} \
-s "Critical:DSHS test log." ${TO_EMAIL}
执行该脚本
[root@hadoop001 data]# sh attachment.sh
用到的命令是 mailx -a (添加附件) mailx -r(设置发件人地址) mail -s(指定主题)
mailx更多参数:http://linux.51yip.com/search/mailx
执行完毕后去自己的qq邮箱: