mail简单介绍及服务搭建

本文介绍了邮件服务的基础知识,包括pop3和smtp协议,详细阐述了如何配置和安装邮件传送代理MTA如postfix,以及邮件发送工具mailx。在配置部分提到了需要在个人邮箱开启smtp服务并获取授权码。最后,文章讲解了如何通过脚本统一发送邮件和发送不同内容给同一收件人的方法。

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文件中每行代表一个邮件

------------------------------------------------------------------------------------------------------- 返回目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值