Linux下利用mail命令发送邮件


前言

   mail命令实际是调用sendmail程序包,可以使用sendmail的配置参数; 终端下利用mail发送邮件可以实现报警脚本并达到实时监控预警的功能。

一、环境

Linux下Centos 7

[root@localhost /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@localhost /]# uname -a
Linux localhost 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux


二、方法

1.安装mail程序

已安装mail程序
[root@localhost /]# rpm -qa | grep mail
mailx-12.5-19.el7.x86_64
libreport-plugin-mailx-2.1.11-53.el7.centos.x86_64
mailcap-2.1.41-2.el7.noarch
未安装mail程序
[root@localhost /]# yum -y install mailx

2.关闭其他邮件工具

[root@localhost /]# systemctl stop postfix
[root@localhost /]# systemctl stop sendmail

3.开启SMTP并获得授权码

在任何邮箱中开启对应账号的STMP、POP3、IMAP协议,通过邮箱绑定的手机号发送指定信息给官方,之后会获取到随机的授权码,该授权码用于配置文件/etc/mail.rc中的set smtp-auth-password=xxxx #授权码.

4.获取数字证书

发送邮箱用的是163邮箱,接收邮箱用的是qq邮箱,这里只需获取发送邮箱的数字证书即可.

[root@localhost /]# echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /root/.certs/163.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 CN RSA CA G1
verify return:1
depth=0 C = CN, ST = Zhejiang, L = Hangzhou, O = "NetEase (Hangzhou) Network Co., Ltd", OU = IT Dept., CN = *.163.com
verify return:1
DONE

[root@localhost /]#  certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
[root@localhost /]#  certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
[root@localhost /]#  certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d /root/.certs/./ -i /root/.certs/163.crt
Notice: Trust flag u is set automatically if the private key is present.

[root@localhost /]#  ls /root/.certs/
163.crt  cert8.db  key3.db  secmod.db
[root@localhost /]#  certutil -L -d /root/.certs

Certificate Nickname            Trust Attributes
                                SSL,S/MIME,JAR/XPI
                                
GeoTrust SSL CA                                P,P,P


5.配置/etc/mail.rc

[root@localhost /]# vim /etc/mail.rc
...在配置文件末尾追加如下内容:
set from=xxx1@163.com  #你的邮箱账号
set smtp=smtps://smtp.163.com
set smtp-auth-user=xxx@163.com
set smtp-auth-password=***  #上面获取的授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs  #数字证书目录

6.发送邮件方式

(1)直接使用shell当编辑器

[root@localhost /]# mail xxx@qq.com
Subject: test
123456
.
EOT

(2)管道

[root@localhost /]# echo "hello,liuyan.this is your e-mail"|mail -s "TEST" xxx@qq.com

(3) 文件

[root@localhost /]# mail -s "TEST2" xxx@qq.com < mail.txt

(4) 附件

[root@localhost /]# yum install sharutils
[root@localhost /]# uuencode mail.txt mail |mail -s "TEST3" xxx@qq.com 


三、 异常处理

1、端口处理未打开TCP/UDP的53端口

[root@localhost /]# firewall-cmd --add-port=53/tcp --permanent
success
[root@localhost /]# firewall-cmd --add-port=53/udp --permanent
success

2、网络ping不通,出现IP地址能ping通但域名ping不通,就是因为1、出现的问题




  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一只鱼曼巴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值