Shell 脚本配置发送QQ邮件

本文详细介绍了两种使用QQ邮箱在Linux环境下配置邮件发送的方法。方法一通过编写并执行Shell脚本来配置邮件服务,方法二利用Postfix服务进行邮件发送配置,并生成及验证SSL证书。

方法一

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

①点击设置 ====》点击账户
第一步
② 开启SMTP服务 ====》生成授权码
在这里插入图片描述

2用root用户,执行脚本

#!/bin/bash

. /etc/init.d/functions

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

frommail='1937859795@qq.com'
smtp_host='smtp.qq.com'
smtp_auth_user='1937859795@qq.com'
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

最后到邮箱查收,成功收到邮件!
在这里插入图片描述

方法二:

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

[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 from=1937859795@qq.com
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

在这里插入图片描述
发送有内容的邮件
cat filename| mail -s " title" 1937859795@qq.com

⑧发送打附件的邮件

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

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬瓜螺旋雪碧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值