Blocking or Rejecting Emails While Using Postfix Mail Server or AAPanel
Creating a Blacklist of emails are necessary for everyone. This help to reduce number of spam emails from specific domains and senders. This guide will help you understand how to how to block all unwanted senders.
Whether You are Using MailServer Plugin from AAPanel or You don’t even use AAPanel and installed MailSever independently using Command Line or Terminal. The process remains the same
How to Create a Blacklist of Emails and Domains With a Reject Message With Postfix
Step 1
Create a file without any extension inside the postfix folder if it’s not already created. If you use AAPanel, you can navigate to /etc/postfix/ folder and created a file with any name, e.g. blacklist_senders. Or simple use the following command that works regardless or AAPanel or any other panel. If the file doesn’t exit, it will create new one and enable Edit mode.
nano /etc/postfix/blacklist_senders
Step 2
Add the following line depending on what you want. You can combine all of them. One each line for new rule. DO NOT USE COMMA. You can use Period and Exclamation marks.
This will block email from specific Address
rosemartinseo@gmail.com REJECT Your email is blocked! Stop sending spam.
This will block emails from all the senders of this domain and subdomains
*.spammer.com REJECT Sorry, emails from badsite.com are not accepted.
Step 3
Save the file. If you are using command line, you can save exit with following command;
Press Ctrl + X, and then type Y to confirm. It will Save the file and exit.
Step 4
Edit Postfix’s main.cf file. You can navigate to the folder in case of AAPanel or any file manager. located at /etc/postfix/main.cf. If not, use nano command with or without AAPanel.
nano /etc/postfix/main.cf
Look for smtpd_recipient_restrictions.
smtpd_recipient_restrictions may already have some content. You need to add check_blacklist_senders hash:/etc/postfix/blacklist_senders to it. Here is the example:
For example, you find this:
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
You will edit that line and it will look like this
smtpd_recipient_restrictions = check_blacklist_senders hash:/etc/postfix/blacklist_senders, permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
We have simply add check_blacklist_senders hash:/etc/postfix/blacklist_senders, right after = sign.
Step 5
Earlier we created file blacklist_senders, but MailServer still doesn’t recognize it. Use this command in terminal.
postmap /etc/postfix/blacklist_senders
This command will create blacklist_senders.db file.
Step 6
Restart Postfix with Following command or you can use AAPanel’s MailServer setting to Restart
systemctl restart postfix
Note: Whenever you add more email lists to blacklist_senders file, you always have to run postmap /etc/postfix/blacklist_senders and restart the postfix to apply the changes.