以下配置是只发送邮件的,如果需要接受邮件。需要额外做单独的配置。 如果修改了配置报错,最好删了相关配置文件,重新安装一遍
安装
sudo apt-get update
sudo apt-get install postfix mailutils
安装过程中会弹出一个配置窗口,选择“Internet Site”
。其他配置可以一键确认,不会影响到发送邮件。
如果没有弹出来的话(已经安装过了)。执行 sudo dpkg-reconfigure postfix
会再次弹出来。
配置 SASL
touch /etc/postfix/sasl/sasl_passwd # 创建文件
填充以下内容
# 邮箱 : 密钥
[smtp.gmail.com]:587 xx@gmail.com:xxx_secret
Create a hash database file
sudo postmap /etc/postfix/sasl/sasl_passwd
sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
修改配置文件 /etc/postfix/main.cf
# 修改以下字段
mydestination = $myhostname, localhost, localhost.localdomain
relayhost = [smtp.gmail.com]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
# 将以下行添加到文件末尾:
# Enable SASL authentication
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
重载配置
systemctl restart postfix
发送邮件
echo "Test email" | mail -s "Test email subject" xx@gmail.com
问题排查
日志
tail /var/log/mail.log
less /var/log/mail.log
清理环境后重装
sudo apt-get remove --purge sendmail*
sudo apt-get remove --purge postfix*
sudo apt-get remove --purge dovecot*
sudo apt-get autoremove
参考:
- https://tonyteaches.tech/postfix-gmail-smtp-on-ubuntu/
完整配置如下,需要留意注释的地方
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2
# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=encrypt
# smtp_tls_wrappermode=yes ###### 这个打开后会导致 报错 routines:ssl3_get_record:wrong version number:../ssl/record/ssl3_record.c:331:
smtp_tls_CApath=/etc/ssl/certs
# smtp_tls_security_level=encrypt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
# smtpd_sasl_auth_enable=yes # new add
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = vultr.guest
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, localhost, localhost.localdomain
relayhost = [smtp.gmail.com]:587
mynetworks = 0.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
# Enable SASL authentication
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Gmail
gmail 配置 smtp https://west2.cn/2315.html