Skip to content
Bots!
Bots!
  • About
    • Myself
    • আমার দোয়া
  • Bookmarks
    • Bookmarks
    • My OCI Bookmarks
    • Useful Proxmox Commands & Links
    • Learning Nano
    • Useful Sites
    • Useful Virtualbox Command
    • Useful MySQL Command
    • Useful Linux Command
    • BTT-CAS
  • Resources
    • Webinar on Cloud Adoption for Project Managers
  • Photos
  • Videos
  • Downloads
Bots!

Configuring Postfix to block outgoing mail to all but one domain

Rumi, September 6, 2019

This is so simple to do, but I have to look it up every time I need it (not something that comes up regularly!);

When configuring a development server, you may find you have a need to ensure that emails will not be sent to any domain except those you explicitly permit (for example if you’re using real-world data to do some testing, do you want to send all those users irrelevant emails?).

This documentation details how to configure Postfix on a Linux server to disregard any mail sent to domains that are not explicitly permitted.

Don’t use IPTables

You could, of course, add two IPTables rules to the outgoing chain. The first of which would allow connections on Port 25 to the domain you wish to allow, the second blocking connection to any server on Port 25.

It’ll block the mail from being sent, but will mean that every one of those messages sits in the mail queue for 60 days until it’s disregarded. You could reconfigure the timeout, but given the ease of the steps below, what’s the point?

Use Transport Mapping

Using this method, we can tell Postfix to either reject the mail, or disregard it. The latter is generally the preferred method as we want the sending application to believe the mail has been sent.

First we need to edit the postfix configuration file

nano /etc/postfix/main.cf

Now search for transport_maps (Ctrl-W, transport_maps -> Enter). You shouldn’t find anything (if you do, skip the next step, but make note of which file is listed)

Add a line reading

transport_maps = hash:/etc/postfix/transport

Note: Depending on the version of Postfix you’re running, you could use texthash which would avoid needing to run postmap on the file. It’s up to the reader to work out which route they’d prefer, but if your postfix version is < 2.8 you can’t use texthash.

Save and close (Ctrl-X, Y -> Enter)

Now we need to update the transport maps (if you didn’t need to add a line, subsitute the relevant file path here)

nano /etc/postfix/transport

Now we need to add a line specifying which domain to allow sending to (I’m going to allow to bentasker.co.uk). To do so, insert the following lines

bentasker.co.uk :
* discard:

This will simply discard messages to any email address not of the domain bentasker.co.uk. If you wanted to reject with an error you’d use (set the error text to suit your needs)

bentasker.co.uk:
* error: Only allowing one domain

Note: Simply add additional domains on the line after bentasker.co.uk:, one line per domain.

Save and close (Ctrl-X, Y -> Enter)

Now we need to create a hash of the file (unless you used texthash in main.cf)

postmap /etc/postfix/transport

Finally, we need Postfix to reload it’s rules

/etc/init.d/postfix reload

Job done! You should now find that emails sent to domains not specified are silently discarded, whilst those for whitelisted domains go through as normal. Be sure to check that the change has worked though, mailq and the maillog are your friends!

For non-whitelisted domains you should see the message being removed in the maillog but with no connection taking place before hand, and relay showing as none (there may be no sign of a connection, but if relay != none there’s a very good chance the mail was sent). 

Src:https://www.bentasker.co.uk/documentation/linux/161-configuring-postfix-to-block-outgoing-mail-to-all-but-one-domain

Administrations Configurations (Linux) Postfix

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

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

Myself…

Hi, I am Hasan T. Emdad Rumi, an IT Project Manager & Consultant, Virtualization & Cloud Savvyfrom Dhaka, Bangladesh. I have prior experience in managing numerous local and international projects in the area of Telco VAS & NMC, National Data Center & PKI Naitonal Root and CA Infrastructure. Also engaged with several Offshore Software Development Team.

Worked with Orascom Telecom-Banglalink, Network Elites as VAS partner, BTRC, BTT (Turkey) , Mango Teleservices Limited and Access to Informaiton (A2I-UNDP)

Currently working at Oracle Corporation as Principal Technology Solution and Cloud Architect.

You can reach me [h.t.emdad at gmail.com] and I will be delighted to exchange my views.

Tags

Apache Bind Cacti CentOS CentOS 6 CentOS 7 Debain Debian Debian 10 Debian 11 Debian 12 DKIM Docker endian icinga iptables Jitsi LAMP Letsencrypt Linux Munin MySQL Nagios Nextcloud NFS nginx pfsense php Postfix powerdns Proxmox RDP squid SSH SSL Ubuntu Ubuntu 16 Ubuntu 18 Ubuntu 20 Varnish virtualbox vpn Webmin XCP-NG zimbra

Topics

Recent Posts

  • Install Jitsi on Ubuntu 22.04 / 22.10 April 30, 2025
  • Key Lessons in life April 26, 2025
  • Create Proxmox Backup Server (PBS) on Debian 12 April 19, 2025
  • Add Physical Drive in Proxmox VM Guest April 19, 2025
  • Mount a drive permanently with fstab in Linux April 16, 2025
  • Proxmox 1:1 NAT routing March 30, 2025
  • Installation steps of WSL – Windows Subsystem for Linux March 8, 2025
  • Enabling Nested Virtualization In Proxmox March 8, 2025
  • How to Modify/Change console/SSH login banner for Proxmox Virtual Environment (Proxmox VE / PVE) March 3, 2025
  • Install Proxmox Backup Server on Debian 12 February 12, 2025

Archives

Top Posts & Pages

  • Install Jitsi on Ubuntu 22.04 / 22.10
©2025 Bots! | WordPress Theme by SuperbThemes