How to automatically reject an email message to a recipient in Postfix
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:
Open the Postfix configuration file in a text editor:
sudo nano /etc/postfix/main.cf
Add the following line to the end of the file to create a new smtpd_recipient_restrictions rule:
This file tells Postfix to reject any email sent to the specified email addresses or domains.
Save and close the file.
Compile the file with the following command postmap /etc/postfix/reject_recipients
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.