Shell:用postfix发送邮件到QQ邮箱

文章目录

    • 1、开启 QQ 邮箱的 的 SMTP , 获取授权码
    • 2、关闭sendmial服务
    • 3、开启postfix服务
    • 4、创建认证
    • 5、配置mail.rc
    • 6、测试
    • 7、发送一个不带附件的邮件到QQ邮箱
    • 8、发送一个带附件的邮件到QQ邮箱

1、开启 QQ 邮箱的 的 SMTP , 获取授权码

Shell:用postfix发送邮件到QQ邮箱_第1张图片
Shell:用postfix发送邮件到QQ邮箱_第2张图片

2、关闭sendmial服务

我的虚拟机没有这个服务
[root@vm01 ~]# service sendmail stop
[root@vm01 ~]# chkconfig sendmail off

3、开启postfix服务

[root@vm01 ~]# service postfix start
[root@vm01 ~]# chkconfig postfix on
[root@vm01 ~]# postfix check  #检查postfix是否有其他问题
比如:
[root@vm01 ~]# rpm -qa|grep mysql
[root@vm01 ~]# yum install mysql-libs

4、创建认证

###创建目录,用来存放证书
[root@vm01 ~]# mkdir -p /root/.certs/
####向qq请求证书
[root@vm01 ~]#  echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
####添加一个SSL证书到证书数据库中
[root@vm01 ~]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
####添加一个Global 证书到证书数据库中
[root@vm01 ~]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
####列出目录下证书
[root@vm01 ~]# certutil -L -d /root/.certs
[root@vm01 ~]# cd /root/.certs
###证书被信任
[root@vm01 ~]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu"  -d ./ -i qq.crt

Shell:用postfix发送邮件到QQ邮箱_第3张图片

5、配置mail.rc

在行尾追加

vi /etc/mail.rc

set from=[email protected]  
set smtp=smtp.qq.com
set smtp-auth-user=26xxxx124
#授权码
set smtp-auth-password=dqxxxxxxxxieb  ###授权码
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs

6、测试

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

Shell:用postfix发送邮件到QQ邮箱_第4张图片

7、发送一个不带附件的邮件到QQ邮箱

[root@vm01 shell]#  cat mail_noattachment.sh 
#!/bin/bash 

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

RUNNINGNUM=1

echo -e "`date "+%Y-%m-%d %H:%M:%S"` : The current running $JOB_NAME job num is $RUNNINGNUM in 192.168.137.201 ......" | mail \
-r "From: alertAdmin <${FROM_EMAIL}>" \
-s "Warn: Skip the new $JOB_NAME spark job." ${TO_EMAIL}

[root@vm01 shell]#  sh mail_noattachment.sh 

8、发送一个带附件的邮件到QQ邮箱

[root@vm01 shell]# cat ruozedata.log 
www.baidu.com
www.jd.com
www.taobao.com

[root@vm01 shell]# cat mail_attachment.sh 
#!/bin/bash 

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

LOG=/root/shell/ruozedata.log

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}

[root@vm01 shell]# sh  mail_attachment.sh

你可能感兴趣的:(Shell)