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 2

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

This is part of a series! Please make sure you have read Part 1 first! IMPORTANT – you can try out the result of this tutorial by visiting this CodeSandbox. However, I encourage you to read the blog post and try to follow along to understand how and why it works. Last time we left off with a very nice starry sky, but it’s a bit dead. We need to add some animation to it!

Continue reading 


fzf + SSH Config Hosts

󰃭 2024-10-10 | #linux #ssh #tips

SSH has a nice feature in which you can store aliases for frequently accessed hosts. Combining this with fzf, you can have a nice quick shortcut to quickly pick a server to connect to into. This comes in very handy if you need to ssh into different servers and forget their IP or hostname often. Here’s a sample ssh config file (normally located at ~/.ssh/config): # see https://man.openbsd.org/ssh_config.5 for all the available configuration settings Host runner-staging HostName 10.

Continue reading 