Few registrars or hosting companies offer free email forwarding services these days. So you’ll either need to pay for email forwarding for your domain or set up your own email forwarder.
Let’s create an email forwarder in a Docker container. This is probably the simplest solution. You can use it, for example, as a self-hosted email forwarder on a VPS server.
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 Docker Hub and GitHub). We’ll just give an example of configuring an email forwarder with Docker Compose here.
Please notice: Setting up an email forwarder in a Docker container doesn’t mean you need to run everything else (like an HTTP server or MySQL server) in Docker containers. You can keep only the forwarder in a container and install everything else directly on your server. Or, you can run everything in Docker — it’s up to you.
Suppose, you own the domains domain1.com
and 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:
box1@domain1.com
=>mybox@gmail.com
box2@domain1.com
=>mybox@gmail.com
box1@domain2.com
=>mybox@gmail.com
Before running the example below, please replace all these emails with your real email addresses.
To add the forwarder:
1. Add MX Records
Add MX records for all domains where you plan to receive or forward mail to domain email 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 fordomain1.com
, @ would be the same asdomain1.com
. - 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,mail.domain1.com
. - TTL: 1 Hour
Time To Live. This is how much time other DNS servers should wait before updating this record on their side. You can set any time you wish here.
Also, please see this article on adding an MX record at GoDaddy.
Adding an MX record is usually similar and very straightforward on other registrars.
2. Configure the Docker Container
Create a file compose.yaml
(in this example, it was created in the folder /opt/projects/mail_forwarder/
on an Ubuntu 22.04 server):
services: smf: container_name: mail-forwarder image: zixia/simple-mail-forwarder restart: always ports: - "25:25" environment: - SMF_CONFIG=box1@domain1.com:mybox@gmail.com;box2@domain1.com:mybox@gmail.com;box1@domain2.com:mybox@gmail.com volumes: - /opt/projects/mail_forwarder/volumes/logs:/var/log/postfix
Please notice:
- Please replace all e-mail addresses above with your real e-mail addresses.
- Do not put any spaces around “=” in the
SMF_CONFIG
line. Or you’ll get an error. - If you don’t need Postfix logs to persist after the container restarts, you can comment out or remove the “
volumes:
” section. - 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 email 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 email 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”):
docker compose down
If you ever need to enter the container terminal, use the command:
docker exec -it mail-forwarder bash
, where mail-forwarder
is the name of the container.
You can see the list of currently running containers with:
docker ps
Please see the Docker documentation for more commands.
Conclusion
Setting up an email 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 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 bulletproof.
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 features, please see the Simple Mail Forwarder documentation on the Docker Hub and GitHub.
If you do not need an email forwarder, but just need to be able to send email messages 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.
Awesome, thank you. Simple and worked an absolute treat. Thanks for sharing.
Thank you very much for the good words! I really appreciate them.