Exim4 配置外部 SMTP 服务器发邮件
大多监控脚本需要用到邮件通知,通过安装 Exim4 程序,可以快速配置邮件发送环境。比较简单方法是采用外部 SMTP 服务器发送邮件,这样不易被视为垃圾邮件,适合邮件不特别频繁的使用场景。
Exim4 可以在软件包管理器里获取,安装前先更新一遍软件列表。
apt-get update && apt-get install exim4
Ubuntu 系统可能还需额外安装 mailutils 组件。
apt-get install mailutils
安装后,运行 dpkg-reconfigure exim4-config
命令启动配置向导。设置流程如下表格。
选项问题 | 设置操作 |
---|---|
General type of mail configuration: | 选择 mail sent by smarthost; received via SMTP or fetchmail 模式 |
System mail name: | 输入本机邮箱地址域名,这里填写 localhost.localdomain 检查 /etc/hosts 文件 127.0.0.1 设置内容,应设置为:127.0.0.1 localhost.localdomain localhost 127.0.0.1 主机名 |
IP-addresses to listen on for incoming SMTP connections: | 使用默认值 127.0.0.1 |
Other destinations for which mail is accepted: | 设置收件人域名列表,填写 localhost.localdomain |
Machines to relay mail for: | 留空不设置 |
IP address or host name of the outgoing smarthost: | 设置发送邮件的 SMTP 服务器地址及端口 例如 smtp.qq.com::587 (注意是两个冒号) |
Hide local mail name in outgoing mail? | 选择 No |
Keep number of DNS-queries minimal (Dial-on-Demand)? | 选择 No |
Delivery method for local mail: | 选择 mbox format in /var/mail/ |
Split configuration into small files? | 选择 Yes |
编辑 /etc/exim4/passwd.client
文件添加邮箱信息,格式为:SMTP 服务器 : 邮箱帐户 : 登录密码。为避免 SMTP 域名地址别名问题影响连接,可以配合通配符星号使用。
*.qq.com:username@qq.com:password
编辑 /etc/email-addresses
文件添加邮箱地址重写,使发出去的邮件显示指定邮箱地址。
下面以 root 用户帐户设置为例。
root: username@qq.com root@localhost: username@qq.com root@localhost.localdomain: username@qq.com root@主机名: username@qq.com
完成上面设置后,重启服务刷新 Exim4 配置文件生效。
update-exim4.conf invoke-rc.d exim4 restart exim4 -qff
最后,测试邮件能否正常发送。如果遇到问题,检查 tail /var/log/exim4/mainlog
日志文件。
echo "This is a test mailing" | mail -s "Test mail" mail@example.com