1. mail介绍
1.1 mail简单介绍
(1)相关协议
pop3:post office protocol 相当于邮局,存放邮件的地方,目前常用的一种协议
smtp:简单邮件传输协议,用来投递邮件,相当于邮差
smtp端口:25 仅用来实现邮件路由和传递
1.2 mail相关命令及管理
使用mail发邮件时,必须先将sendmail服务启动。
mail –s “邮件主题” –c”抄送地址” –b “密送地址” -- -f 发送人邮件地址 –F 发件人姓名 < 要发送的邮件内容
三种常用格式发信
mail -s test nebula@nebula.edu.cn #第一种方法,你可以把当前shell当成编辑器来用,编辑完内容后 Ctrl-D结束
echo “mail content”|mail -s test nebula@nebula.edu.cn #第二种方法,使用管道的缘故
mail -s test nebula@nebula.edu.cn < file #第三种方法,以file的内容为邮件内容发信
2. mail配置
2.1 安装sendmail或者postfix(邮件传送代理MTA)
说明:此处使用postfix
[root@localhost ~]# rpm -qa postfix
postfix-2.10.1-7.el7.x86_64
[root@localhost ~]# systemctl start postfix
[root@localhost ~]# systemctl status postfix
Active: active (running) since 二 2020-11-10 15:15:44 CST; 6h ago
[root@localhost ~]# systemctl enable postfix
2.2 安装邮件发送工具mailx
[root@localhost ~]# yum install mailx
2.3 配置mail
说明:此处的smtp服务需要去个人邮箱开通pop3或者smtp服务,邮箱会提供一个授权码
[root@localhost ~]# vim /etc/mail.rc
set sendcharsets=iso-8859-1,utf-8
set from=xxxxx@126.com #注册的个人邮箱,用于zabbix服务端转发邮件到指定邮箱
set smtp=smtp.126.com
set smtp-auth-user=xxxxxx@126.com #个人邮箱账户
set smtp-auth-password=xxxx #开通邮箱pop3时的授权码
set smtp-auth=login
set sll-verify=ignore
set nss-config-dir=/etc/pki/nssdb
2.4 测试
[root@localhost ~]# echo "hello"|mail -s "test" xxx@126.com
[root@localhost ~]# echo "hello"|mail -s "test" xxx@qq.com
3. 编写脚本
(1)统一发送同一邮件
[root@localhost ~]# vim mail.sh
#!/bin/bash
File= # 用于存放发送内容的文件
Mail_file= # 用于存放收件人邮箱名单的文件
Topic= # 邮件主题
while read line
do
mail -s $Topic $line < $File
done < $Mail_file
[root@localhost ~]# sh mail.sh &
(2)发送不同内容邮件,同一收件人
[root@localhost ~]# vim mail.sh
#!/bin/bash
File= # 用于存放发送内容的文件
Mail_address= # 收件人邮箱地址
Topic= # 邮件主题
while read line
do
echo $line | mail -s $Topic $Mail_address
done < $File
[root@localhost ~]# sh mail.sh &
# 注意,此时是File文件中每行代表一个邮件
------------------------------------------------------------------------------------------------------- 返回目录