Last modified by Jean Franco on 2022/10/30 16:16

Hide last authors
Jean Franco 1.1 1 First you should find out about your Postfix version to make sure it supports custom bounce messages:
2
3 {{{postconf -d | grep mail_version}}}
4
5 {{{server2:~# postconf -d | grep mail_version
6 mail_version = 3.4.13
7 milter_macro_v = $mail_name $mail_version
8 server2:~#}}}
9
10 If your Postfix is 2.3 or newer, then you're good to go.
11
12 === 2 Set maximal_queue_lifetime And delay_warning_time ===
13
14 From [[http:~~/~~/www.postfix.org/postconf.5.html>>url:http://www.postfix.org/postconf.5.html]]:
15
16 maximal_queue_lifetime: The maximal time a message is queued before it is sent back as undeliverable.
17
18 delay_warning_time: The time after which the sender receives the message headers of mail that is still queued.
19
20 The postconf -n command shows the settings that are currently configured in /etc/postfix/main.cf, whereas the postconf -d command shows the default settings that are valid unless something else is set in /etc/postfix/main.cf.
21
22 To find out about the current value of maximal_queue_lifetime, you can run
23
24 {{{postconf -d | grep maximal_queue_lifetime
25 postconf -n | grep maximal_queue_lifetime}}}
26
27 If postconf -n doesn't display anything, this means the value from postconf -d is currently being used:
28
29 {{{server2:~# postconf -d | grep maximal_queue_lifetime
30 maximal_queue_lifetime = 5d
31 server2:~# postconf -n | grep maximal_queue_lifetime
32 server2:~#}}}
33
34 The same goes for delay_warning_time:
35
36 {{{postconf -d | grep delay_warning_time
37 postconf -n | grep delay_warning_time}}}
38
39 {{{server2:~# postconf -d | grep delay_warning_time
40 delay_warning_time = 0h
41 server2:~# postconf -n | grep delay_warning_time
42 server2:~#}}}
43
44 If you want to modify these settings, you can use the postconf -e command. It will write the settings to /etc/postfix/main.cf, e.g. like this:
45
46 {{{postconf -e 'maximal_queue_lifetime = 1d'
47 postconf -e 'delay_warning_time = 0h'}}}
48
49 Restart Postfix afterwards:
50
51 {{{service postfix restart}}}
52
53 The reason that we care about these two settings is that their values can be used in the custom bounce messages.
54
55 === 3 Create A Custom Bounce Message ===
56
57 From [[http:~~/~~/www.postfix.org/bounce.5.html>>url:http://www.postfix.org/bounce.5.html]]:
58
59 //The template file can specify templates for failed mail, delayed mail, successful delivery or for address verification. These templates are named failure_template, delay_template, success_template and verify_template, respectively. You can but do not have to specify all four templates in a bounce template file.//
60
61 //Each template starts with "template_name = <<EOF" and ends with a line that contains the word "EOF"//
62
63 We can now create the file /etc/postfix/bounce.cf which contains the templates like this (I'm using all four templates here, but you can leave out the ones you don't need). It is absolutely important that the file ends with an empty line!
64
65 {{{nano /etc/postfix/bounce.cf}}}
66
67 {{{#
68 # The failure template is used when mail is returned to the sender;
69 # either the destination rejected the message, or the destination
70 # could not be reached before the message expired in the queue.
71 #
72
73 failure_template = <<EOF
74 Charset: us-ascii
75 From: MAILER-DAEMON (Mail Delivery System)
76 Subject: Undelivered Mail Returned to Sender
77 Postmaster-Subject: Postmaster Copy: Undelivered Mail
78
79 This is the mail system at host $myhostname.
80
81 I'm sorry to have to inform you that your message could not
82 be delivered to one or more recipients. It's attached below.
83
84 For further assistance, please send mail to <postmaster>
85
86 If you do so, please include this problem report. You can
87 delete your own text from the attached returned message.
88
89 The mail system
90 EOF
91
92
93 #
94 # The delay template is used when mail is delayed. Note a neat trick:
95 # the default template displays the delay_warning_time value as hours
96 # by appending the _hours suffix to the parameter name; it displays
97 # the maximal_queue_lifetime value as days by appending the _days
98 # suffix.
99 #
100 # Other suffixes are: _seconds, _minutes, _weeks. There are no other
101 # main.cf parameters that have this special behavior.
102 #
103 # You need to adjust these suffixes (and the surrounding text) if
104 # you have very different settings for these time parameters.
105 #
106
107 delay_template = <<EOF
108 Charset: us-ascii
109 From: MAILER-DAEMON (Mail Delivery System)
110 Subject: Delayed Mail (still being retried)
111 Postmaster-Subject: Postmaster Warning: Delayed Mail
112
113 This is the mail system at host $myhostname.
114
115 ####################################################################
116 # THIS IS A WARNING ONLY. YOU DO NOT NEED TO RESEND YOUR MESSAGE. #
117 ####################################################################
118
119 Your message could not be delivered for more than $delay_warning_time_hours hour(s).
120 It will be retried until it is $maximal_queue_lifetime_days day(s) old.
121
122 For further assistance, please send mail to <postmaster>
123
124 If you do so, please include this problem report. You can
125 delete your own text from the attached returned message.
126
127 The mail system
128 EOF
129
130
131 #
132 # The success template is used when mail is delivered to mailbox,
133 # when an alias or list is expanded, or when mail is delivered to a
134 # system that does not announce DSN support. It is an error to specify
135 # a Postmaster-Subject: here.
136 #
137
138 success_template = <<EOF
139 Charset: us-ascii
140 From: MAILER-DAEMON (Mail Delivery System)
141 Subject: Successful Mail Delivery Report
142
143 This is the mail system at host $myhostname.
144
145 Your message was successfully delivered to the destination(s)
146 listed below. If the message was delivered to mailbox you will
147 receive no further notifications. Otherwise you may still receive
148 notifications of mail delivery errors from other systems.
149
150 The mail system
151 EOF
152
153
154 #
155 # The verify template is used for address verification (sendmail -bv
156 # address...). or for verbose mail delivery (sendmail -v address...).
157 # It is an error to specify a Postmaster-Subject: here.
158 #
159
160 verify_template = <<EOF
161 Charset: us-ascii
162 From: MAILER-DAEMON (Mail Delivery System)
163 Subject: Mail Delivery Status Report
164
165 This is the mail system at host $myhostname.
166
167 Enclosed is the mail delivery report that you requested.
168
169 The mail system
170 EOF
171 }}}
172
173 You can customize the messages to your liking. In the messages, you can use all main.cf variables (e.g. $myhostname). If you take a look at the delay_template, you'll see that I use the additional variables $delay_warning_time_hours and $maximal_queue_lifetime_days. You could as well use $delay_warning_time_seconds, $delay_warning_time_minutes, $delay_warning_time_days, $delay_warning_time_weekes resp. $maximal_queue_lifetime_seconds, $maximal_queue_lifetime_minutes, $maximal_queue_lifetime_hours, $maximal_queue_lifetime_weeks, but keep in mind what [[http:~~/~~/www.postfix.org/bounce.5.html>>url:http://www.postfix.org/bounce.5.html]] explains about these variables:
174
175 //delay_warning_time_suffix: Expands into the value of the delay_warning_time parameter, expressed in the time unit specified by a suffix, which is one of seconds, minutes, hours, days, or weeks.//
176
177 //maximal_queue_lifetime_suffix: Expands into the value of the maximal_queue_lifetime parameter, expressed in the time unit specified by suffix. See above under delay_warning_time for possible suffix values.//
178
179 So if you use the variable $delay_warning_time_minutes instead of $delay_warning_time_hours in your template, you should follow it by the word "minutes" instead of "hours".
180
181 Next, we configure Postfix to use the custom templates:
182
183 {{{postconf -e 'bounce_template_file = /etc/postfix/bounce.cf'}}}
184
185 To check what the templates look like when all variables are replaced with their real values, and to make sure that there are no errors in your templates (e.g. a missing newline at the end of /etc/postfix/bounce.cf), run:
186
187 {{{postconf -b /etc/postfix/bounce.cf}}}
188
189 {{{server2:~# postconf -b /etc/postfix/bounce.cf
190 expanded_failure_text = <<EOF
191 This is the mail system at host server2.example.com.
192
193 I'm sorry to have to inform you that your message could not
194 be delivered to one or more recipients. It's attached below.
195
196 For further assistance, please send mail to <postmaster>
197
198 If you do so, please include this problem report. You can
199 delete your own text from the attached returned message.
200
201                    The mail system
202 EOF
203
204 expanded_delay_text = <<EOF
205 This is the mail system at host server2.example.com.
206
207 ####################################################################
208 # THIS IS A WARNING ONLY.  YOU DO NOT NEED TO RESEND YOUR MESSAGE. #
209 ####################################################################
210
211 Your message could not be delivered for more than 0 hour(s).
212 It will be retried until it is 1 day(s) old.
213
214 For further assistance, please send mail to <postmaster>
215
216 If you do so, please include this problem report. You can
217 delete your own text from the attached returned message.
218
219                    The mail system
220 EOF
221
222 expanded_success_text = <<EOF
223 This is the mail system at host server2.example.com.
224
225 Your message was successfully delivered to the destination(s)
226 listed below. If the message was delivered to mailbox you will
227 receive no further notifications. Otherwise you may still receive
228 notifications of mail delivery errors from other systems.
229
230                    The mail system
231 EOF
232
233 expanded_verify_text = <<EOF
234 This is the mail system at host server2.example.com.
235
236 Enclosed is the mail delivery report that you requested.
237
238                    The mail system
239 EOF
240 server2:~#}}}
241
242 If no errors are shown, we can restart Postfix so that it can use the custom templates:
243
244 {{{service postfix restart}}}
Maila Networks