Article

Install sSMTP With Mailtrap for Web Application Development on Ubuntu 16.04

Published on: Mon, Feb 5, 2018 at 4:47 pm EST
Email Linux Guides Ubuntu

In this tutorial, you will learn how to install sSMTP, a simple forwarding server.

Mailtrap.io offers a fake email server for developers. Use it to send reports or emails during development phase to real addresses but intercept them at Mailtrap.io (they are never delivered to the final recipient) and see immediately how your HTML email is rendered or how a new CTO button stands out.

By integrating sSMTP and Mailtrap, your Ubuntu Development VPS can use the same email sending server for general mailing purposes, like cron job completion reporting.

This way, you are able to test your application with real email addresses and get a feel for how it behaves on the client side, all without flooding your personal or coworkers' mail accounts.

Prerequisites

  • Vultr VPS with Ubuntu 16.04 installed and updated.
  • Mailtrap.io account.
  • A non-root sudo user.
  • Text editor of your choice, we'll use nano.
  • Backup or a snapshot of your current installation.

Installation procedure

Log into your VPS as a regular user. Make yourself an administrator by issuing the following.

sudo su

Then confirm it with your password. Install the sSMTP package for the server side with mutt as the mail client for testing purposes.

apt install ssmtp mutt

Configuration

Now you have to make it work with your mailtrap account.

You will need your credentials and server names. In our case, the server is smtp.mailtrap.io with port 2525.

There are two files we are editing. First, while still logged in as root, edit the configuration file.

nano /etc/ssmtp/ssmtp.conf

By default, it looks like the following text.

# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=example.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES

At this point you might edit the entries to match your own values or just replace the whole content with your specific information. The capitalized entries contain your actual data. USERNAME@EXAMPLE.COM is the email from which you wish to send the emails. MAILTRAP_USERNAME and MAILTRAP_PASSWORD are generated for you at Mailtrap.io. Both are long, random strings. Look it up in your Mailtrap Inbox under SMTP Settings.

root=USERNAME@EXAMPLE.COM
mailhub=smtp.mailtrap.io:2525
AuthUser=MAILTRAP_USERNAME
AuthPass=MAILRTRAP_PASSWORD
FromLineOverride=Yes

Save the file. With nano press "Ctrl-X" and confirm with "Y".

The second file which needs to be edited is for mapping local user accounts to external email addresses, reverse aliasing. Bring the file up.

nano /etc/ssmtp/revaliases

Add your entries to the end so it looks something like this text.

# sSMTP aliases
#
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:USERNAME@EXAMPLE.COM:smtp.mailtrap.io:2525
username:USERNAME@EXAMPLE.COM:smtp.mailtrap.io:2525

Essentially, the system should send all the emails from the local account root as email user USERNAME@EXAMPLE.COM via the mailtrap server. Enter as many users as you need, each in its own line. If you only need root to send out messages, just omit other lines. You may come back later and add users as needed.

Save the file. With nano press "Ctrl-X" and confirm with "Y".

You're done. You might want to see it in action by sending yourself an email. To do that, create your message and send it.

echo "This is the body" | mutt -s "Hello World" SEND_TO_USERNAME@EXAMPLE.COM

Log into your Mailtrap account and check your Demo inbox. The mail should be waiting there already.

Conclusion

Finally, switch your terminal session from root back to the regular user.

exit

Optionally, add MAILTO=SEND_TO_USERNAME@EXAMPLE.COM to your crontab. To do a quick test, let's say, checking the subfolders in your /home folder every minute, edit the crontab.

crontab -e

Choose your editor if needed and add/edit these lines at the end so it resembles the following text.

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
mailto=SEND_TO_USERNAME@EXAMPLE.COM
* * * * * ls /home

This runs the ls /home every minute and because there is a MAILTO entry added, sends the response to that address. You can learn more about crontab tables here.

Save it on exit. Expect an email in Mailtrap every minute.

Want to contribute ?

You could earn up to $300 by adding new articles