Setting up sendmail to redirect emails

󰃭 2016-05-14

Disclaimer: the instructions below are for Ubuntu, but they should work for most distros, the biggest difference is that the configuration files might be located elsewhere.

If you’re like me, you have a main email address and other email addresses set up in other domains.

I dislike having to check all my email addresses individually, so I set up my mail servers to redirect all the email to my main address automatically.

Sendmail has an aliases feature that makes this very simple to set up.

Let’s say you want to redirect emails this way:

  • webmaster@yourdomain.com -> example@gmail.com, someoneelse@gmail.com
  • help@yourdomain.com -> helper@gmail.com
  • support@yourdomain.com -> helper@gmail.com

Follow these steps as root:

  1. Change the /etc/mail/aliases file to look like this:

      webmaster: example@gmail.com, someoneelse@gmail.com
      help: helper@gmail.com
      support: help
    

    As you can see, each line in the file matches an origin email to a destination email. Each line can reference previous entries as well.

  2. After changing the file, run:

      $ newaliases
    
  3. Make sure port 25 is open on your machine, so sendmail is able to listen for incoming email:

      $ iptables -A INPUT -p tcp --dport 25 -j ACCEPT
    

    Also make sure to save the iptables rules so they will be restored when the service restarts. This varies by distro, so it’s better to google something like iptables save <your distro>.

  4. Change /etc/mail/sendmail.mc so sendmail receives email from the outside world.

    Change this line:

      DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
    

    to

      DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp')dnl
    

    We’re not done modifying this file yet. Now we need to verify that the domain in the configuration matches your server’s domain. If not, change it:

      MASQUERADE_AS(`yourdomain.com')dnl
    
  5. After saving the file, you need to regenerate sendmail.cf:

      m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
    
  6. Add your domain to /etc/mail/local-host-names. This file has a domain per line. If your domain is not there, add it on a new line.

  7. Restart sendmail:

      $ /etc/init.d/sendmail restart
    

Depending on the configuration of your email client or web UI, the emails might end up stuck in a spam folder, so make sure to check there.

If you use Gmail, you can make a filter to avoid sending your redirected emails to spam. To do so:

  1. Do a search for from:yourdomain.com.
  2. Click on “Create a filter with this search”.
  3. Check the “Never send it to Spam” box.
  4. Click on “Create filter”.

Preventing email from being sent to Spam



More posts like this

Starry Sky in HTML5 Canvas - Part 1

󰃭 2019-04-13 | #canvas #javascript #programming-projects #tutorials

In my spare time I often enjoy creating visualizations using HTML5 canvas. I’m planning to do a little presentation about this so I thought a good way to get started was to create a blog post explaining how to do a simple one. This tutorial will teach you how to create something like the image below from scratch! IMPORTANT – you can try out the result of this part of the tutorial by visiting this CodeSandbox.

Continue reading 


Free Static Web Hosts for Frontend Developers

󰃭 2022-01-15 | #css #html #javascript #tips #tutorials

Nowadays it’s very easy to publish on the web for free. There are countless blogging platforms and website creators. But these platforms usually end up controlling your content. Sometimes you cannot even export your own data! The other extreme is to set up your own server by yourself. Buy a VPS (virtual private server) or a shared hosting somewhere, install a web server, and upload your files. This is a lot of work already!

Continue reading 