How to Create a Simple Mail Forwarder in 2024

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.

Let’s create a mail forwarder in a Docker container. It is probably the simplest solution. You can use it, for example 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 Docket Hub and GitHub). We’ll just give an example of configuring a mail forwarder with Docker Compose version 3 here.

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 can keep only the mail forwarder in a Docker container. And install everything else directly on your server. Or you can keep everything in Docker. It’s up to you.

Contents

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 mail 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:

  1. Sign in to your GoDaddy account
  2. Scroll down to the Domains table
  3. Click DNS next to the domain where you wish to add an MX record
  4. In the block “DNS Records” click the button “Add” on the top right
  5. 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 domain1.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 could 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 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_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:

  1. Please replace all e-mail addresses above with your real e-mail addresses.
  2. Do not put any spaces around “=” in the SMF_CONFIG line. Or you’ll get an error.
  3. If you do not need the Postfix logs to persist after the Docker container restart, you can comment or remove the "volumes:" section.
  4. 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.
  5. 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”):

docker-compose down

If you ever need to enter the Docker 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 Docker commands.

Conclusion

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.

Sergei Korolev
Sergei Korolev
Web developer and the author of all articles on this site. With over 25 years of programming experience, he has specialized in web programming for more than 19 years. He is a Zend Certified Engineer in PHP 5.3. He is available for hire at a rate of $60 USD per hour. You can see his resume here. Please contact him via this contact form to get a quote. He currently lives in Belgrade, Serbia.

2 thoughts on “How to Create a Simple Mail Forwarder in 2024”

Leave a Reply

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