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+

21 thoughts on “Sendmail using Nullmailer and Gmail account on Linux server

  1. Looks like SSMTP has reached its end of life:

    Note: This program still works as of 11-14-2009 but note that SSMTP is no longer being developed. You might want to consider an alternative like MSMTP.

  2. Thanks, that worked perfectly. Script was pretty handy, but it may be wise to make an annotated version that explains more. I understood what it was doing, but others may not.

  3. Current version of nullmailer (1.11) supports ssl, just add –ssl to the remotes file

  4. Hi, your script worked at first try on my Ubuntu 12.04.
    However….when sending a mail it seems that openssl is started and it runs at close to 100 % cpu time ?!?!? And continues to do so . Anything wrong with my setup ?
    A mail is sent every now and then to inform about detected earthquakes from field stations, in other words its not used extensively.

  5. Pingback: How to backport packages on Ubuntu Linux

  6. Hi, this may sound so elementary for you, but where should I save the file? Should it start on system startup or should I start it manually?

    Thanks a bunch!

  7. How to bind this script to Zoneminder? Script itself successfully sends messages, but how to configure Zoneminder 1.25 to this script?

  8. Was using this setup for a while, however I had the 100% CPU problem.

    Now using smtp directly as a remote, not sure if this is a new feature of a recent release of nullmailer.. Example config:

    smtp.gmail.com smtp –port=465 –auth-login –user=test@example.com –pass=password –ssl

  9. cool… a file! With no explanation as to what to name it, where it goes, or what to do with it. And then people wonder why linux is not more user friendly lmfao. Next time: have a noob walk through your stuff. If they can’t do it, refine your directions or quit making tutorials.

  10. Worked perfectly. Thanks. It would have been nice to have some explanation so I didn’t stare at it for an hour to make sure I was happy with it before running it, but otherwise, thank you!

  11. I found the same issue of Terje Utheim: 100% ongoing cpu usage!
    Any suggestion about how to diagnose and fix this?
    thanks

  12. If nullmailer is failing with smtp.gmail.com try using IPv4 address directly: 74.125.206.109

    my /etc/nullmailer/remotes looks like this:

    74.125.206.108 smtp –port=465 –auth-login –ssl –user=USER@gmail.com –pass=PASS –insecure

  13. Note to the webmaster as well as people attempting to copy and paste lines such as:
    `74.125.206.108 smtp –port=465 –auth-login –ssl –user=USER@gmail.com –pass=PASS –insecure`

    Your content management system is converting double hyphens to em-dash characters, so `- – OPTION` is rendered as `–OPTION` which you probably don’t want

Leave a Reply

Your email address will not be published. Required fields are marked *