Configure Postfix to send mail on Ubuntu Linux server

本文详细介绍了如何在Ubuntu Linux服务器上安装和配置Postfix服务,包括设置SMTP服务器地址、处理邮件身份验证、修改配置文件,以及解决常见问题,如使用假邮件地址发送和显示真实发件人名等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The below is my experience to successfully configure Postfix service on Ubuntu Linux server to send mail via an external SMTP server.

Precondition

You should know the address and port of your SMTP server, whether or not your SMTP server supports to use fake e-mail address to send mail without account/password authentication. Generally, The rule of SMTP server requires real e-mail account.

1. Install

$ sudo apt-get update

$ sudo apt-get install -y mailutils

During installation, you can select the default and enter when asked some questions. If you have already installed Postfix on your Linux server, can skip above steps.

2. Configure Postfix

Postfix (main.cf) is now set up with a default configuration. If you need to make changes, edit /etc/postfix/main.cf (and others) as needed. To view Postfix configuration values, see postconf(1). After modifying main.cf, please be sure to run 'systemctl reload postfix'.

2.1 configure relayhost address in Postfix configuration file

Edit /etc/postfix/main.cf

relayhost = [SMTP_SERVER_IP]  #The default port is 25

Or specifiy SMTP server address and port simultaneously

relayhost = [smtp.example.com]:587

On my Linux server, I use below as my company e-mail account is Microsoft Exchange:

relayhost = [outlook.office365.com]:587

Note: On Ubuntu 22.04 LTS, if encounter below errors:

postfix/local[1110059]: C5B731B03345: to=<your.name@example.com>, relay=local, delay=0.14, delays=0.1/0/0/0.03, dsn=5.1.1, status=bounced (unknown user: "your.name")

Please also comment below line out by prefixing it with a '#'

# mydestination = $myhostname, $myhostname.$mydomain, localhost.$mydomain, localhost

Add the following line

mydestination =

2.2 configure account and password (optional)

Note:  if your SMTP server supports to use fake e-mail address to send mail without account/password authentication, can skip the step 2.2. Bug if seeing below errors in /var/log/mail.log, it indicates you need to configure an account and password to send mail. Please follow below steps to configure.

Client not authenticated to send mail. [SG2PR0601CA0021.apcprd06.prod.outlook.com] (in reply to MAIL FROM command))

1) edit /etc/postfix/main.cf, add below lines

smtp_sasl_auth_enable = yes

smtp_sasl_password_maps = hash:/etc/postfix/sasl/auth_passwd

smtp_sasl_security_options = noanonymous

2) create password and db files

$ sudo vim /etc/postfix/sasl/auth_passwd

insert the following:

[smtp.example.com]:587 your.name@example.com:password

note: please use actual SMTP server address and port, username and password when you try to configure.

create a hash database file for Postfix with the postmap commands

$ sudo postmap /etc/postfix/sasl/auth_passwd

for security, only allow root user to read and write auth_passwd and auth_passwd.db

$ sudo chown root:root /etc/postfix/sasl/auth_passwd /etc/postfix/sasl/auth_passwd.db

$ sudo chmod 0600 /etc/postfix/sasl/auth_passwd /etc/postfix/sasl/auth_passwd.db

2.3 change sender e-mail address in outgoing e-mail

In outgoing e-mail, you can show the address you want others to see. If you do the above step 2.2, must use the real e-mail address used in the step 2.2. If not do the above step 2.2, you can specify any.

1) edit /etc/postfix/main.cf, add below a line

smtp_generic_maps = hash:/etc/postfix/generic

2) create user-email address map and db files

$ sudo vim /etc/postfix/generic

insert the following:

root your.name@example.com

username your.name@example.com

root@hostname your.name@example.com

username@hostname your.name@example.com

note: please use actual username and email address when you try to configure.

create a hash database file for Postfix with the postmap commands

$ sudo postmap /etc/postfix/generic

Note: when seeing below errors in /var/log/mail.log, it means you authenticated using one email address, but tried to send email using another email address for the “FROM” address. we should make them keep alignment.

said: 554 5.2.252 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message.

3. Show real name in mailbox (optional)

Note: If you do the above step 2.2, please skip this step 3.

E-mail from Postfix, you want to show real name instead of Linux account name in mailbox, for example: expect to show "Skye Zhang <your.name@example.com>" not "root <your.name@example.com>". Please follow below step to configure.

Only need to change real user name for root account:

$ sudo chfn -f "Skye Zhang" root

4. Reload Postfix

After modifying main.cf and finishing all configurations, please be sure to run below command to take effect.

$ sudo systemctl reload postfix

or:

$ sudo systemctl restart postfix

5. Test

On Linux server, use mail command to send an email, then check whether or not you can receive below test mail in your mailbox.

$ echo 'Here is the message body.' | mail -s "Test email subject" your.name@example.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值