10

I would like to reject mails to a certain address with a custom message. Mails to other non existing addresses should be unchanged. How can I do that? I'm using Postfix 2.7.0 on Ubuntu 10.4.

Background: My websites sends mails to my users, and so far, my personal address is used as the sender. I would like to change this to a noreply@... address, but since the users reply quite often to these emails, I would like to send them a helpful reject message.

Thanks!

Gerald Schneider
  • 26,582
  • 8
  • 65
  • 97
iGEL
  • 275

2 Answers2

21

Create a custom_replies map (i.e. /etc/postfix/custom_replies) with the following contents:

noreply@mydomain.com REJECT Like I said, NOREPLY

Run sudo postmap /etc/postfix/custom_replies.

Edit /etc/postfix/main.cf and put the following as the first check of the smtpd_recipient_restrictions:

check_recipient_access hash:/etc/postfix/custom_replies,

Then issue a sudo postfix reload.

Try sending email to noreply@mydomain.com:

$ telnet localhost 25
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mydomain.com ESMTP Postfix (Ubuntu)
HELO localhost
250 mydomain.com
MAIL FROM: <god@mydomain.com>
250 2.1.0 Ok
RCPT TO: <noreply@mydomain.com>
554 5.7.1 <noreply@mydomain.com>: Recipient address rejected: Like I said, NOREPLY
quit
221 2.0.0 Bye
3

To add to @ΤΖΩΤΖΙΟΥ's answer, if you need to check a specific condition and then return a custom message, you can create an smtpd_restriction_classes that specify the action you want to do once the condition has been verified.

Use case: Provide a custom rejection message to suspended accounts

As already mentioned, create the custom message file (e.g. /etc/postfix/custom_message) but with a "catch-all" regular expression:

/.*/ REJECT Sorry, your account has been suspended

Edit the /etc/postfix/main.cf file and add the following line before any restriction lines (like smtpd_recipient_restrictions):

smtpd_restriction_classes = reject_suspended_accounts_with_custom_msg
reject_suspended_accounts_with_custom_msg = regexp:/etc/postfix/custom_message

In the map file where you select the suspended virtual mailboxes (e.g. etc/postfix/suspended-map.cf) put the following query (example with MySQL, provided there is an account_has_been_suspended column to identify such accounts):

...
query = SELECT 'reject_suspended_accounts_with_custom_msg' FROM virtual_users WHERE email_address='%s' AND account_has_been_suspended=1

Finally, in the proper restriction parameter (either smtpd_sender_restrictions or smtpd_recipient_restrictions) add the check:

smtpd_recipient_restrictions = check_recipient_access mysql:/etc/postfix/suspended-map.cf

In this way, a virtual mailbox that has been marked as "suspended" in the database, will receive a custom rejection message.