Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexhayes/a7927feee20f5ba14a21 to your computer and use it in GitHub Desktop.
Save alexhayes/a7927feee20f5ba14a21 to your computer and use it in GitHub Desktop.
Force all email delivery in Ubuntu to a particular address

Force All Email Delivery To A Single Email Address

So, you're a developer testing lots of codes and you want to ensure that you don't accidentally send out email to people during testing.

However, you also want to actually view emails in a real email client, not a console.

This is how you can do it using Ubuntu and Exim ensuring that the email gets sent out through Google apps.

Installation

sudo apt-get install exim4

Configuration

  1. Turn on 2-Step Authentication in Google Apps and create a two step pass code - we're going to need this later so write it down. See Two-Step Authentication for more details.

  2. Copy update-exim4.conf.conf to /etc/exim4/update-exim4.conf.conf (yes... .conf.conf)

  3. Run the following:

    sudo HOSTNAME=`hostname` sed --in-place -e "s/\[YOUR-HOST-NAME\]/$HOSTNAME/g" /etc/exim4/update-exim4.conf.conf
  4. Copy passwd.client to /etc/exim4/passwd.client then;

    1. Replace you@example.com with your username and domain name.

    2. Replace 2steppassword with the 2-Step password you created in Step 1.

    3. Change access to the file;

      sudo chown root:Debian-exim /etc/exim4/passwd
      sudo chmod 640 /etc/exim4/passwd
  5. Copy 01_exim4-config_catchall_redirect into /etc/exim4/conf.d/router/01_exim4-config_catchall_redirect and change it accordingly.

  6. Update and Restart;

    sudo update-exim4.conf
    sudo service exim4 restart

Test

echo "Hello World" | mail -s 'Subject...' anyone@example.com

Rather than the mail above being delivered to anyone@example.com it will be delivered to the address you specified in 01_exim4-config_catchall_redirect.

If the mail doesn't get there you can monitor the exim logs as follows;

tail -f /var/log/exim4/mainlog

Two-Step Authentication

Depending on how your Google Apps has been configured you may not need to use a Two-Step Authentication pass code. However if you don't you'll need to store your actual password, unencrypted, on the file system - not a very smart idea.

So, to save a whole bunch of stuffing around just turn it on :)

catch_all_outgoing:
driver = redirect
data = you@example.com
# password file used when the local exim is authenticating to a remote
# host as a client.
#
# see exim4_passwd_client(5) for more documentation
#
# Example:
### target.mail.server.example:login:password
*.google.com:you@example.com:2steppassword
# /etc/exim4/update-exim4.conf.conf
#
# Edit this file and /etc/mailname by hand and execute update-exim4.conf
# yourself or use 'dpkg-reconfigure exim4-config'
#
# Please note that this is _not_ a dpkg-conffile and that automatic changes
# to this file might happen. The code handling this will honor your local
# changes, so this is usually fine, but will break local schemes that mess
# around with multiple versions of the file.
#
# update-exim4.conf uses this file to determine variable values to generate
# exim configuration macros for the configuration file.
#
# Most settings found in here do have corresponding questions in the
# Debconf configuration, but not all of them.
#
# This is a Debian specific file
dc_eximconfig_configtype='smarthost'
dc_other_hostnames='[YOUR-HOST-NAME]'
dc_local_interfaces='127.0.0.1 ; ::1'
dc_readhost=''
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost='smtp.gmail.com::587'
CFILEMODE='644'
dc_use_split_config='true'
dc_hide_mailname='false'
dc_mailname_in_oh='true'
dc_localdelivery='maildir_home'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment