shell之发送qq邮件并附带附件

本文详细介绍了如何生成QQ邮箱的登录授权码,并在服务器上配置Postfix服务以使用SMTP发送邮件。同时,提供了通过Shell脚本发送带有附件的邮件的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.生成qq登录授权码

流程如下图:设置-》账户
1
开启SMTP服务-》生辰授权码
2

2.服务器配置

2.1关闭sendmail的服务
#我这里没有sendmail服务
[root@hadoop001 wsktest]# service sendmail status
sendmail: unrecognized service
[root@hadoop001 wsktest]# service sendmail stop
sendmail: unrecognized service
2.2开启postfix服务
[root@hadoop001 wsktest]# service postfix status
master is stopped
[root@hadoop001 wsktest]# service postfix start
Starting postfix:                                          [  OK  ]
[root@hadoop001 wsktest]# service postfix status
master (pid  5816) is running...
2.3创建证书

如下,可直接粘贴相关命令执行。

[root@hadoop001 wsktest]# mkdir -p /root/.certs/
[root@hadoop001 wsktest]# 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@hadoop001 .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
.crt
cd [root@hadoop001 .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@hadoop001 .certs]# certutil -L -d /root/.certs

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

GeoTrust SSL CA                                              C,,  
[root@hadoop001 .certs]# cd /root/.certs
[root@hadoop001 .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.
[root@hadoop001 .certs]# ll
total 80
-rw-------. 1 root root 65536 Apr 12 03:11 cert8.db
-rw-------. 1 root root 16384 Apr 12 03:11 key3.db
-rw-r--r--. 1 root root  2529 Apr 12 03:09 qq.crt
-rw-------. 1 root root 16384 Apr 12 03:11 secmod.db
2.3修改ail.rc文件

追加/etc/mail.rc内容如下:

#qq账号和授权码我使用了XXX模糊了,实际应该正确填写自己的账号和授权码。

set from=571XXX512@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=57XXX512
#授权码
set smtp-auth-password=biyXXXkwhbefa
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs
2.4测试账号
#向邮箱571XXX512@qq.com发送邮件
[root@hadoop001 .certs]# echo hello word | mail -s " title" 571XXX512@qq.com
#查看邮件服务日志
[root@hadoop001 ~]# tail -30f /var/log/maillog 

3.shell编程向指定邮箱附带附件

3.1发送有内容的邮件
#先将邮件内容编写到mailContext.txt文本中
[root@hadoop001 .certs]# cat mailContext.txt
Hellow,I am skwang2. I am from china,
Nice to meet you.
#发送邮件
[root@hadoop001 .certs]# cat mailContext.txt | mail -s "单点故障问题" skwang2@iflytek.com
3.2发送有附件的邮件

编辑up.sh脚本,-a 表示添加附件

[root@hadoop001 wsktest]# cat up.sh 
#!/bin/bash

if [ $# -lt 2 ]; then
    echo "Error:Missing accessories"
    echo "Useage: up.sh <mail-address> <attachment>"
    exit 1
fi


first=1
attachment=""

for i in $@
do
    if [ $first -eq 1 ]; then
        first=0
    else
        attachment="$attachment  -a  $i "
    fi
done

cat /root/wsktest/mailContext.txt | mail -s $1   $attachment XXXX@iflytek.com

发送带附件的邮件

[root@hadoop001 wsktest]# ll
total 156
-rw-r--r--. 1 root root 73916 Apr 12  2019 1.png
-rw-r--r--. 1 root root 70114 Apr 12  2019 2.png
-rw-r--r--. 1 root root    56 Apr 12 03:37 mailContext.txt
-rw-r--r--. 1 root root   370 Apr 12 03:57 up.sh
[root@hadoop001 wsktest]# sh -x  up.sh "主题:单点故障问题分析" ./1.png ./2.png

查看邮件
3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值