Sendmail using Nullmailer and Gmail account on Linux server

When you run VPS servers or other “low end” boxes it is common scenario need to setup a outgoing email mail transfer agent to get reports from cron jobs or enable PHP scripts to send email.

Linux offers some really heavy weight options for sending email, including Sendmail, Postfix and Exim4. However, if you are a low end box user, you run low end services of which requirements are likely to send one email per month from your puny cron job or web contact form. This makes the options above

  • Plain scary
  • Very difficult to configure, maintain, due to extra complexity not necessary for your use case
  • Open some attack surface
  • You still need a master SMTP where you push forward your email messages

They don’t scale down.

Meet Nullmailer. Nullmailer is a simple MTA (mail transfer agent) and sendmail command provider which simply dumps your email output forward to SMTP service.

But that’s not all. If you are using low end boxes you probably don’t need good SMTP service from your VPS provider  (spam makes them expensive to provide). So, here is the trick: you are probably using already a free web email provider like gmail, Hotmail or whatever happens to be the dominating internet company of the day. Nullmailer can be configured to use authenticated SMTP sending through any email provider quite easily.

Below is a setup script for Ubuntu / Debian (tested on Ubuntu 12.04 LTS) which setups xinetd tunnel to connect to GMail’s smtp.google.com and send emails as your authenticated Gmail user. The orignal idea is described in this blog post by Jon Spriggs.

The downside of this hacky solution include

  • No real email From addresses: all email is aliased to your authentcated SMTP user
  • SMTP user password is stored on the server as plain text, so you probably want to create a rogue gmail user for this
  • No locally buffered email (not sure about this with Nullmailer?) – if SMTP acts strangely be prepared for major screw up
  • Using sendmail command with this solution may cause delays and other issues in the script (not sure how timeouting is handled with Nullmailer?)

The script is also available on Github, hosted inside ZtaneSH project. Please contribute your changes back on Github in a case you come across of improvements.

Nothing else is needed, except running the script as instructed.

#!/bin/bash
#
# Setup nullmailer on Ubuntu using your Gmail account as SMTP
# - you get a working sendmail command without requiring to setup complex SMTP
# stuff. Mostly useful with cron scripts.
#
# Orignal script based on https://jon.sprig.gs/blog/post/9
#

# Abort script if any command fails
set -e

#
# Create a gmail SSL wrapper script
#

if [ -z "$GMAIL_USER" ] || [ -z "$GMAIL_PASSWORD" ] || [ -z "$TEST_ADDRESS" ] ; then
    echo "Setup sendmail via gmail proxy on your Ubuntu box"
    echo "Usage:"
    echo "GMAIL_USER=foobar@gmail.com GMAIL_PASSWORD=12312312 TEST_ADDRESS=youremail@example.com sh setup-nullmailer.sh"
    exit 1
fi

# install required software
sudo apt-get install -y openssl xinetd nullmailer

sudo tee /usr/bin/gmail-smtp <<EOF >/dev/null
#!/bin/sh
/usr/bin/openssl s_client -connect smtp.gmail.com:465 -quiet 2>/dev/null
EOF
sudo chmod +x /usr/bin/gmail-smtp

#
# Create xinetd.d entry which wraps SMTP traffic to port 10025 go
# go to gmail
#

sudo tee /etc/xinetd.d/gmail-smtp <<EOF >/dev/null
# default: on
# description: Gmail SMTP wrapper for clients without SSL support
# Thanks to http://ubuntuforums.org/showthread.php?t=918335 for this install guide
service gmail-smtp
{
    disable         = no
    bind            = localhost
    port            = 10025
    socket_type     = stream
    protocol        = tcp
    wait            = no
    user            = root
    server          = /usr/bin/gmail-smtp
    type            = unlisted
}
EOF
sudo /etc/init.d/xinetd reload

#
# Set nullmail to use xinetd
#
sudo tee /etc/nullmailer/remotes <<EOF >/dev/null
127.0.0.1 smtp --port=10025 --user=$GMAIL_USER --pass=$GMAIL_PASSWORD
EOF
sudo /etc/init.d/nullmailer reload

# send test email
echo "This is a test message from ${USER}@${HOSTNAME} at $(date)" | sendmail $TEST_ADDRESS

echo "Test mail send to $TEST_ADDRESS"

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+