Shell 脚本配置发送QQ邮件

文章目录

    • 方法一
      • 1进入QQ邮箱网页界面客服端
      • 2用root用户,执行脚本
    • 方法二:

方法一

1进入QQ邮箱网页界面客服端

①点击设置 ====》点击账户
Shell 脚本配置发送QQ邮件_第1张图片
② 开启SMTP服务 ====》生成授权码
Shell 脚本配置发送QQ邮件_第2张图片

2用root用户,执行脚本

#!/bin/bash

. /etc/init.d/functions

if [ $# -ne 0 ];then
    echo "Usage: sh `basename $0`"
    exit 1
fi

frommail='[email protected]'
smtp_host='smtp.qq.com'
smtp_auth_user='[email protected]'
smpt_auth_password='kgnydypviahxidda'
certs_dir=/root/.certs
mail_conf=/etc/mail.rc


content=`cat $mail_conf |grep 'smtp-auth-user'`
if [ "x$content" != "x"  ];then
   action  "'$mail_conf' 文件已经配置" /bin/true
   exit 1
fi


cat >> $mail_conf <<EOF 
set from=$frommail
set smtp=$smtp_host
set smtp-auth-user=$smtp_auth_user
set smtp-auth-password=$smpt_auth_password
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=$certs_dir
EOF

[ $? -eq 0  ] && action "配置$mail_conf文件" /bin/true

[ ! -d $certs_dir ] && mkdir -p $certs_dir
echo -n | openssl s_client -connect $smtp_host:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $certs_dir/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d $certs_dir -i $certs_dir/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d $certs_dir -i $certs_dir/qq.crt
certutil -L -d $certs_dir

[ $? -eq 0  ] && action "生成证书" /bin/true

执行

[root@flink01 ~]# sh auto_mail_config.sh 
配置/etc/mail.rc文件                                       [  OK  ]
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
verify return:1
depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = GeoTrust RSA CA 2018
verify return:1
depth=0 C = CN, ST = Guangdong, L = Shenzhen, O = Tencent Technology (Shenzhen) Company Limited, OU = R&D, CN = pop.qq.com
verify return:1
DONE

Certificate Nickname                                         Trust Attributes
                                                             SSL,S/MIME,JAR/XPI

GeoTrust SSL CA                                              C,,  
生成证书                                                   [  OK  ]

发送邮件

[root@flink01 ~]# echo  hello kzw | mailx -v -s " title" 1937859795@qq.com

最后到邮箱查收,成功收到邮件!
Shell 脚本配置发送QQ邮件_第3张图片

方法二:

①与方法一中的第一步还是一样,必不可少,打开服务协议,生成验证码
②停止发邮件服务

[root@hadoop002 ~]service sendmail stop
sendmail: unrecognized service
[root@hadoop002 ~]chkconfig sendmail off

③开启postfix服务

[root@hadoop002  ~]# service postfix status
master is stopped
[root@hadoop002  ~]# service postfix start
Starting postfix:                                          [  OK  ]
[root@hadoop002  ~]#service postfix status
Redirecting to /bin/systemctl status  postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-08-23 15:42:20 CST; 47min ago
  Process: 3102 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 3099 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 3094 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 3174 (master)

④,修改 inet_interfaces(记得先关闭防火墙)
注意参数看仔细,是inet_interfaces 而不是inet_interface

vi /etc/postfix/main.cf
inet_interfaces = all
inet_protocols = ipv4

⑤创建认证 (直接拷贝命令执行即可,这里顺便贴出执行成功的效果)

[root@hadoop002  ~]# mkdir -p /root/.certs/
[root@hadoop002 .certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
verify return:1
depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = GeoTrust RSA CA 2018
verify return:1
depth=0 C = CN, ST = Guangdong, L = Shenzhen, O = Tencent Technology (Shenzhen) Company Limited, OU = R&D, CN = pop.qq.com
verify return:1
DONE
[root@hadoop002 .certs]# ll
total 64
-rw------- 1 root root 65536 Aug 23 16:13 cert8.db
-rw------- 1 root root 16384 Aug 23 16:13 key3.db
-rw-r--r-- 1 root root  2529 Aug 23 16:14 qq.crt
-rw------- 1 root root 16384 Aug 23 15:49 secmod.db
[root@hadoop002 .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@hadoop002 .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@hadoop002 .certs]# certutil -L -d ~/.certs

Certificate Nickname                                         Trust Attributes
                                                             SSL,S/MIME,JAR/XPI

GeoTrust SSL CA                                              C,,  
[root@hadoop002 .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
Notice: Trust flag u is set automatically if the private key is present.

⑥配置邮件发送读者,修改mail.rc参数,末尾直接添加
vi /etc/mail.rc

set [email protected]
set smtp=smtp.qq.com
set smtp-auth-user=1937859795
#授权码(第一步生成的授权码)
set smtp-auth-password=sokiolestmlzdddf
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs

⑦,发送一个测试邮件 (后面的邮箱可以填任意收件人邮箱)

echo hello email| mail -s " title" 1937859795@qq.com

Shell 脚本配置发送QQ邮件_第4张图片
发送有内容的邮件
cat filename| mail -s " title" [email protected]

⑧发送打附件的邮件

[root@hadoop002 .certs]#echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the fail sql attachement." |
mailx \
-r "From: alertAdmin <[email protected]>" \
-a qq.crt \
-s "Critical:KSSH fail sql." 1937859795@qq.com

Shell 脚本配置发送QQ邮件_第5张图片

你可能感兴趣的:(Shell)