使用shell发送邮件

1. 生成QQ邮箱登录授权码

使用shell发送邮件
开启SMTP服务,获取授权码
使用shell发送邮件

2. 邮件服务器配置

2.1 关闭sendmail服务

#我这里没有sendmail服务
[[email protected]~]# service sendmail status
sendmail: unrecognized service
[[email protected]~]# service sendmail stop
sendmail: unrecognized service

2.2 开启postfix服务

[[email protected]~]# service postfix status
master is stopped
[[email protected]~]# service postfix start
Starting postfix:                                          [  OK  ]
[[email protected] ~]# service postfix status
master (pid  2566)) is running...

2.3 创建证书

[[email protected] ~]# mkdir .certs
[[email protected] ~]# cd .certs/
[[email protected] .certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

[[email protected] .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[[email protected] .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[[email protected] .certs]#  certutil -L -d /root/.cert

[[email protected] .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt

2.4 配置mail.rc配置文件

在最后追加一下内容

set from=你的QQ邮箱
set smtp=smtp.qq.com
set smtp-auth-user=你的QQ号
#授权码
set smtp-auth-password=你的授权码
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs

2.5 测试

想自己邮箱发送一条测试邮件

[[email protected] .certs]#  echo hello word | mail -s " title" [email protected]

使用shell发送邮件

3 发送一个带附件的邮件

[[email protected] shell]# vi mail_attachment.sh 
#!/bin/bash

FROM_EMAIL="[email protected]"
TO_EMAIL="[email protected]"

LOG=/root/shell/sh.help

echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the fail sql attachement." | mailx \
-r "From: alertAdmin <${FROM_EMAIL}>" \
-a ${LOG} \
-s "Critical:DSHS fail sql." ${TO_EMAIL}

使用shell发送邮件