Skip to content

Instantly share code, notes, and snippets.

@mudpile45
Forked from bsodmike/howto.md
Created October 18, 2012 08:59
Show Gist options
  • Save mudpile45/3910554 to your computer and use it in GitHub Desktop.
Save mudpile45/3910554 to your computer and use it in GitHub Desktop.
HOWTO: Setup mail forwarding with Sendmail in Ubuntu 10.04 LTS

Domain to domain mail forwarding

This guide combined with the info here allows you do the following:

  • Forward all mails from one domain to another (via catch-all addresses)
  • Set up a catch-all email address (just enter a @ with no user in front of it in the virtuserstable)
  • set up distribution lists etc. (use commas)

Why would you want this? Well, for me I own some domain names and I like to use gmail to read my mail. One use case is that we have a small organization that I've already set up a google for domains account with, but the organization has multiple domains that all redirect to our website. I wanted our mail to also all redirect to the same mail, so I created MX records for all the domains that are supposed to redirect and pointed them at my VPS and on my VPS I set up mail relaying to my original google for domains account, with entries for each user. This way, if I had my google domains running at myemail@mygoogledomain.com, but someone sends mail for me to myemail@myotherdomain.com, it goes to my mail server which then forwards it on to myemail@mygoogledomain.com at which point google for domains takes over and gets the emails forwarded on to me.

The other use case is that I have a domain I own for personal use, and I like to use different email addresses with various companies to reduce spam. (Also, this lets me see which sites I sign up for will spam me by which email address they send to. And more accurate filtering in gmail since I can filter by the To: field). For example, I make an account at shadyshite.com, but I'm afraid they'll spam me after they send me my verification mail. So, when signing up there I enter the email shadysite.com@mypersonaldomain.com. The email runs to my mail server, which forwards it on to my gmail account where I see I got a mail addressed to shadysite.com@mypersonaldomain.com. If it's all good, I just keep receiving emails there. If not, then I can filter by To: shadysite.com@mypersonaldomain.com. Even better, if I ever need to send an email from this address gmail lets you set up send-from addresses after a verification mail. So, verify as yourself and then you can even send emails from shadysite.com@mypersonaldomain.com.

Basic steps: (see below for more detail for each step)

  1. Point an MX record at the machine you're using
  2. Install sendmail (it's probably already installed, but you can try sudo apt-get install sendmail on ubuntu/debian systems
  3. Set up local-host-names (see below)
  4. Set up relay hosts (this should be the same as the local host names)
  5. Allow access to sendmail from the outside world
  6. Make a virtual users table file

More info here: this

Copyright (c) 2011, Michael de Silva (michael@mwdesilva.com) Blog: http://www.bsodmike.com ~ Twitter: @bsodmike

Perform the following to enable email forwarding from the virtual domain virtualdomain.tld to another email address (in this example, jdoe@gmail.com). There are no local accounts associated - the aliases file has been left untouched.

Note that the hostname of the system this was performed on is different to virtualdomain.tld; typically, this should be the case for you as well (that's the whole point of virtual hosts; default will be bound to whatever VPS provided access domain defined in hostname).

/etc/mail/local-host-names

The system needs to know which domains it's acting as a mail server for, otherwise sendmail will refust to forward them. To fix, edit /etc/mail/local-host-names and add the domain names you want it to forward for:

example1.com
example2.com

/etc/mail/access

# Hosts with to allow relaying
virtualdomain.tld RELAY

/etc/mail/sendmail.mc

  • Make sure you do this, if you don't then the sendmail interface isn't listening to the outside world and can't perform relays, just as is mentioned here:

Remove ", Addr=" clauses to receive from any interface. Do this with caution as this opens up your MTA to be abused by spammers if you're not careful. /etc/mail/access needs to be setup carefully to ensure it rejects any domains apart from your own.

dnl DAEMON_OPTIONS(`Family=inet6, Name=MTA-v6, Port=smtp, Addr=::1')dnl
DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp')dnl

Add the following

dnl # Define virtualusertable
FEATURE(`virtusertable', `hash /etc/mail/virtusertable')dnl
VIRTUSER_DOMAIN_FILE(`/etc/mail/virtual-domains')dnl

Create virtusertable and add

jdoe@virtualdomain.tld     jdoe@gmail.com

Perform make and /etc/init.d/sendmail reload to restart the daemon. You can debug the relaying action by watching the log file by doing tail -f /var/log/mail.log.

###Unable to send out emails?

One of the most common reason I've seen for a freshly installed sendmail not being able to send out emails is the DAEMON_OPTIONS being set to listen only on 127.0.0.1

See /etc/mail/sendmail.mc

dnl # dnl # The following causes sendmail to only listen on the IPv4 loopback address dnl # 127.0.0.1 and not on any other network devices. Remove the loopback dnl # address restriction to accept email from the internet or intranet. dnl # DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl If that's your case, remove the "Addr=127.0.0.1" part, rebuild your conf file and you're good to go!

DAEMON_OPTIONS(`Port=smtp, Name=MTA')dnl

[root@server]$ m4 sendmail.mc > /etc/sendmail.cf [root@server]$/etc/init.d/sendmail restart

@OxyOss
Copy link

OxyOss commented Jul 15, 2017

Hi,
quick question, in the virtualusertable section you specify two different files. Is that a mistake?

@daturadev
Copy link

virtual domains??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment