This howto describes how to relay mail (such as system alerts) to email services such as gmail. First part describes doing so using sSMTP which only supports relaying local system mail and the second part shows how to do this using Postfix which is a fully featured MTA. Postfix might be an overkill in most cases but hey, it might have features that you may find useful!

This howto is tailored to FreeBSD systems but the main configuration will work on other operating systems.


Start off by installing sSMTP through ports:

$ cd /usr/ports/mail/ssmtp
$ sudo make install clean

Once installed, create a /usr/local/etc/ssmtp/ssmtp.conf config file with the following contents:

$ cat /usr/local/etc/ssmtp/ssmtp.conf

For AuthUser, put down your gmail username or your full email for Google Apps account that you want to forward the mail to.

Next up, change the mailwrapper config to point to sSMTP by editing /etc/mailer/mailer.conf. Mailwrapper is a FreeBSD utility that lets you change sendmail into any other MTA. It was built and designed for this specific purpose as many system utilities were originally written to tie into Sendmail. Mailwrapper is installed by default on FreeBSD systems but if yours doesn't have it, download the system sources and compile it:

$ cd /usr/src/usr.sbin/mailwrapper
$ sudo make obj depend
$ sudo make install
$ cat /etc/mailer/mailer.conf
sendmail /usr/local/sbin/ssmtp
send-mail /usr/local/sbin/ssmtp
mailq /usr/local/sbin/ssmtp
newaliases /usr/local/sbin/ssmtp
hoststat /usr/bin/true
purgestat /usr/bin/true

You may also leave /etc/mailer/mailer.conf as default as is, and just change the /usr/sbin/sendmail into a symlink that points to /usr/local/sbin/ssmtp:

$ sudo ln -sf /usr/local/sbin/ssmtp /usr/sbin/sendmail

Test mail flow by using mailx:

$ echo "blah" | mailx -s "testing 123"

You should see mail get processed in /var/log/maillog and come to your gmail account.

$ sudo tail -f /var/log/maillog
Mar 30 16:32:59 servername sSMTP[73787]: Creating SSL connection to host
Mar 30 16:32:59 servername sSMTP[73787]: SSL connection using ECDHE-RSA-AES128-GCM-SHA256
Mar 30 16:33:01 servername sSMTP[73787]: Sent mail for username@servername (221 2.0.0 closing connection t76sm135841ioi.4 - gsmtp) uid=1000 username=username outbytes=346


Second option is to use Postfix. Being a fully featured MTA, it may be an overkill but we'll go over it in case you want to send mail from other machines on your network (which was my usecase). Of course, I don't have to remind you about securing your MTA on your network. Open relaying used to be a thing but with advent of spamming, it's been disabled on most MTAs. We will be enabling this functionality, but we will only enable it to listen on a local interface and to accept connections from local networks. You will need to ensure that port 25 is disabled/blocked on your local firewall. Most ISPs block port 25 anyway.

Start off by installing Postfix:

$ cd /usr/ports/mail/postfix
$ sudo make install clean

Make sure to select cyrus-sasl auth module during the config dialog.

Once installed, created /usr/local/etc/postfix/ file and change the following options:

$ /usr/local/etc/postfix/
myhostname =
mydomain =
myorigin =
iner_interfaces =your LAN IP
mynetworks = (substitute your LAN network)

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

# TLS options
smtp_use_tls = yes
smtp_tls_security_level = encrypt
tls_random_source = dev:/dev/urandom

# Relay host
relayhost = []:587

Change the /usr/sbin/sendmail symlink to point to Mailwrapper. This also makes sure that mail will not go out via sSMTP.

$ sudo ln -sf /usr/sbin/mailwrapper /usr/sbin/sendmail

Start Postfix:

$ sudo service postfix start


$ sudo /usr/local/etc/rc.d/postfix start

Test mail flow via mail command or telnet:

$ echo "blah" | mailx -s "testing 123"
$ telnet localhost 25 << EOF
Subject: Testing 123
Hello, world!

You should see mail get processed in /var/log/maillog and come to your gmail account.

$ sudo tail -f /var/log/maillog
Mar 30 12:13:59 servername postfix/pickup[67198]: F348B112F: uid=1001 from=username
Mar 30 12:13:59 servername postfix/cleanup[67890]: F348B112F:
Mar 30 12:14:00 servername postfix/qmgr[67199]: F348B112F:, size=303, nrcpt=1 (queue active)
Mar 30 12:14:01 servername postfix/smtp[67892]: F348B112F:,[]:587, delay=1, delays=0.01/0/0.34/0.67, dsn=2.0.0, status=sent (250 2.0.0 OK 1427735641 t5sm8184300ign.12 - gsmtp)
Mar 30 12:14:01 servername postfix/qmgr[67199]: F348B112F: removed

You will also be able to send mail from other systems on your local LAN. Simply point your MUA/mail client to your server or use telnet to test connectivity.

At this point you should be done configuring a mail relay to send email to outside services. Above examples used gmail but any other service should work.


comments powered by Disqus