本页内容

  • 1 Postfix 版本
  • 2 设置 maximal_queue_lifetime 和 delay_warning_time
  • 3 创建自定义退信消息
  • 4 链接

从 Postfix 2.3 版本起,Postfix 支持自定义退信消息。本指南介绍如何设置自定义 Postfix 退信消息以及最大队列生存时间。

1 Postfix 版本

首先,你应该查明你的 Postfix 版本,以确认它支持自定义退信消息:

postconf -d | grep mail_version
server2:~# postconf -d | grep mail_version
mail_version = 3.4.13
milter_macro_v = $mail_name $mail_version
server2:~#

如果你的 Postfix 是 2.3 或更高版本,那么就可以继续了。

2 设置 maximal_queue_lifetime 和 delay_warning_time

来自 http://www.postfix.org/postconf.5.html :

maximal_queue_lifetime :消息在被作为无法投递而退回之前,可在队列中停留的最长时间。

delay_warning_time :在此时间之后,发件人会收到仍在队列中的邮件的消息头。

postconf -n 命令显示当前在 /etc/postfix/main.cf 中配置的设置,而 postconf -d 命令显示默认设置,除非在 /etc/postfix/main.cf 中另行设置,否则这些默认设置生效。

要查看 maximal_queue_lifetime 的当前值,你可以运行

postconf -d | grep maximal_queue_lifetime
postconf -n | grep maximal_queue_lifetime

如果 postconf -n 没有显示任何内容,这意味着当前使用的是 postconf -d 中的值:

server2:~# postconf -d | grep maximal_queue_lifetime
maximal_queue_lifetime = 5d
server2:~# postconf -n | grep maximal_queue_lifetime
server2:~#

delay_warning_time 同理:

postconf -d | grep delay_warning_time
postconf -n | grep delay_warning_time
server2:~# postconf -d | grep delay_warning_time
delay_warning_time = 0h
server2:~# postconf -n | grep delay_warning_time
server2:~#

如果你想修改这些设置,可以使用 postconf -e 命令。它会将设置写入 /etc/postfix/main.cf ,例如像这样:

postconf -e 'maximal_queue_lifetime = 1d'
postconf -e 'delay_warning_time = 0h'

之后重启 Postfix:

service postfix restart

我们关注这两个设置的原因是,它们的值可以在自定义退信消息中使用。

3 创建自定义退信消息

来自 http://www.postfix.org/bounce.5.html :

模板文件可以为投递失败的邮件、延迟的邮件、成功投递或地址验证指定模板。这些模板分别命名为 failure_template、delay_template、success_template 和 verify_template。你可以在退信模板文件中指定全部四个模板,但并非必须。

每个模板以 "template_name = <

现在我们可以创建文件 /etc/postfix/bounce.cf ,其中包含类似这样的模板(这里我使用了全部四个模板,但你可以省略不需要的模板)。文件必须以一个空行结尾,这一点绝对重要!

nano /etc/postfix/bounce.cf
#
# The failure template is used when mail is returned to the sender;
# either the destination rejected the message, or the destination
# could not be reached before the message expired in the queue.
#

failure_template = <<EOF
Charset: us-ascii
From: MAILER-DAEMON (Mail Delivery System)
Subject: Undelivered Mail Returned to Sender
Postmaster-Subject: Postmaster Copy: Undelivered Mail

This is the mail system at host $myhostname.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to <postmaster>

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

                   The mail system
EOF


#
# The delay template is used when mail is delayed. Note a neat trick:
# the default template displays the delay_warning_time value as hours
# by appending the _hours suffix to the parameter name; it displays
# the maximal_queue_lifetime value as days by appending the _days
# suffix.
#
# Other suffixes are: _seconds, _minutes, _weeks. There are no other
# main.cf parameters that have this special behavior.
#
# You need to adjust these suffixes (and the surrounding text) if
# you have very different settings for these time parameters.
#

delay_template = <<EOF
Charset: us-ascii
From: MAILER-DAEMON (Mail Delivery System)
Subject: Delayed Mail (still being retried)
Postmaster-Subject: Postmaster Warning: Delayed Mail

This is the mail system at host $myhostname.

####################################################################
# THIS IS A WARNING ONLY.  YOU DO NOT NEED TO RESEND YOUR MESSAGE. #
####################################################################

Your message could not be delivered for more than $delay_warning_time_hours hour(s).
It will be retried until it is $maximal_queue_lifetime_days day(s) old.

For further assistance, please send mail to <postmaster>

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

                   The mail system
EOF


#
# The success template is used when mail is delivered to mailbox,
# when an alias or list is expanded, or when mail is delivered to a
# system that does not announce DSN support. It is an error to specify
# a Postmaster-Subject: here.
#

success_template = <<EOF
Charset: us-ascii
From: MAILER-DAEMON (Mail Delivery System)
Subject: Successful Mail Delivery Report

This is the mail system at host $myhostname.

Your message was successfully delivered to the destination(s)
listed below. If the message was delivered to mailbox you will
receive no further notifications. Otherwise you may still receive
notifications of mail delivery errors from other systems.

                   The mail system
EOF


#
# The verify template is used for address verification (sendmail -bv
# address...). or for verbose mail delivery (sendmail -v address...).
# It is an error to specify a Postmaster-Subject: here.
#

verify_template = <<EOF
Charset: us-ascii
From: MAILER-DAEMON (Mail Delivery System)
Subject: Mail Delivery Status Report

This is the mail system at host $myhostname.

Enclosed is the mail delivery report that you requested.

                   The mail system
EOF

你可以按自己的喜好自定义这些消息。在消息中,你可以使用所有 main.cf 变量(例如 $myhostname )。如果你看一下 delay_template ,就会看到我使用了额外的变量 $delay_warning_time_hours 和 $maximal_queue_lifetime_days 。你也可以使用 $delay_warning_time_seconds、$delay_warning_time_minutes、$delay_warning_time_days、$delay_warning_time_weekes 以及 $maximal_queue_lifetime_seconds、$maximal_queue_lifetime_minutes、$maximal_queue_lifetime_hours、$maximal_queue_lifetime_weeks ,但请记住 http://www.postfix.org/bounce.5.html 对这些变量的说明:

delay_warning_time_suffix:展开为 delay_warning_time 参数的值,以后缀所指定的时间单位表示,后缀可以是 seconds、minutes、hours、days 或 weeks 之一。

maximal_queue_lifetime_suffix:展开为 maximal_queue_lifetime 参数的值,以后缀所指定的时间单位表示。可能的后缀值参见上文 delay_warning_time 部分。

因此,如果你在模板中使用变量 $delay_warning_time_minutes 而不是 $delay_warning_time_hours ,就应在其后跟上单词 "minutes" 而不是 "hours"。

接下来,我们配置 Postfix 使用自定义模板:

postconf -e 'bounce_template_file = /etc/postfix/bounce.cf'

要检查当所有变量都被替换为真实值时模板的样子,并确认模板中没有错误(例如 /etc/postfix/bounce.cf 末尾缺少换行符),请运行:

postconf -b /etc/postfix/bounce.cf
server2:~# postconf -b /etc/postfix/bounce.cf
expanded_failure_text = <<EOF
This is the mail system at host server2.example.com.
I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.
For further assistance, please send mail to <postmaster>
If you do so, please include this problem report. You can
delete your own text from the attached returned message.
                   The mail system
EOF
expanded_delay_text = <<EOF
This is the mail system at host server2.example.com.
####################################################################
# THIS IS A WARNING ONLY.  YOU DO NOT NEED TO RESEND YOUR MESSAGE. #
####################################################################
Your message could not be delivered for more than 0 hour(s).
It will be retried until it is 1 day(s) old.
For further assistance, please send mail to <postmaster>
If you do so, please include this problem report. You can
delete your own text from the attached returned message.
                   The mail system
EOF
expanded_success_text = <<EOF
This is the mail system at host server2.example.com.
Your message was successfully delivered to the destination(s)
listed below. If the message was delivered to mailbox you will
receive no further notifications. Otherwise you may still receive
notifications of mail delivery errors from other systems.
                   The mail system
EOF
expanded_verify_text = <<EOF
This is the mail system at host server2.example.com.
Enclosed is the mail delivery report that you requested.
                   The mail system
EOF
server2:~#

如果没有显示错误,我们就可以重启 Postfix,使其能够使用自定义模板:

service postfix restart

4 链接

  • Postfix bounce 手册页:http://www.postfix.org/bounce.5.html
  • Postfix 配置参数:http://www.postfix.org/postconf.5.html