In this tutorial, I’ll show you how to Install Mailtrain Newsletter Application on Ubuntu 18.04 LTS. Mailtrain is an open source, self-hosted newsletter application built from Node.js. Mailtrain is built on top of Nodemailer and it supports MySQL/MariaDB database backends.

Mailtrain is an alternative to commercial email marketing services such as Mailchimp, Sendgrid e.t.c.

Features of Mailtrain Newsletter Application

Below are the top features of Mailtrain newsletter application.

  • Easy Subscriber list management – You can manage your subscribers list, remove users, add users manually or via API.
  • Custom fields – Mailtrain supports custom fields such as numbers,drop-downs, text fields check boxes, tags merging and custom forms.
  • Email templates – Mailtrain provides a number of templates you can use for sending out emails to subscribers.
  • Large mail lists management – You can manage as many as >1 Million subscribers.
  • You can import  large user list using  CSV file import.
  • Support for RSS campaign: You can auto generate newsletter from RSS feed and use for sending emails out to subscribers.
  • List Segmentation: Send messages only to list subscribers that match predefined segmentation rules. No need to create separate lists with small differences.
  • GPG Encryption: Subscribers can upload their GPG public key to receive encrypted messages from the list.
  • Automation: You can define automation triggers to send specific messages when a user activates the trigger.

Mailtrain has below hardware requirements

  • 1 vCPU
  • 1024 MB RAM

You have two options of running Mailtrain on Ubuntu 18.04. You can choose to run Mailtrain on a Docker container or Manual Installation of Mailtrain on an Ubuntu 18.04 instance. We will consider the latter method.

Follow steps below to install Mailtrain Newsletter Application on Ubuntu 18.04 LTS.

Step 1: Update system & install dependencies

First, update your system packages to the latest releases.

sudo apt update
sudo apt -y install software-properties-common pwgen git dnsutils imagemagick build-essential python

Step 2: Install Node.js on Ubuntu 18.04

Node.js is a basic requirement of Mailtrain. Install it by running:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt -y install nodejs
sudo apt -y install gcc g++ make

Step 2: Install Redis on Ubuntu 18.04

With Mailtrain, you can use Redis session caching. Redis is used for session storage and for caching state between multiple processes. If you don’t have Redis already installed on Ubuntu 18.04, install it by following our guide below.

How to Install Redis on Ubuntu 18.04 / Debian 9

Step 4: Install MariaDB / MySQL Database Server

MySQL/MariaDB is used to store Mailtrain Data. Install MariaDB/MySQL database server using one of below articles:

Install MariaDB 10.x on Ubuntu 18.04 and CentOS 7

How to Install MySQL 8.0 on Ubuntu 18.04 / 16.04

After installation of MariaDB/MySQL database server, create a user and database for Mailtrain.

First login to MySQL shell as root user:

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 48
Server version: 10.3.11-MariaDB-1:10.3.11+maria~bionic-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

We will create a database for each of these server components.

CREATE DATABASE mailtrain;

Create a database user and grant privileges for created databases.

CREATE USER 'mailtrain'@'localhost' IDENTIFIED BY 'StrongPassword';
GRANT ALL PRIVILEGES  ON mailtrain.* TO 'mailtrain'@'localhost';

Also Grant Monitoring user SELECT permissions.

CREATE USER 'mailtrain_monitoring'@'localhost' IDENTIFIED BY 'StrongPassword'; 
GRANT  SELECT ON mailtrain.* TO 'mailtrain_monitoring'@'localhost';
FLUSH PRIVILEGES;

Confirm access by Logging into the database as postal user:

$ mysql -u mailtrain -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 49
Server version: 10.3.11-MariaDB-1:10.3.11+maria~bionic-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| mailtrain          |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]> QUIT
Bye

Step 4: Install and Configure Mailtrain on Ubuntu 18.04

Ensure you have git installed.

sudo apt -y install git

Clone Mailtrain from github to /srv/mailtrain

sudo git clone https://github.com/Mailtrain-org/mailtrain.git /srv/mailtrain

Import Mailtrain SQL schema to the database.

mysql -u mailtrain -p mailtrain < /srv/mailtrain/setup/sql/mailtrain.sql

A number of tables will be created by above import.

$ mysql -u mailtrain -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 53
Server version: 10.3.11-MariaDB-1:10.3.11+maria~bionic-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> USE mailtrain;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mailtrain]> SHOW tables;
+---------------------+
| Tables_in_mailtrain |
+---------------------+
| attachments         |
| campaign            |
| campaign_tracker    |
| campaigns           |
| confirmations       |
| custom_fields       |
| custom_forms        |
| custom_forms_data   |
| import_failed       |
| importer            |
| links               |
| lists               |
| queued              |
| rss                 |
| segment_rules       |
| segments            |
| settings            |
| subscription        |
| templates           |
| trigger             |
| triggers            |
| tzoffset            |
| users               |
+---------------------+
23 rows in set (0.001 sec)

MariaDB [mailtrain]> QUIT
Bye

Install required Node dependencies

# cd /srv/mailtrain
# npm install --production
# npm audit fix

Create Production Mailtrain configuration file

sudo cp /srv/mailtrain/config/default.toml /srv/mailtrain/config/production.toml
sudo vim /srv/mailtrain/config/production.toml

Configure HTTP port to listen on, Protocol and Listen Address:

[www]
# HTTP port to listen on
port=3000
# HTTP interface to listen on
host="0.0.0.0"
# Enable HTTPS
https=false

You can change port 3000 to other custom port and choose to enable or disable https. if you choose to enable HTTPS, make sure you provide paths to your certificate private key and certificate file.

Generate random Secret for signing the session ID cookie.

sudo apt -y install pwgen
pwgen 32

Set generated secret on the file.

secret="du6yahghohnu2lu8nee4ahmaithooWa2"

Also provide database connection details.

[mysql]
host="localhost"
user="mailtrain"
password="StrongPasword"
database="mailtrain"

If you have redis installed and configured, provide connection details as well.

[redis]
# enable to use Redis session cache or disable if Redis is not installed
enabled=true
host="localhost"
port=6379
db=5
# Uncomment if your Redis installation requires a password
#password=""

Change other settings as you see fit and save the file. When done, create a configuration file for the report worker.

sudo  cp /srv/mailtrain/workers/reports/config/default.toml /srv/mailtrain/workers/reports/config/production.toml
sudo vim /srv/mailtrain/workers/reports/config/production.toml

Provide reporting/monitoring database user created earlier.

[mysql]
host="localhost"
user="mailtrain_monitoring"
password="StrongPassword"
database="mailtrain"
port=3306
charset="utf8mb4"
timezone="local"

Cate a systemd service unit file under  /etc/systemd/system/ directory.

sudo vim /etc/systemd/system/mailtrain.service

Paste the following contents to the file:

[Unit]
Description=Mailtrain server
Requires=mysql.service
After=syslog.target network.target

[Service]
Environment="NODE_ENV=production"
WorkingDirectory=/srv/mailtrain
ExecStart=/usr/bin/node index.js
Type=simple
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Reload systemd and start the service.

systemctl daemon-reload
sudo systemctl enable --now mailtrain

Check service status, it should return running state.

$ systemctl status mailtrain.service
* mailtrain.service - Mailtrain server
   Loaded: loaded (/etc/systemd/system/mailtrain.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-12-23 05:15:35 PST; 19s ago
 Main PID: 22340 (mailtrain)
    Tasks: 20 (limit: 2319)
   CGroup: /system.slice/mailtrain.service
           |-22340 mailtrain
           `-22370 /usr/bin/node /srv/mailtrain/services/sender.js

Dec 23 05:15:37 ubuntu-01 node[22340]: info sql Update 28 applied
Dec 23 05:15:37 ubuntu-01 node[22340]: info sql Update 29 applied
Dec 23 05:15:38 ubuntu-01 node[22340]: info sql Update 30 applied
Dec 23 05:15:38 ubuntu-01 node[22340]: info sql Update 31 applied
Dec 23 05:15:38 ubuntu-01 node[22340]: info sql Update 32 applied
Dec 23 05:15:38 ubuntu-01 node[22340]: info sql Update 33 applied
Dec 23 05:15:38 ubuntu-01 node[22340]: info sql Database check completed
Dec 23 05:15:38 ubuntu-01 node[22340]: info Express WWW server listening on port 3000
Dec 23 05:15:38 ubuntu-01 node[22340]: verb UTC Updating timezone offsets
Dec 23 05:15:38 ubuntu-01 node[22340]: info Service All services started

Once Mailtrain is running. it should bind to port 3000 unless you changed it in the configuration file.

# ss -tunelp | grep 3000 
tcp   LISTEN  0       128  0.0.0.0:3000  0.0.0.0:*  users:(("mailtrain",pid=22340,fd=14)) ino:77855 sk:48 <->

Configure Nginx Reverse Proxy

We can use Nginx to Proxy requests to Mailtrain service.

sudo apt -y install nginx
sudo cp /srv/mailtrain/setup/mailtrain-nginx.conf /etc/nginx/conf.d/

Edit the file to set your domain name.

server_name example.com www.example.com;

Validate Nginx configurations.

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Start and enable Nginx service

sudo systemctl restart nginx
sudo systemctl enable nginx

Access Mailtrain URL on http://example.com

The default logins are:

Username: admin
Password: test

Change the password on after logging in on Admin > Account

Provide the old password and the new password then click “Update” button.

In our next article, we will cover how to handle “bounced messages” in Mailtrain. Enjoy using Mailtrain on Ubuntu 18.04 LTS to manage your email subscription lists.