Install Mailtrain v1.24 on Ubuntu 20.04 Server Without Docker

This tutorial will show you how to install Mailtrain on Ubuntu 20.04 without Docker. Mailtrain is an open-source self-hosted newsletter application, an alternative to commercial email service providers like Mailchimp. You can use Mailtrain to send newsletters to your email subscribers via your own email server or by using any SMTP relay service (Mailjet, SendGrid, AmazonSES, Mailgun, etc).

Update: Mailtrain V2 is released. New users should use V2 instead of V1.24: How to Install Mailtrain v2 on Ubuntu 20.04 Server

Mailtrain is released under the terms of GPL v3.0 license, built on Node.js and MySQL/MariaDB. The latest version is v1.24.1, released on September 28, 2018. Features of Mailtrain are as follows:

  • It allows you to easily manage large mailing lists (like 1 million subscribers).
  • You can add subscribers manually, through the API, or import from a CSV file.
  • It supports custom fields (text fields, numbers, drop-downs or check boxes), merge tags and custom forms.
  • List segmentation.
  • RSS campaign: auto-generate newsletter from RSS feed and send it to subscribers.
  • Subscribers can upload their GPG public keys and Mailtrain will encrypt the newsletter for them.
  • Allows you to check individual click statistics for every link.
  • Advanced email template editors and HTML code editor.
  • Automation: send specific emails when user activates your predefined trigger.
  • You can create open email list (allow public subscription) and closed email list (subscribers are added to the list by admin).
  • It allows you to resume old email campaigns (send old campaigns to new subscribers).

Step 1: Choose the Right Hosting Provider

Self-hosting can save you a lot of money. Mailtrain is free and open-source software. You only need to pay $26/month for the VPS (virtual private server), which can run a full-featured mail server and the Mailtrain email marketing platform. It can easily handle millions of subscribers. So your total cost is always $26/month no matter how many subscribers you have. If you own millions of subscribers on MailChimp, the cost would be thousands of dollars per month.

It’s not an easy task to find a VPS (Virtual Private Server) provider suitable for email hosting and email marketing. Many hosting companies like DigitalOcean blocks port 25. DigitalOcean would not unblock port 25, so you will need to set up SMTP relay to bypass blocking, which can cost you additional money. If you use Vultr VPS, then port 25 is blocked by default. They can unblock it if you open a support ticket, but they may block it again at any time if they decide your email sending activity is not allowed. Vultr actually may re-block it if you use their servers to send newsletters.

Another problem is that big well-known hosting providers like DigitalOcean or Vultr are abused by spammers. Often the server IP address is on several blacklists. Vultr has some entire IP ranges blacklisted.

ScalaHosting is a very good option to run a mail server because

  • They don’t block port 25.
  • The IP address isn’t on any email blacklist. (At least this is true in my case. I chose the Dallas data center.) You definitely don’t want to be listed on the dreaded Microsoft Outlook IP blacklist or the SpamRats blacklist. Some blacklists block an entire IP range and you have no way to delist your IP address from this kind of blacklists.
  • You can edit PTR record to improve email deliverability.
  • They allow you to send newsletters to your email subscribers with no hourly limits or daily limit, whatsoever. Note that you are not allowed to send spam, also known as unsolicited bulk email. If the recipient doesn’t explicitly give you permission to send emails, and you send emails to them, that’s unsolicited email.

I recommend following the tutorial linked below to properly set up a Linux VPS server on ScalaHosting. Use coupon code linuxbabe2021 on ScalaHosting payment page to save $100 if you choose to pay 12 months upfront.

You also need a domain name. I registered my domain name from NameCheap because the price is low and they give you whois privacy protection free for life.

If you don’t have your own mail server yet, I recommend using the free iRedMail program to quickly set up your own mail server before installing Mailtrain, so you don’t have to spend money on commercial SMTP relay service.

Step 2: Install MariaDB Database Server

Your subscribers data will be stored in a database. Mailtrain supports MySQL and MariaDB. MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. So let’s install the MariaDB database server.

Enter the following command to install it on Ubuntu 20.04.

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mariadb

Sample output:

 mariadb.service - MariaDB 10.3.22 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: e>
     Active: active (running) since Mon 2020-04-20 15:31:14 HKT; 52s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 1826628 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 30 (limit: 9451)
     Memory: 73.0M
     CGroup: /system.slice/mariadb.service
             └─1826628 /usr/sbin/mysqld

If it’s not running, start it with this command:

sudo systemctl start mariadb

To enable MariaDB to automatically start at system boot time, run

sudo systemctl enable mariadb

Now run the post installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

ubuntu-20.04-install-LAMP-stack-MariaDB

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Note that the letter Y is capitalized, which means it’s the default answer.)

Install-LAMP-stack-on-Ubuntu-20.04-MariaDB-Database-serverCheck MariaDB server version information.

mariadb --version

Output:

mariadb  Ver 15.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Step 3: Create a Database and User for Mailtrain

Now we need to log in to MariaDB console and create a database and user for Mailtrain. By default, the MaraiDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can use username and password of the OS to log into MariaDB console. So you can run the following command to login without providing MariaDB root password.

sudo mariadb -u root

Create a database for Mailtrain using the following command. I named it mailtrain, but you can use whatever name you like. (Don’t leave out the semicolon.)

create database mailtrain;

Then enter the command below to create a database user for Mailtrain and grant all privileges of the mailtrain database to the user. Replace mtuser and your-password with your preferred username and password.

grant all privileges on mailtrain.* to mtuser@localhost identified by 'mtuser_password';

Next, create a user with read only access to mailtrain database. I named this user mt_readonly.

grant select on mailtrain.* TO mt_readonly@localhost identified by 'mt_readonly_password';

Flush the privileges table for the changes to take effect and then get out of MariaDB console.

flush privileges;

exit;

Step 4: Install Node.js

Mailtrain is built on Node.js, which is a JavaScript run-time environment that translates human-readable JavaScript code into machine code. So we need to install Node.js on Ubuntu 20.04 in order to run Mailtrain. Mailtrain requires Node.js 7+. The latest version of Node.js is v14. However, I don’t recommend using the latest version, because it’s not compatible with Mailtrain v1.24. For best compatibility, I recommend installing Node.js 8 from the Snap Store.

If you have installed Node.js from the official APT repository, you need to uninstall it, because it would interfere with the Snap version of Node.js.

sudo apt remove nodejs

Next, install the snap daemon.

sudo apt install snapd

There are multiple versions of Node.js in the Snap Store, which can be seen with:

snap info node

Output:

channels:
  latest/stable:    –                                                     
  latest/candidate: –                                                     
  latest/beta:      –                                                     
  latest/edge:      15.0.0-nightly2020042524a4e615 2020-04-25 (2668) 30MB classic
  14/stable:        14.0.0                         2020-04-23 (2647) 30MB classic
  14/candidate:     ↑                                                     
  14/beta:          ↑                                                     
  14/edge:          ↑                                                     
  13/stable:        13.13.0                        2020-04-14 (2635) 29MB classic
  13/candidate:     ↑                                                     
  13/beta:          ↑                                                     
  13/edge:          ↑                                                     
  12/stable:        12.16.2                        2020-04-23 (2644) 21MB classic
  12/candidate:     ↑                                                     
  12/beta:          ↑                                                     
  12/edge:          ↑                                                     
  11/stable:        11.15.0                        2019-06-26 (2336) 19MB classic
  11/candidate:     ↑                                                     
  11/beta:          ↑                                                     
  11/edge:          ↑                                                     
  10/stable:        10.20.1                        2020-04-23 (2638) 20MB classic
  10/candidate:     ↑                                                     
  10/beta:          ↑                                                     
  10/edge:          ↑                                                     
  9/stable:         9.11.2                         2018-12-14 (1407) 17MB classic
  9/candidate:      ↑                                                     
  9/beta:           ↑                                                     
  9/edge:           ↑                                                     
  8/stable:         8.16.0                         2019-06-24 (2310) 16MB classic
  8/candidate:      ↑                                                     
  8/beta:           ↑                                                     
  8/edge:           ↑                                                     
  6/stable:         6.17.1                         2019-06-24 (2311) 13MB classic
  6/candidate:      ↑                                                     
  6/beta:           ↑                                                     
  6/edge:           ↑                                                                    

We can install Node.js from the 8/stable channel.

sudo snap install node --classic --channel=8/stable

To check your Node.js and npm version, run

node -v

npm -v

Output:

mailtrain nodejs ubuntu 20.04

Note: If this is the first time you install a Snap package on the server, you need to log out and log back in, in order to use the snap version of Ruby.

To compile and install native addons from npm you also need to install build tools:

sudo apt install -y build-essential

Step 5: Run Mailtrain

Go to /var/www/ and fetch Mailtrain files from Github.

cd /var/www/

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

Import the initial SQL data to the mailtrain database by using the following command. You need to enter the password of mtuser.

mariadb -u mtuser -p mailtrain < /var/www/mailtrain/setup/sql/mailtrain.sql

Create the production.toml configuration file.

sudo nano /var/www/mailtrain/config/production.toml

In this file, you can add configurations that override the default configurations in /var/www/mailtrain/config/default.toml file. We only need to add the following configurations.

user="mailtrain"
group="mailtrain"
[log]
level="error"
[www]
secret="Replace this with some random characters"
[mysql]
user="mtuser"
password="mtuser_password"
[queue]
processes=5

Save and close the file. Then create the mailtrain user and group for the mailtrain daemon to run as. Note that we don’t need to create password for this user, so we create a system user instead of a normal user.

sudo adduser --system --group mailtrain

Create a configuration file for the report worker.

sudo nano /var/www/mailtrain/workers/reports/config/production.toml

Add the following configurations. This report worker will have read-only access to mailtrain database.

[log]
level="error"
[mysql]
user="mt_readonly"
password="mt_readonly_password"

Save and close the file. Then change the permission of /var/www/mailtrain/.

sudo chown mailtrain:mailtrain /var/www/mailtrain/ -R

sudo chmod o-rwx /var/www/mailtrain/config/

Install Python2.7 and create a symbolic link.

sudo apt install python2.7

sudo ln -s /usr/bin/python2.7 /usr/bin/python

And install required node packages.

cd /var/www/mailtrain

sudo -u mailtrain npm config set scripts-prepend-node-path true

sudo -u mailtrain npm install --no-progress --production --unsafe-perm=true

Copy the Mailtrain systemd service unit file to /etc/systemd/system/ directory.

sudo cp /var/www/mailtrain/setup/mailtrain.service /etc/systemd/system/

Open this file.

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

Change /opt/mailtrain to /var/www/mailtrain. And change /usr/bin/node to /snap/bin/node because we are using the Snap version of Node.js.

You may also want to comment out the following line. Because, with the following line enabled, if MySQL/MariaDB restarts, then the mailtrain.service will also restart, and if the MySQL/MariaDB server is being upgraded, then mailtrain.service will stop. I think there’s no need to require the mysql.service.

Requires=mysql.service

Save and close this file. Then start mailtrain.service.

sudo systemctl start mailtrain.service

Check its status. Make sure it is running.

systemctl status mailtrain.service

Output:

 mailtrain.service - Mailtrain server
     Loaded: loaded (/etc/systemd/system/mailtrain.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-04-26 10:26:37 HKT; 11s ago
   Main PID: 3058360 (mailtrain)
      Tasks: 20 (limit: 9451)
     Memory: 228.6M
     CGroup: /system.slice/mailtrain.service
             ├─3058360 mailtrain
             └─3058396 /snap/node/2310/bin/node /var/www/mailtrain/services/sender.js

Enable auto-start at system boot time.

sudo systemctl enable mailtrain.service

Now that Mailtrain is running, you can access Mailtrain web interface via port 3000.

your-server-ip:3000

mailtrain ubuntu 18.04 install

Step 6: Set up Reverse Proxy and Enabling HTTPS

Before using the Mailtrain web interface, let’s put it behind Nginx and then enable HTTPS. Install Nginx web server on Ubuntu 20.04 with:

sudo apt install nginx

Now you can copy the example mailtrain-nginx.conf file to /etc/nginx/conf.d/ directory.

sudo cp /var/www/mailtrain/setup/mailtrain-nginx.conf /etc/nginx/conf.d/

Open this file.

sudo nano /etc/nginx/conf.d/mailtrain-nginx.conf

Find the following line.

server_name mailtrain.org www.mailtrain.org;

Change the value of server_name parameter to your own domain name like newsletter.your-domain.com. Don’t forget to create DNS A record for this sub-domain.

server_name newsletter.linuxbabe.com;

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test if successful, reload Nginx for the changes to take effect.

sudo systemctl reload nginx

You should now be able to access Mailtrain web interface via your sub-domain: newsletter.your-domain.com.

Now edit /var/www/mailtrain/config/production.toml file.

sudo nano /var/www/mailtrain/config/production.toml

In the [www] section, add the following two lines to indicate that mailtrain is behind the Nginx proxy and make it listen on local host address only.

host="127.0.0.1"
proxy=true

Save and close the file. Then restart Mailtrain for the change to take effect.

sudo systemctl restart mailtrain

Step 7: Enable HTTPS With Let’s Encrypt

Install Let’s Encrypt client (certbot) on your Ubuntu 20.04 server.

sudo apt install certbot python3-certbot-nginx

Then you can use the Nginx plugin to automatically obtain and install a TLS certificate by executing the following command.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d newsletter.your-domain.com

docker mailtrain

Now you can access Mailtrain web interface via your domain name and a secure HTTPS connection.

Install-Mailtrain-on-Ubuntu-20.04-without-docker

htop command tells me that my server is using about 500MB RAM. If I run Mailtrain with Docker, it’s about 900MB.

mailtrain system requirements linux

Login with username admin and password test. Then change your account email address and password. Go to settings page to change default configurations. You should change the service address from http://localhost:3000/ to your sub-domain (https://newsletter.your-domain.com).

Note: The admin account is an easy target for hackers. For best security, it’s recommended that you change the username from admin to something else. The Mailtrain web interface doesn’t provide such option, but you can change the username in MariaDB database using SQL command.

In the Mailer Settings, you can use SMTP if you have your own email server or use Amazon SES. Actually, you can also use other SMTP relay services in the SMTP tab. In the screenshot below, I use my own email server. Emails will be submitted on port 587 with STARTTLS encryption.

mailtrain smtp settings

If Mailtrain is installed on your email server, then you should use the following SMTP settings. The hostname should be 127.0.0.1 and port should 25. There’s no need to enable encryption or authentication when talking to localhost.

mailtrain smtp settings no encryption and authentication

Save your settings. Then you can create a list to test the functionalities of Mailtrain.

How to Enable Redis Cache

You can install Redis server for session cache. This will allow Mailtrain to run 5 processes instead of the default single process, which will speed up email delivery if you have lots of email subscribers (Note that this will increase the memory usage like about 250MB).

sudo apt install redis

After it’s installed, Redis should be automatically started. You can check its status with:

systemctl status redis

Sample output:

 redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-04-26 11:02:03 HKT; 4min 23s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 3059455 (redis-server)
      Tasks: 4 (limit: 9451)
     Memory: 2.3M
     CGroup: /system.slice/redis-server.service
             └─3059455 /usr/bin/redis-server 127.0.0.1:6379

If it’s not running, you can start it with:

sudo systemctl start redis

Enable auto-start at boot time:

sudo systemctl enable redis

Next, edit Mailtrain configuration file.

sudo nano /var/www/mailtrain/config/production.toml

Add the following two lines at the end of the file to enable Redis.

[redis]
enabled=true

Save and close the file. Then restart Mailtrain.

sudo systemctl restart mailtrain

How to Handle Bounce Messages in Mailtrain

Sooner or later, your email list will contain addresses that you can’t send emails to. For example, when a subscriber who uses a company email address leaves the company, that email address will be deleted. So your email server will receive bounce message saying that the email can’t be delivered.

If you use an SMTP relay service to send email, they will handle bounce messages for you. If you use your own email server to send email, then you need to handle bounce messages in Mailtrain. Mailtrain offers two ways to handle bounced messages.

  • via VERP
  • via Postfix log

I personally use the VERP method, because it’s widely used in the email community and also because the second method causes high CPU usage on my server.

VERP Bounce Handling

With VERP (variable envelope return path), your email list manager uses unique envelope addresses for each subscriber. To enable VERP, edit the production.toml file.

sudo nano /var/www/mailtrain/config/production.toml

If your Mailtrain host has no SMTP server running, then add the following text.

[verp]
enabled=true
port=25
disablesenderheader=true

If your Mailtrain server has a SMTP server like Postfix running, then add the following text. The bounce handling server will listen on 127.0.0.1:2525.

[verp] 
enabled=true 
port=2525 
host="127.0.0.1"
disablesenderheader=true

Save and close the file. Then restart Mailtrain for the changes to take effect.

sudo systemctl restart mailtrain

In Mailtrain web interface, go to Settings -> VERP Bounce Handlding. Check Use VERP to catch bounces. Save your settings.

mailtrain verp bounce handling

Next, you need to create a MX record for the server hostname (bounces.your-domain.com), then add A record for this hostname, pointing to your Mailtrain host, so bounce messages can be sent to your Mailtrain host. Each subscriber in your list will have an unique envelope address like [email protected].

Note that if you deployed DMARC record for your domain name, then the SPF alignment must be set to relaxed mode. If it’s set to strict mode, then your newsletter could fail DMARC check.

If the Mailtrain bounce handling server is listening on port 2525 of 127.0.0.1 and Postfix SMTP server is listening on port 25 of the public IP address, then you need to set up transport map so that Postfix can relay the bounce message to Mailtrain. Create the transport map file.

sudo nano /etc/postfix/transport

Add the following line to this file. This tells Postfix to relay emails with address like [email protected] to the Mailtrain bounce handling server.

bounces.your-domain.com  smtp:[127.0.0.1]:2525

Save and close the file. Then generate the index file.

sudo postmap /etc/postfix/transport

Edit Postfix main configuration file.

sudo nano /etc/postfix/main.cf

Add the following line to the file.

transport_maps = hash:/etc/postfix/transport

Note: If you used iRedMail to set up your mail server, then the transport_maps parameter has some other values. You should append the value at the end like below.

transport_maps =
     proxy:mysql:/etc/postfix/mysql/transport_maps_user.cf
     proxy:mysql:/etc/postfix/mysql/transport_maps_maillist.cf
     proxy:mysql:/etc/postfix/mysql/transport_maps_domain.cf
     hash:/etc/postfix/transport

Save and close the file. Then restart Postfix for the change to take effect.

sudo systemctl restart postfix

Handling Bounced Messages via Postfix Log

This assumes that Mailtrain is installed on your email server.

Mailtrain is able to detect bounced messages from Postfix mail log by finding lines that contains status=bounced. First, you need to enable the Postfix log reading server. Edit the production.toml file.

sudo nano /var/www/mailtrain/config/production.toml

Add the following text.

[postfixbounce]
enabled=true

Save and close the file. Then restart Mailtrain for the changes to take effect.

sudo systemctl restart mailtrain

Now the Postfix log reading server is listening on 127.0.0.1:5699. To let it read Postfix log, run the following command.

tail -F /var/log/mail.log | nc localhost 5699 -

To make it run in the background, you can create a systemd service unit.

sudo nano /etc/systemd/system/bouncetail.service

Add the following text.

[Unit]
Description=Postfix bounce notifier
After=mailtrain.service

[Service]
ExecStart=/bin/sh -c '/usr/bin/tail -F /var/log/mail.log | nc localhost 5699 -'
Type=simple
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Save and close the file. Then start this service and enable auto-start at boot time.

sudo systemctl start bouncetail

sudo systemctl enable bouncetail

Be sure to restart bouncetail service after you restart mailtrain service. I found the bouncetail service causes high CPU usage on my server, so I don’t use this method.

Update

After using Mailtrain on my own server for some time, I found that they are other reasons that cause email bounce. The following are soft bounces.

  • The recipient’s mailbox is full.
  • Your IP address is on a blacklist.
  • The recipient email server is down or offline.

There’s also hard bounce, i.e. the recipient’s email address doesn’t exist. By default, Mailtrain unsubscribes an email address if one bounce is detected, which is fine for hard bounce, but it’s a good idea to allow several soft bounces before you unsubscribe the bad email address.

To bypass email blacklists, please read the following article.

If you changed the SMTP sending settings in Mailtrain, but it seems still using the old settings, then you should try restarting Mailtrain and Redis.

sudo systemctl restart mailtrain redis-server

How to Re-send Bounced Emails

If you think some bounced emails are due to technical errors on your mail server, then you can re-send bounce emails in Mailtrain.

First, you need to know the campaign ID, which can be found in the campaign URL like this one: https://newsletter.linuxbabe.com/campaigns/view/818. The campaign ID is 818.

Then log into MariaDB/MySQL console:

sudo mysql -u root

Select the mailtrain database.

use mailtrain;

Show the campaign table.

select * from campaign__818;

Sample output:

MariaDB [mailtrain]> select * from campaign__818;
+-----+------+---------+--------------+--------+---------------------------------------------+
| id  | list | segment | subscription | status | response                                    | 
+-----+------+---------+--------------+--------+---------------------------------------------+
|   1 |    1 |      10 |         6194 |      1 | 250 2.0.0 Ok: queued as DC5E4660FAE         |
|   2 |    1 |      10 |         6560 |      3 | Greeting never received                     |
|   3 |    1 |      10 |         6616 |      1 | 250 2.0.0 Ok: queued as 2CA9C660FB1         |
|   4 |    1 |      10 |         6750 |      3 | Greeting never received                     | 
|   5 |    1 |      10 |         6870 |      3 | Greeting never received                     |

This will show each sent email status. Status 3 means the email is bounced. Delete the bounced record from the table.

DELETE FROM campaign__818 WHERE status = 3;

Now you can click the Continue button on the Mailtrain campaign page to re-send emails to bounced recipients.

Uploading Images in Mailtrain

To upload images in Mailtrain, you need to install the imagemagick package. Without it, Mailtrain might throw errors when you upload images.

sudo apt install imagemagick

How to Insert a Signup Form on Your Website

First, you need to enable cross-origin resource sharing. Edit the production.toml file.

sudo nano /var/www/mailtrain/config/production.toml

Add the following line in the file to whitelist your website.

[cors]
# Allow subscription widgets to be embedded
origins=['https://www.example.com']

Save and close the file. Then restart Mailtrain for the change to take effect.

sudo systemctl restart mailtrain

Next, add the following code on your website to display the sign up form. Replace the red colored text with the Mailtrain domain name and your own list ID.

<div data-mailtrain-subscription-widget data-url="https://newsletter.example.com/subscription/8ljDT9KMGr/widget">
     <a href="https://newsletter.example.com/subscription/8ljDT9KMGr">Subscribe to our list</a>
</div>
<script src="https://newsletter.example.com/subscription/widget.js"></script>

Then add custom CSS rules to make it more visually appealing.

How to Create Additional Users in Mailtrain

Mailtrain v2 will allow the admin to create multiple users with granular user permissions and flexible sharing. But with the current version v.1.24.1, you can create users only through the database. Log into MySQL/MariaDB console.

sudo mysql -u root

Use the mailtrain database.

USE mailtrain;

Then create a user entry with the following SQL command.

INSERT INTO `users` (`username`, `password`, `email`, `access_token`, `reset_token`, `reset_expire`, `created`) VALUES ('your-username',PASSWORD("your-password"),'[email protected]',NULL,NULL,NULL,NOW());

Exit the database server.

EXIT;

I found that users created in this way can’t log in to the Mailtrain web interface the first time. Users need to use the “forgot password” link on the login page to reset his/her password, then the login should work. Also note that all users have administrative permission in the Mailtrain web interface.

How To Import Email List From MailChimp

First, you need to go to the Audience tab in your MailChimp dashboard. Click the View Contacts button to show all subscribers. Then click the Export Audience button to export your list as a CSV file.

Then go to Mailtrain dashboard and select your email list. Select the List Actions drop-down button and click Import Subscribers.

mailtrain import from mailchimp

In the next page, you will need to upload the CSV file downloaded from MailChimp.

Mailtrain import subscribers from CSV file

Then you need to match the fields in MailChimp to the fields in your Mailtrain email list. If some of the fields in MailChimp don’t exist in Mailtrain email list, then you can create them in Mailtrain dashboard.

match fields mailchimp mailtrain

Please note that if your MailChimp fields contain radio buttons, then you should also create radio buttons in Mailtrain fields. However, you need to segment your MailChimp list based on the value of the radio button, then export these segments to different CSV files and import them to Mailtrain one segment at a time. That’s because Mailtrain doesn’t provide a way to match each radio button values from MailChimp.

Cron Job to Automatically Clean Unsubscribed Email Addresses

You can manually delete unsubscribed email addresses in the Mailtrain web-based admin panel, but it’s much more efficient to delete them using MySQL/MariaDB commands, then you just create a Cron job to automate this task.

First, log into MySQL/MariaDB database server.

sudo mysql -u root

Then connect to the mailtrain database.

use mailtrain;

The subscribers’ info are stored in the subscription tables. If you have several mailing lists, then you will have several subscription tables. You can list all these tables using the following SQL command.

show tables like 'subscription%';

Output on my server.

+-------------------------------------+
| Tables_in_mailtrain (subscription%) |
+-------------------------------------+
| subscription                        |
| subscription__1                     |
| subscription__2                     |
+-------------------------------------+

The first table is an empty table. I have two lists in Mailtrain. They are stored in subscription__1 and subscription__2 table. Subscribed email addresses have status code set to 1. Unsubscribed email addresses have status code set to 2. So, to delete unsubscribed email addresses, you can run the following SQL commands.

DELETE FROM subscription__1 where status = '2';
DELETE FROM subscription__2 where status = '2';

To exit MySQL/MariaDB database server, run

exit;

Now open the root user’s crontab file.

sudo crontab -e

Add the following two lines.

# Delete unsubscribed email addresses from mailing list daily
@daily /usr/bin/mysql -u root mailtrain -Bse "DELETE FROM subscription__1 where status = '2';DELETE FROM subscription__2 where status = '2';"

Save and close the file. And you’re done.

How to Export Your Email Lists in Mailtrain

There’s no export button in the Mailtrain admin panel. However, if you have installed phpMyAdmin on the server, you can export your email lists from there. Select the mailtrain database in phpMyAdmin, then select the table that stores your email lists. In my case, the subscription__1 and subscription__2 tables store subscribers’ information.

Then click the export button to save the table as a file. Note that there are 2 export buttons. You need to click the second export button to export the entire table. The first export button will only export the current visible records in the table.

export email lists in mailtrain

Next, you can choose the file format for the export file. Finally, click the Go button.

mailtrain export list as csv

How to Hide the Mailtrain Home Page

If you don’t want visitors to see your mailtrain home page, you can set up a 301 permanent redirect to redirect visitors to your website’s home page, by adding the following code in Mailtrain’s Nginx configuration file.

location = / {
   return 301 http://www.your-domain.com;
}

Save and close the file. Then reload Nginx for the change to take effect.

sudo systemctl reload nginx

You need to remember the Mailtrain login URL.

https://newsletter.your-domain.com/users/login

By default, the login button will redirect you to the Mailtrain home page, so you need to remember another URL.

https://newsletter.your-domain.com/lists

This allows you to manage your lists and other stuff in the dashboard.

How to Insert Options on the Signup Form

If you want to give options on the signup form for the visitor to select, then you need to create custom fields. Go to your list and select custom fields in the List Actions drop-down menu and create custom field.

First, you need to create a field that will become the header question for the option list. In Field Type, you can select radio button (for single choice) or checkboxes (for multiple choices). In Group, don’t select anything, because this field will become a group for the options.

Then create custom field for each option. You need to choose option for a group value in Field Type, and choose the previous custom filed name in Group.

How to Change Field Type

By default, the Mailtrain web interface doesn’t allow you to change filed type, but you can change it in the mailtrain database. For example, I need to change a field type from checkboxes to radio button.

I recommend backing up the database before changing anything in the database. You can run the following command to backup mailtrain database.

sudo mysqldump -u root mailtrain > mailtrain.sql

Then log into MariaDB database server.

sudo mysql -u root

Use the mailtrain database.

USE mailtrain;

All custom fields are stored in the custom_fields table. You can check its content with:

SELECT * FROM custom_fields;

Find the custom field you want to modify and run the following SQL command to change its type. Replace the id number with your own.

UPDATE custom_fields SET type = 'radio' Where id = 3;

Exit the database server.

EXIT;

How to Clean Your Email List in Mailtrain

Unengaged subscribers won’t make you money and because they don’t open your email, mailbox providers will reduce your domain reputation. To make sure your future email will get into the inbox, you should remove unengaged subscribers from your email list.

You can create a segment for people who didn’t open your email in the last 60 days, then delete their email addresses. The rule for this segment is as follows:

mailtrain segment latest open

RAM Requirement

Mailtrain can use quite a lot of RAM. Make sure your server has at least 1GB free RAM. For example, if the current RAM usage is 4GB, then the total RAM should be at least 5GB. If RAM runs out, emails in Mailtrain could be bounced.

Wrapping Up

I hope this tutorial helped you install Mailtrain on Ubuntu 20.04. You can go to Mailtrain wiki to find more information on using Mailtrain. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care.

Rate this tutorial
[Total: 1 Average: 5]

15 Responses to “Install Mailtrain v1.24 on Ubuntu 20.04 Server Without Docker

  • Very comprehensive and clear. Thank you for your efforts

  • Thank you for the awesome tutorial, one question, when I try to import my subscribers list with a CSV file that measures 1.58 MB (windows) I am given the message: “413 Request Entity Too Large” I read elsewhere that updating the Nginx configuration client max body size would fix this, though I do not see this line of code when I tried opening up “/etc/nginx/nginx.conf”

    Any assistance on this would be greatly appreciated, cheers!

    • Xiao Guoan (Admin)
      4 years ago

      If this line is not in the file, then add this line in the http section of this file.

  • Hi, recently I did a clean install for a production site, but when I try to use the gmail smtp (a subscription account of gsuite) got the folloging log
    454 4.7.0 Too many login attempts, please try again later
    And cant send more than 200 mails, I have tried disabling SMTP authentication in mailtrain settings, and configuring the smtp relay service to not require authentication and allowing only from the ip

    Any hint?

  • Hi! Just did a fresh installation on a Ubuntu 20.04 (without Docker), but unfortunately the mailtrain service crashes if i try to upload a image to the Mosaico Gallery. The Apache Error logs just throws a:

    [proxy_http:error] [pid 37394:tid 140712982828800] (20014)Internal error (specific information not available): [client xxxxx:65294] AH01102: error reading status line from remote server 127.0.0.1:3000, referer: https://mailtrain.xxx.xx/mosaico/editor?id=3&type=template

    Unfortunately the mailtrain service itself doesn’t generate a logfile at all and a strace to the process didn’t help to find out whats wrong. In syslog i see the following:

    Jun 19 15:38:26 post node[59334]: events.js:183
    Jun 19 15:38:26 post node[59334]: throw er; // Unhandled ‘error’ event
    Jun 19 15:38:26 post node[59334]: ^
    Jun 19 15:38:26 post node[59334]: Error: spawn convert ENOENT
    Jun 19 15:38:26 post node[59334]: at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
    Jun 19 15:38:26 post node[59334]: at onErrorNT (internal/child_process.js:362:16)
    Jun 19 15:38:26 post node[59334]: at _combinedTickCallback (internal/process/next_tick.js:139:11)
    Jun 19 15:38:26 post node[59334]: at process._tickDomainCallback (internal/process/next_tick.js:219:9)
    Jun 19 15:38:26 post systemd[1]: mailtrain.service: Main process exited, code=exited, status=1/FAILURE
    Jun 19 15:38:26 post systemd[1]: mailtrain.service: Failed with result ‘exit-code’.

    Any ideas?

    Cheers,
    joey.

    • Xiao Guoan (Admin)
      4 years ago

      Perhaps you need to install the imagemagick package.

      sudo apt install imagemagick

      I can upload images to the Mosaico Gallery in Mailtrain without any errors. I use Nginx.

  • Hello,

    I just did fresh installation. It worked everything great but I have problem with my domain if I am trying to select newsletter.my-domain.com it seems to be controll by my-domain.com/mail. I did use your tutorial with my own email server (iRedMail). Have I done something wrong with installation of iRedMail or just messed up my DNS?

    Any hint?

    Thanks a lot!

    • Xiao Guoan (Admin)
      3 years ago

      It seems iRedMail doesn’t use the /etc/nginx/conf.d/ directory. Move the virtual host file to /etc/nginx/sites-enabled/ directory.

      sudo mv /etc/nginx/conf.d/mailtrain-nginx.conf /etc/nginx/sites-enabled

      Then reload Nginx.

      sudo systemctl reload nginx
  • Gerardo gerMdz
    3 years ago

    What else to say. Excellent and thank you.

  • I keep getting the error “EROR 1045 (28000): Access denied for user … (using pasword: YES)” I have tried running it as root, and running it with sudo through another user and cannot get around it.

    If I offer no password it gives the same error but instead of using password: YES, it’s NO.

    Please help me fix this issue. Thanks.

    • Xiao Guoan (Admin)
      3 years ago

      If you use MySQL, the command should be

      mysql -u root -p 

      Then enter your MySQL root password (not the Ubuntu root password).

      If you use MariaDB, the command should be

      sudo mysql -u root

      Then enter the Ubuntu root password (not the MariaDB root password).

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here