【shell脚本实现带附件邮件】

环境CentOS

使用mail命令发送邮件需要先配置发送邮件的邮箱,以QQ邮箱为例获取授权码

第一步安装依赖

[root@hecs-38553 ~]# yum -y install sendmail
Last metadata expiration check: 1:08:19 ago on Tue 13 Dec 2022 03:11:00 PM CST.
Dependencies resolved.
============================================================================================================================================================================================
 Package                                      Architecture                             Version                                            Repository                                   Size
============================================================================================================================================================================================
Installing:
 sendmail                                     x86_64                                   8.15.2-34.el8                                      AppStream                                   768 k
Installing dependencies:
 cyrus-sasl                                   x86_64                                   2.1.27-5.el8                                       base                                         96 k

Transaction Summary
============================================================================================================================================================================================
Install  2 Packages

Total download size: 864 k
Installed size: 1.8 M
Downloading Packages:
(1/2): cyrus-sasl-2.1.27-5.el8.x86_64.rpm                                                                                                                   205 kB/s |  96 kB     00:00    
(2/2): sendmail-8.15.2-34.el8.x86_64.rpm                                                                                                                    201 kB/s | 768 kB     00:03    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                       226 kB/s | 864 kB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                    1/1 
  Running scriptlet: cyrus-sasl-2.1.27-5.el8.x86_64                                                                                                                                     1/2 
  Installing       : cyrus-sasl-2.1.27-5.el8.x86_64                                                                                                                                     1/2 
  Running scriptlet: cyrus-sasl-2.1.27-5.el8.x86_64                                                                                                                                     1/2 
  Running scriptlet: sendmail-8.15.2-34.el8.x86_64                                                                                                                                      2/2 
  Installing       : sendmail-8.15.2-34.el8.x86_64                                                                                                                                      2/2 
  Running scriptlet: sendmail-8.15.2-34.el8.x86_64                                                                                                                                      2/2 
  Verifying        : cyrus-sasl-2.1.27-5.el8.x86_64                                                                                                                                     1/2 
  Verifying        : sendmail-8.15.2-34.el8.x86_64                                                                                                                                      2/2 

Installed:
  cyrus-sasl-2.1.27-5.el8.x86_64                                                                sendmail-8.15.2-34.el8.x86_64                                                               

Complete!
[root@hecs-38553 ~]# yum install -y mailx
Last metadata expiration check: 1:11:32 ago on Tue 13 Dec 2022 03:11:00 PM CST.
Package mailx-12.5-29.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

安装完后启动sendmail命令查询运行状态

[root@hecs-38553 ~]# service sendmail start
Redirecting to /bin/systemctl start sendmail.service
[root@hecs-38553 ~]# service sendmail status
Redirecting to /bin/systemctl status sendmail.service
● sendmail.service - Sendmail Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/sendmail.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-12-13 16:21:55 CST; 42min ago
  Process: 620594 ExecStart=/usr/sbin/sendmail -bd $SENDMAIL_OPTS $SENDMAIL_OPTARG (code=exited, status=0/SUCCESS)
  Process: 620586 ExecStartPre=/etc/mail/make aliases (code=exited, status=0/SUCCESS)
  Process: 620585 ExecStartPre=/etc/mail/make (code=exited, status=0/SUCCESS)

设置发件人信息

[root@hecs-38553 ~]# vim /etc/mail.rc

#mail
set from=xxxxxx@qq.com   #显示的发件人邮箱
set smtp=smtps://smtp.qq.com:465  #邮件服务器
set smtp-auth-user=xxxxx@qq.com    #发件人邮箱账号
set smtp-auth-password=lnayvxtpzrmwbebg  #发件人邮箱授权码
set smtp-auth=login         #登录

发送邮件

  1. 通过文件内容发送
    mail -s ‘mail test’ xxx@yyy.com < con.txt
  2. 通过管道符直接发送
    echo “this is my test mail” | mail -s ‘mail test’ xxx@yyy.com
  3. 带附件发送
    echo “this is my test mail” | mail -a /root/client.c -s ‘mail test’ xxx@qq.com

从QQ邮箱获取SSL证书,并在CentOS中设置

#存放证书的位置
$ ~ mkdir -p /root/.certs
#获取QQ 邮箱的 SSL 证书
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

#添加第一个证书到证书数据库中
certutil -A -n "GeoTrust SSL CA" -t "C,,"  -d  ~/.certs  -i  ~/.certs/qq.crt
#添加第二个证书到证书数据库中
certutil -A -n "GeoTrust Global CA" -t "C,,"  -d  ~/.certs  -i  ~/.certs/qq.crt

#列出指定的目录下的所有证书
certutil -L -d /root/.certs
#如果出现Error in certificate: Peer's certificate issuer is not recognized.执行下面命令
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/qq.crt
我遇到的错误提示Error in certificate: Peer's certificate issuer has been marked as not trusted by the.解决办法
$  certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/qq.crt
Notice: Trust flag u is set automatically if the private key is present.

邮箱查看

在这里插入图片描述

后续可以用shell脚本文件的形式一键发送定时发送

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Quieeeet

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

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

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

打赏作者

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

抵扣说明:

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

余额充值