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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell中设置定期发送邮件通常使用的是Linux或Unix系统中的`cron`(定时任务)工具,配合`mail`命令或者更现代的方法如`mutt`或`sendmail`。以下是一个基本步骤和示例: 1. **配置cron**: 首先,你需要确保`cron`服务已经启动,并且有权限写入cron文件。在终端中输入: ``` crontab -e ``` 这会打开默认的文本编辑器(通常是`nano`)来编辑用户的cron配置文件。 2. **添加任务**: 在编辑器中添加一个新的行,格式通常是: ```bash * * * * * command-to-run ``` 其中每个星号代表分钟(0-59)、小时(0-23)、日(1-31)、月份(1-12)和星期(0-7,其中0和7都代表周日)。例如,如果你想要每天早上8点发送邮件,你可以这样写: ```bash 0 8 * * * /usr/bin/mail -s "Daily Report" recipient@example.com < report.txt ``` 这里假设`mail`命令在系统的路径下可用,`report.txt`是你要发送邮件内容。 3. **指定邮件内容**: `mail`命令后面跟着邮件的主题 `-s` 和收件人邮箱。如果邮件内容不是来自标准输入(在这里是`report.txt`),你需要提供完整的命令来读取内容。 4. **保存并退出**: 保存并关闭编辑器,一般按`Ctrl+X`,然后`Y`确认保存,再按回车键。 5. **测试**: 添加任务后,你可以立即运行一次任务来检查是否能正确发送邮件。再次运行`crontab -e`,然后在终端查看刚刚添加的任务。 **相关问题:** 1. 如何查看当前的cron任务列表? 2. 如果邮件发送失败,如何调试邮件命令? 3. 如何为`mail`命令配置SMTP服务器以发送非本地邮件? 如果你需要更复杂的邮件发送,比如带有附件或使用更先进的邮件客户端,可以考虑`mutt`或`sendmail`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值