I have Sendmail installed under Ubuntu 16.04.1. My needs are simple:

  • Forward all email sent to *@domain.com to one specific gmail address


  • If email is sent to specificuser@domain.com, forward those messages to one specific gmail address (different gmail address than above). This user does not have an account on the server.

I know this is simple, but I cannot figure out how to accomplish it.

I've got a virtusertable that looks like this:

specificuser@domain.com     user2@gmail.com
@domain.com                 user1@gmail.com

Can anyone assist me in getting this configured, please? Thanks!

  • What happens at the moment? Does everything go to the default address or does nothing get forwarded? Do you get any errors? Have you tried using sendmail's rewrite test mode (-bt)? – Paul Haldane Aug 8 '16 at 14:27
  • Nothing gets forwarded at all -- not messages sent to specificuser@domain.com NOR anything sent to the domain. – Drew Vogel Aug 8 '16 at 14:43
  • 1
    What is reported/logged in sendmail's log files? – AnFi Aug 8 '16 at 15:11

Install Prerequisites

apt-get install sendmail mailutils sendmail-bin

Create a Gmail Auth File

Create the actual directory for the auth file

mkdir -m 700 /etc/mail/authinfo/
cd /etc/mail/authinfo/

Create the auth file itself

vi /etc/mail/authinfo/gmail-auth

Put this in the contents of gmail-auth (make sure to put in the correct user/gmail/password(Also leave the prefixes U:user I:email P:password))


Next you want to create the hash map for the authentication file

makemap hash gmail-auth < gmail-auth

Configure Your Sendmail

Add the following block of code right above first "MAILER" definition line

define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-auth.db')dnl

Next you have to compile the sendmail configuration.

make -C /etc/mail

Restart sendmail to pick up the changes

/etc/init.d/sendmail reload

Configuration Test

echo "Just testing my sendmail gmail relay" | mail -s "Sendmail gmail Relay" my-email@my-domain.com

At this point you should have the relay working. Now we will move on to the virtusertable.


In order to create virtusertable mappings, you must first create a text file in the /etc/mail/ directory of your server. Each entry in a virtusertable should be on a single line. The original recipient address on the left hand side, with one or more spaces or tabs separating it from the right hand side, which contains the destination address.


vi /etc/mail/virtusertable

user@domain.com    user@gmail.com
@domain.com        master@domain.com

Any time you make a change to the /etc/mail/virtusertable text file, you will need to create a db file that sendmail can read. The following command will create the /etc/mail/virtusertable.db file when run by root:

makemap hash /etc/mail/virtusertable < /etc/mail/virtusertable

Now restart sendmail and all should be functioning as expected.

/etc/init.d/sendmail reload
  • Thank you very much. I don't see where those steps take into account my need to forward email to a specific user's gmail box. Where do I configure that? – Drew Vogel Aug 8 '16 at 20:20
  • Figured we could address that after you get the forward working. – xguru Aug 8 '16 at 20:23
  • OK. I followed the steps you provided (thanks again!) and the "echo "Just testing my sendmail gmail relay" | mail -s "Sendmail gmail Relay" my-email@my-domain.com" worked as expected. I also sent from another email to my server's email address, and it worked as expected. So far, so good! I tried sending to <random>@server, and that worked too! Next question... How do I configure it to send specific user's email to her gmail? – Drew Vogel Aug 8 '16 at 20:31
  • Specifically how is the smtp server receiving the mail? I get that you want it routed to user@gmail.com, but how is it being generated and how is it being handled now? (sorry if I'm overlooking already supplied information) – xguru Aug 8 '16 at 22:11
  • I am not sure how to answer your question. I am a Google Apps user, and my domain used to work as I desired. My wife recently changed her Gmail address and I cannot find where to change the forwarding from <specificnanw>@mydomain.com to her new Gmail address. It's been so long since I configured it that I frankly forgot. And I don't know where to start looking... – Drew Vogel Aug 9 '16 at 1:59

have you tried ssmtp?

SMTP is a program which delivers email from a local computer to a configured mailhost (mailhub)

with gmail setup



  • Will sSMTP do what I require? If so, do I need to remove Sendmail? – Drew Vogel Aug 8 '16 at 14:56
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Michael Hampton Aug 8 '16 at 18:25

You will likely need to authenticate with Gmail to sent the messages. Otherwise the mail may be treated as Spam. This is done by configuring a client password (where your server is the client) entry.

For an Exim4 install I would do the following:

  • Configure the server to use one of Gmail's MX addresses as a smarthost (with authentication);
  • Configure a wildcard alias for the your domain; and
  • Setup a wildcard alias alias for the domain.

The default alias router doesn't handle wildcards, but it is a simple modification to add wildcards.

These steps apply for pretty well any server software you want to use. Some software may default to using a smarthost.


Sendmail consults virtusertable only for deliveries to email addresses in local email domains ($=w) and domains listed in $={VirtHost}.
Sendmail by default adds some names to $=w automatically.

You can check the above lists (classes) using as root the commands below

echo `$=w` | sendmail -bt
echo `$={VirtHost}` | sendmail -bt

1) You can add domain.com to $={VirtHost} in sendmail.mc

C{VirtHost} domain.com

2) You can allow relaying to the domain from anywhere on the internet with the following access-table entry:

to:domain.com  RELAY

A) Because it will leave envelope sender unchanged gmail may reject or treat as spam some forwarded messages
B) Such forwarding to gmail is not recommended without anti-spam filtering
=> I would recommend (slightly) more complicated solution with relaying via procmail script. It will rewrite envelope sender and provide anti-spam hook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.