Few registrar or hosting companies provide free e-mail forwarding services nowadays. So you have to either pay for mail forwarding for your domain, or you could create your own mail forwarder.
It is very easy to create a mail forwarder on a VPS server. This article describes such a solution.
We’ll create a mail forwarder in a Docker container. It is probably the simplest way to set up a mail forwarder.
Please notice: Setting up a mail forwarder in a Docker container does not mean you need to have everything else (HTTP server, MySQL server, etc.) on your server in Docker containers. You could keep only the mail forwarder in a Docker container. And install everything else directly on your server. Or you could keep everything in Docker. It’s up to you.
We’ll be using the Simple Mail Forwarder zixia/simple-mail-forwarder to create a mail forwarder in a Docker container. It is pretty well documented. Please see the documentation on Docket Hub and GitHub). We’ll just give an example of configuring a mail forwarder with Docker Compose version 3 here.
Suppose, you own the domains
domain2.com. Both sites are hosted (as virtual hosts) on the same VPS server. You want to forward the domain mail for both domains like this:
Please replace all these e-mails with your real e-mail addresses before running the example below.
To do the forwarding:
1. Add MX Records
Add MX records for all domains where you are planning to receive or forward mail on domain e-mail addresses.
Adding an MX record is very easy. We’ll give an example for GoDaddy DNS servers:
- Sign in to your GoDaddy account
- Scroll down to the Domains table
- Click DNS next to the domain where you wish to add an MX record
- In the block “DNS Records” click the button “Add” on the top right
- Enter an MX record:
- Type: MX
- Name: @
This is the same as the main domain name. E.g. if these are DNS records for
domain1.com, @ would be the same as
- Priority: 10
You can put any (0 greater) integer number here.
SMTP servers will try to deliver mail first to mail servers defined by MX records with lower priority.
- Value: domain1.com
It must contain a domain name (not an IP address) of the host, responsible for receiving mail for the domain, defined in the Name section of this MX record.
Sometimes, you need to set up a separate server for receiving mail for your domain. In this case, put that server domain name into the Value, for example,
- TTL: 1 Hour
Time To Live. This is how much time other DNS servers should wait before updating this record on their side. You could set any time you wish here.
Also, please see this article on adding an MX record at GoDaddy.
On other registrars, adding an MX record is usually similar and very straightforward.
2. Configure the Docker Container
Create a file
docker-compose.yml (in this example, it was created in the folder
/opt/projects/mail_forwarder/ on an Ubuntu 20.04 server):
version: '3.8' services: smf: container_name: mail-forwarder image: zixia/simple-mail-forwarder restart: always ports: - "25:25" environment: - SMF_CONFIGfirstname.lastname@example.org:email@example.com;firstname.lastname@example.org:email@example.com;firstname.lastname@example.org:email@example.com volumes: - /opt/projects/mail_forwarder/volumes/logs:/var/log/postfix
- Please replace all e-mail addresses above with your real e-mail addresses.
- Do not put any spaces around “=” in the
SMF_CONFIGline. Or you’ll get an error.
- If you do not need the Postfix logs to persist after the Docker container restart, you can comment or remove the
- We are using the ports directive here. This is because we need to publish our port 25 to the host machine.
For example, in the article on adding e-mail functionality to the official Docker WordPress image, we needed to send mail from web forms in WordPress. So, expose was enough. We did not need to accept any mail from external SMTP servers on the Internet and then forward it to some e-mail addresses.
- If you use a firewall on the server (e.g.
ufw), do not forget to open port 25 for incoming connections.
Now, to start the mail forwarder, run in the terminal:
docker-compose up -d
If you need to run the container but to rebuild the image first:
docker-compose up -d --build
To stop the container (and remove the container and everything created by “up”):
If you ever need to enter the Docker container terminal, use the command:
docker exec -it mail-forwarder bash
mail-forwarder is the name of the container.
You can see the list of currently running containers with:
Please see the Docker documentation for more Docker commands.
Setting up a mail forwarder in a Docker container should be a quick and simple solution. It has some advantages:
- Easy to set up
- Easy to remove if necessary
You just stop the Docker container. You can also remove the image (optional).
- Your container should be isolated from the host machine pretty reliably.
Docker uses iptables for network isolation. It is pretty bullet-proof.
Still, this was a very basic example of the Simple Mail Forwarder configuration. In addition, you may want to use the TLS (SSL) certificate, set up DKIM keys, etc. For these please see the Simple Mail Forwarder documentation on the Docker Hub and GitHub.
If you do not need a mail forwarder, but just need to be able to send e-mails from web forms of a WordPress site in a Docker container, please see this article.
Also, if you do not want to experiment in the production environment, you could try things in the local development environment first. In this case, please see this article on how to set up the local development environment on a VirtualBox virtual machine.
Thank you very much for reading to the end.
2 thoughts on “How to Create a Simple Mail Forwarder in 2023”
Awesome, thank you. Simple and worked an absolute treat. Thanks for sharing.
Thank you very much for the good words! I really appreciate them.