Ubuntu 18.04 中使用 Postfix 发送邮件

Postfix

之前介绍过使用 Linux 自带的 mail 来发送邮件 Linux使用mailx通过第三方SMTP发送邮件,带附件操作

最近发现 Ubuntu 18.04 中移除了 heirloom-mail1

Postfix is the default Mail Transfer Agent (MTA) in Ubuntu. It attempts to be fast and secure, with flexibility in administration. It is compatible with the MTA sendmail. This section will explain installation, including how to configure SMTP for secure communications.

可见 Postfix 是官方的邮件MTA,工具本身也很好用。因此将此作为新的邮件发送工具吧。

如果机器上没有这个软件的话,安装即可。

sudo apt-get update
sudo apt-get install libsasl2-modules
sudo apt install postfix

安装过程中会弹出配置界面,选择 Internet Site
System mail name 可以随便填一个,可以填你邮箱名后缀,如 163.com

配置2

安装完成后,其配置文件为 /etc/postfix/main.cf
打开配置文件,增加一个字段,myhostname = xxx@xxx.com
自然地,需要用户名和密码,SMTP服务器需要这个来鉴权。

  • 添加用户名密码 sudo vim /etc/postfix/sasl_passwd
// 格式如下,注意以下的 [] 都不需要在文本中体现
[smtp.example.com] username:password

// 如果需要指定端口号,例如 587
[smtp.example.com]:587 username:password
  • 创建上述用户名密码的 hash data base 文件
// 创建 hash 文件
sudo postmap /etc/postfix/sasl_passwd

操作成功的话,此时会在目录下出现 sasl_passwd.db

  • 更改文件权限
// 安全起见,更改文件权限
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

使用外部 SMTP 代理进行发送

使用外部代理发送邮件的一个益处是避免被当成垃圾邮件。外部邮件发送代理很多,比如 163, foxmail, gmail

  • 配置外部 SMTP 代理, sudo vim /etc/postfix/main.cf
    继续编辑配置文件,指定字段内容 relayhost = [smtp.example.com]:587。注意如果 sasl_passwd 文件里指定了端口号,那么这里需要保持一致。

  • 在文件尾部增加鉴权操作

# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
  • 重启 Postfix 服务
    sudo service postfix restart
    or
    sudo systemctl restart postfix.service

测试 SMTP 服务

我们发一份邮件给自己来测试一下配置的正确性,使用 mail 命令来发送。sudo apt-get install mailutils

echo "Hello, this is George, how are you?" | mail -s "From Ubuntu 18.04" -a "From: sender@xxx.com" recipient@xxx.com -A your_attachment_file

可以看到,发送成功了。
在这里插入图片描述

问题定位

发送的日志,错误日志可以在 /var/log/mail.log 以及 /var/log/mail.err 中查看。

更详尽的配置和原理请查阅3


  1. https://askubuntu.com/questions/1034112/why-was-heirloom-mailx-deleted-from-ubuntu-18-04 ↩︎

  2. https://www.linode.com/docs/email/postfix/postfix-smtp-debian7/#examples-of-postfix-configurations-with-different-providers ↩︎

  3. http://www.postfix.org/documentation.html ↩︎

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值