How to automatically reject an email message to a recipient in Postfix

Background

To automatically reject an email message using Postfix, you can set up a smtpd_recipient_restrictions rule in the Postfix configuration file located at /etc/postfix/main.cf

Why would you want to automatically reject an email message in Postfix?

The amount of reasons why you would want to automatically reject a message in Postfix are too numerous to mention here, but in our case we had a user who used a CCTV login to send email alerts to a full mailbox at Google. Since Google rejects the email which Postfix holds in the queue for up to 5 days, the queue buildup was gigantic and caused our monitoring system to give false positives. We requested our client to do something about this but they had to ask their client and things came to a standstill.

Step to automatically reject certain recipients or domains

Here are the steps to automatically reject Postfix recipients:

  1. Open the Postfix configuration file in a text editor:
sudo nano /etc/postfix/main.cf
  1. Add the following line to the end of the file to create a new smtpd_recipient_restrictions rule:
smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/reject_recipients

If you have a more complex existing smtpd_recipients_restrictions list, then you might have to slot it in elsewhere, example below:

# 26 Feb 2023 - added check_recipient_access rule to file reject_recipients to avoid queue build up
smtpd_recipient_restrictions = 
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
reject_invalid_hostname,
check_policy_service inet:127.0.0.1:10023,
reject_rbl_client bl.spamcop.net,
reject_rbl_client cbl.abuseat.org,
reject_rbl_client b.barracudacentral.org,
check_recipient_access hash:/etc/postfix/reject_recipients,
permit

This line tells Postfix to check the reject_recipients file for a list of email addresses or domains to reject.

  1. Create a new file called reject_recipients in the /etc/postfix directory:
sudo nano /etc/postfix/reject_recipients
  1. Add the email addresses or domains you want to reject, one per line. For example:
[email protected] REJECT
spammydomain.com REJECT

This file tells Postfix to reject any email sent to the specified email addresses or domains.

  1. Save and close the file.
  2. Compile the file with the following command postmap /etc/postfix/reject_recipients
  3. Restart the Postfix service to apply the changes:
sudo systemctl restart postfix

Now, any email sent to the specified email addresses or domains will be rejected by Postfix and returned to the sender with an error message. Note that this method only rejects emails based on the recipient address; you can also set up additional rules to reject emails based on other criteria such as the sender address or content.

References

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top