CT Group Pty Ltd logo

 

How to use Ubuntu and Sendmail to relay local mail to a Google Apps smart host

Google Apps is great, isn't it. Google Apps provides a stable platform for small businesses, or larger ones, to manage IT needs without having to maintain servers or worry about spam filters, email viruses, hoaxes, etc. The platform also provides these services at a decent price.

But sending emails is slow

We have converted clients from internal mail servers to the Google Apps platform and the response has been mainly positive, as they don't get bills from us every few months after we've had to patch systems or upgrade software (and we don't mind not having to get up especially early to do this). Adding users is simpler as well.

But the one complaint is that it now takes ages to send email from their local clients (Thunderbird or Outlook). As the SMTP server being used is on the other end of an asynchronous Internet connection, sending larger emails can be a hit to productivity compared to the same process when the SMTP server was in the office.

Sending email at LAN speeds

What's the solution? Google Apps comes to the rescue with it's ability to act as a Smart Host, allowing a local SMTP server to get the mail from clients and forward it to the Google server.

'But wait', I hear you say, 'I use Google Apps because I don't want to spend money on local servers'. In most cases there would be at least one physical server on premises running VMWare or a similar hypervisor. If not, unfortunately the following won't help, but otherwise, let's continue.

The ultimate setup here is a local virtual server running Ubuntu and Sendmail. This server accepts mail sent to it from internal clients and forwards this mail to the Google Apps server for delivery.

Google Apps configuration

The first step is to configure Google Apps to allow a server in the office to send mail to the Google server. This is a fairly easy process, with the official documentation being a good explanation. In summary:

o Log in to the Google Apps admin console
o Go to Google Apps > Gmail > Advanced Settings
o Scroll down to SMTP relay service (or use the search function, it's Google after all)
o Give the SMTP relay a name like 'Office'
o Select 'Only registered Apps users in my domain' as the Allowed senders
o Tick 'Only accept mail from the specified IP addresses' and add the static public IP address of the office connection (you're running a business and used to have a mail server, so this is assumed)
o Leave 'Require SMTP Authentication' and 'Require TLS encryption' unticked
o Save your changes

That's it from the Google side, let's get to the hard part.

Ubuntu configuration

The rest of the setup happens on your local server. Download the Ubuntu Server ISO file and create a small virtual machine to host the Ubuntu server. The virtual machine can be quite limited, perhaps 30GB of disk space, 1GB RAM and 1 CPU, or even less could do. No data is going to be permanently stored on this system, it will all be transient.

Attach the ISO file to this new virtual machine and start 'er up. This ISO should boot and start the Ubuntu installation wizard. Work through the wizard, selecting your country, keyboard, etc, and when asked what packages to install just Continue, leaving all the packages unticked. Through the process you will need to specify a host name. For simplicities sake, set it to

smpt

Once Ubuntu is installed sendmail needs to be installed. Log in to the server and then

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install sendmail

sudo shutdown -r now

This will get the latest package updates and install Sendmail, then restart the server.

Sendmail configuration

This is the key to the whole process, and took a little while to piece together from various sources, hence this page.

We are trying to keep things as simple as possible, which makes future maintenance simpler as well. To this end, the changes to the sendmail config files are the minimum necessary. Let's get into the editing:

sudo nano /etc/mail/sendmail.cf

will open the Sendmail config file. Find the

# "Smart" relay host (may be null)
DS

lines and change it to

# "Smart" relay host (may be null)
DSsmtp-relay.gmail.com

(courtesy). Next we restrict the message size so that we don't try to send an email larger than what Google will accept. Find the

# maximum message size
# O MaxMessageSize=0

lines and change it to

# maximum message size
O MaxMessageSize=36000000

(courtesy). Finally, find the

O DaemonPortOptions=Family=inet,  Name=MTA-v4, Port=smtp, Addr=127.0.0.1
O DaemonPortOptions=Family=inet,  Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1

and change them to

O DaemonPortOptions=Family=inet,  Name=MTA-v4, Port=smtp
O DaemonPortOptions=Family=inet,  Name=MSP-v4, Port=submission, M=Ea

(courtesy). Save the file (press <CTRL>-<o>) and close Nano (<CTRL>-<x>).

Now we need to edit the access control list, allow only clients on the LAN to send to this server. Type:

sudo nano /etc/mail/access

to open the file. At the end of the file add the line

192.168.0     RELAY

(courtesy) where 192.168.0 is your local network address. Save and close this file. This allows only clients on the local network to relay through this server.

Last but not least, restart the Sendmail service to make the changes take affect.

sudo service sendmail restart

Now you have an SMTP server on the LAN, so let's configure the clients to use it.

DHCP and DNS changes

A quick note at this point, though, is that our server is still getting its IP address via DHCP and doesn't have any details in the DNS. As we are assuming that local servers are present, a DHCP reservation should be added so that this new server is issued the same address all the time and an A record should be added to the DNS server so that client computers can reach it on smtp.your.domain.com (which should be the domain the rest of your local network runs on).

Thunderbird configuration

Although we are now sending mail to a local SMTP server, Thunderbird by default still tries to save emails to the Sent Items folder. If these settings were left at the default, mail would be sent quickly, but you'd still be looking at a dialog box telling you the message is being saved, defeating all our work about.

While this is easy to turn off, the catch is that sent mail is not automatically saved to the Sent Items folder when the mail isn't sent via Google's SMTP server. A couple of changes in Thunderbird will give us the same functionality while keeping the speed provided by the SMTP relay.

In Thunderbird we need to configure some incoming and outgoing settings, along with a Message Filter.

o Click Tools > Account Settings
o Click Copies & Folders in the left pane
o In the 'When sending messages, automatically' section untick the 'Place a copy in' box and tick the 'Bcc these email addresses' and make sure the users email address appears in the Bcc field.
o Click Outgoing Server in the left pane
o Click Edit on the right
o Change the server name to smtp.your.domain.com (your server name above), set the port to 25, set Connection Security to None and set Authentication Method to No authentication.
o Click OK and OK
o Click Tools > Message Filters...
o Click New...
o Enter the rule name, Sent mail, and leave the Manual Run and Getting New mail boxes ticked. Match the emails where the From is the users email address. Perform the actions Move Message to Sent Mail and Mark As Read.
o Click OK and Close the dialog

Now when emails are sent a copy will be sent through the local SMTP relay back to the users. When the user receives this copy it will be moved to their Sent Items folder, just like they used to get.

That's It

Hopefully this solves a problem, keeping the users onside while utilising the cloud facilities of Google, reducing the IT workload so that IT can do what it is supposed to, adding value by improving the business processes. The days of IT wasting time on infrastructure maintenance are falling behind us...

CSET Technologies is a member of
CT Group Pty Ltd, © 2017

 

Phone 04 1929 8657

 

Support pages >> Links pages