如何配置自定义Postfix跳转消息

如何配置自定义Postfix跳转消息 从Postfix版本2.3,Postfix支持自定义退信消息。 本指南介绍如何配置...

如何配置自定义Postfix反弹消息

版本1.0
作者:Falko Timme

Postfix版本2.3后,Postfix支持自定义退回邮件。 本指南介绍如何配置自定义Postfix反弹消息。

我不会保证这将为您工作!

1 Postfix版本

首先,您应该了解您的Postfix版本,以确保它支持自定义退回邮件:

postconf -d | grep mail_version

server2:~# postconf -d | grep mail_version
mail_version = 2.3.8
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:

/etc/init.d/postfix restart

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

3创建自定义退回邮件

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

模板文件可以指定失败邮件,延迟邮件,成功交付或地址验证的模板。 这些模板分别命名为fail_template,delay_template,success_template和verify_template。 您可以但不必在反弹模板文件中指定所有四个模板。

每个模板以“template_name = << EOF”开始,并以包含单词“EOF”的行结尾

我们现在可以创建文件/etc/postfix/bounce.cf ,其中包含这样的模板(我在这里使用所有四个模板,但是您可以省略不需要的模板)。 文件以空行结尾是绝对重要的!

vi /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参数的值,以Postfix指定的时间单位表示,以秒,分,小时,天或周为单位。

maximal_queue_lifetime_suffix:扩展为maximal_queue_lifetime参数的值,以Postfix指定的时间单位表示。 有关可能的Postfix值,请参见delay_warning_time上面的内容。

因此,如果您在模板中使用变量$ delay_warning_time_minutes而不是$ delay_warning_time_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,以便它可以使用自定义模板:

/etc/init.d/postfix restart