mailx基于465端口发送邮件

需求

阿里云环境,写了一个日志监控脚本,本意通过微信进行推送告警,结果找了很多开源项目,微信都不允许再使用网页的方式登录微信,随即作罢。转而先使用邮件的方式推送告警信息。

实现过程

注意:

  • 阿里云默认禁用25邮件端口,需要启动465端口加密进行邮件发送;
  • 确保邮箱开启SMTP服务,POP3,并申请了密码;
  • 确保已经安装mailx,sendmaill,dos2unix

请求数字证书

mkdir -pv /root/.certs/	# 创建目录用来存放数字证书
echo -n | openssl s_client -connect smtp.126.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/126.crt	# 向126请求数字证书
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/126.crt	# 添加一个证书到证书数据库中
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/126.crt	# 添加一个证书到证书数据库中
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i ~/.certs/126.crt	# 添加一个证书到证书数据库中
certutil -L -d /root/.certs	# 列出目录下证书

开启SMTP/IMAP 授权

记录自己设置的授权码

配置mail.rc

[root@localhost ~]# vim /etc/mail.rc
...
set from=xxx@126.com	# 发送邮件的邮箱
set smtp=smtps://smtp.126.com:465	# 发送邮件邮箱的SMTP服务器
set smtp-auth-user=xxx@126.com	# 邮箱认证的用户名
set smtp-auth-password=123456	# 邮箱认证的用户名
set smtp-auth=login	# smtp验证方式
set ssl-verify=ignore # 忽视 ssl 验证
set nss-config-dir=/root/.certs	# # 证书目录
...

发送邮件验证

### 虽然这里报错了,但是邮件已收到
### 送完邮件还有报错:证书不被信任,且命令行就此卡住,需要按键才能出现命令提示符
mail -s 'passwd' xxxxx@qq.com < /etc/passwd

### 解决办法
cd ~/.certs/
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i 126.crt 

【完】

posted @ 2021-04-10 14:05  hukey  阅读(203)  评论(0编辑  收藏  举报