使用shell发送邮件

1. 生成QQ邮箱登录授权码

在这里插入图片描述
开启SMTP服务,获取授权码
在这里插入图片描述

2. 邮件服务器配置

2.1 关闭sendmail服务

#我这里没有sendmail服务
[root@hadoop614~]# service sendmail status
sendmail: unrecognized service
[root@hadoop614~]# service sendmail stop
sendmail: unrecognized service

2.2 开启postfix服务

[root@hadoop614~]# service postfix status
master is stopped
[root@hadoop614~]# service postfix start
Starting postfix:                                          [  OK  ]
[root@hadoop614 ~]# service postfix status
master (pid  2566)) is running...

2.3 创建证书

[root@hadoop614 ~]# mkdir .certs
[root@hadoop614 ~]# cd .certs/
[root@hadoop614 .certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

[root@hadoop614 .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@hadoop614 .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@hadoop614 .certs]#  certutil -L -d /root/.cert

[root@hadoop614 .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt

2.4 配置mail.rc配置文件

在最后追加一下内容

set from=你的QQ邮箱
set smtp=smtp.qq.com
set smtp-auth-user=你的QQ号
#授权码
set smtp-auth-password=你的授权码
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs

2.5 测试

想自己邮箱发送一条测试邮件

[root@hadoop614 .certs]#  echo hello word | mail -s " title" xxxx@qq.com

在这里插入图片描述

3 发送一个带附件的邮件

[root@hadoop614 shell]# vi mail_attachment.sh 
#!/bin/bash

FROM_EMAIL="1620421126@qq.com"
TO_EMAIL="1620421126@qq.com"

LOG=/root/shell/sh.help

echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the fail sql attachement." | mailx \
-r "From: alertAdmin <${FROM_EMAIL}>" \
-a ${LOG} \
-s "Critical:DSHS fail sql." ${TO_EMAIL}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Shell自动发送邮件可以使用命令行工具如`mail`或`sendmail`,也可以使用脚本语言如Python的smtplib库。下面分别介绍两种方式: 方式一:使用命令行工具 1. 首先,确保你的系统已经安装了mail或sendmail命令行工具。 2. 使用以下命令发送邮件: ``` echo "邮件正文" | mail -s "邮件主题" 收件人邮箱 ``` 或者 ``` echo -e "邮件正文" | sendmail -t 收件人邮箱 ``` 例如,发送一封简单的邮件: ``` echo "这是一封测试邮件" | mail -s "测试邮件" test@example.com ``` 方式二:使用Python脚本 1. 首先,确保你已经安装了Python,并且安装了smtplib库。 2. 创建一个Python脚本,比如`send_email.py`,并添加以下代码: ```python import smtplib from email.mime.text import MIMEText # 邮件参数 sender = '发件人邮箱' receiver = '收件人邮箱' subject = '邮件主题' body = '邮件正文' # 构造邮件内容 msg = MIMEText(body) msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver # 发送邮件 with smtplib.SMTP('smtp.example.com', 25) as server: server.login('发件人邮箱', '密码') server.sendmail(sender, receiver, msg.as_string()) ``` 替换代码中的发件人邮箱、收件人邮箱、邮件主题、邮件正文、SMTP服务器地址和端口号,并且提供发件人邮箱的密码(或者使用授权码)。 3. 运行脚本: ``` python send_email.py ``` 脚本将会发送邮件给指定的收件人。 请注意,在使用该方法时,需要确保你的系统已经正确配置了SMTP服务器,并且能够正常发送邮件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值