How to Install Mailtrain v2 on Ubuntu 20.04 Server

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

Mailtrain is released under the terms of GPL v3.0 license, built on Node.js and MySQL/MariaDB. The latest version is v2, released on June 2021. 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 checkboxes), 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.
  • It allows you to check the open rate, click rate, who opened your email, and individual click statistics for every link. You can also check these statistics by country.
  • Advanced email template editors and HTML code editor.
  • Automation: send specific emails when user activates your predefined trigger.
  • You can create an open email list (allow public subscription) and closed email list (subscribers are added to the list by admin).
  • Create multiple users with granular user permissions and flexible sharing.
  • Hierarchical namespaces for enterprise-level situations
  • Localize the user interface per user
  • List export
  • Ability to send to multiple lists with prevention of double delivery.
  • Multiple send configurations
  • SMTP Throttling to prevent your server from sending a large volume of emails in a short amount of time, which will improve email deliverability.
  • If you need to send emails as fast as possible, simply configure Mailtrain to use as many sender processers as you want, as long as the server has enough CPU and RAM resources.

How to Install Mailtrain v2 on Ubuntu 20.04 Server

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 about $20/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 $20/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.

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

  • They don’t block port 25, so you can send unlimited emails (transactional email and newsletters) without spending money on SMTP relay service. Kamatera doesn’t have any SMTP limits. You can send a million emails per day.
  • 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 blacklist.
  • 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 limits, whatsoever.
  • You can order multiple IP addresses for a single server. This is very useful for folks who need to send a large volume of emails. You can spread email traffic on multiple IP addresses to achieve better email deliverability.

Follow the tutorial linked below to create your Linux VPS server at Kamatera.

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: Add Sudo User

I installed Mailtrain with a sudo user on Ubuntu 20.04. For best results, you should also follow this tutorial with a sudo user, not root. To add a sudo user, simply run the following command on the server

sudo adduser username
sudo adduser username sudo

Then switch to the new user.

su - username

Step 3: Install MariaDB Database Server

Note: If you followed my iRedMail tutorial to set up your email server, then you should already have MariaDB up and running, so you can skip this step.

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 MariaDB on Ubuntu 20.04.

sudo apt install mariadb-server mariadb-client

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

systemctl status mariadb

Output:

 mariadb.service - MariaDB 10.3.22 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-04-10 14:19:16 UTC; 18s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 9161 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 9451)
     Memory: 64.7M
     CGroup: /system.slice/mariadb.service
             └─9161 /usr/sbin/mysqld

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

sudo systemctl start mariadb

To enable MariaDB to automatically start at 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. (Notice that Y is capitalized, which means it is the default answer. )

Install-LAMP-stack-on-Ubuntu-20.04-MariaDB-Database-server

Step 4: Create a Database and User for Mailtrain

Run the following command to log into MariaDB console.

sudo mysql -u root

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

create database mailtrainv2;

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

grant all privileges on mailtrainv2.* to mailtrain@localhost identified by 'mailtrain_password';

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

flush privileges;

exit;

Step 5: 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 in order to run Mailtrain. Mailtrain v2 requires Node.js 14+. This tutorial will install Node.js and the package manager (npm) from the upstream repository.

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install nodejs -y

To check your Node.js and npm version, run

node -v

npm -v

Output:

mailtrainv2 nodejs 14

Note that I don’t recommend installing the latest LTS version of Node.js (v16) from the nodesource.com repository, because it’s not very compatible with Mailtrain v2.  To compile and install native addons from npm you also need to install build tools:

sudo apt install -y build-essential

To edit images in the email template editor and HTML editors, you need to install ImageMagick.

sudo apt install imagemagick

Step 6: Install 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 -y

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 Wed 2021-10-27 20:01:10 HKT; 4 days ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 1359 (redis-server)
      Tasks: 4 (limit: 15514)
     Memory: 10.8M
     CGroup: /system.slice/redis-server.service
             └─1359 /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

Step 7: Run Mailtrain

You should be working in your home directory for this step.

cd ~

Then fetch Mailtrain files from Github. (Please do not use sudo when running the git commands.)

sudo apt install git

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

cd mailtrain/

git checkout v2

Import the initial SQL data to the mailtrain database by using the following command. You need to enter the password of mailtrain user created in step 4.

mysql -u mailtrain -p mailtrainv2 < ~/mailtrain/server/setup/sql/mailtrain.sql

Create the production.yaml configuration file with a command-line text editor like Nano.

nano ~/mailtrain/server/config/production.yaml

In this file, you can add configurations that override the default configurations in ~/mailtrain/server/config/default.yaml file.  We only need to add the following configurations. Note that there should be a space character after each colon.

user: mailtrain
group: mailtrain
roUser: nobody
roGroup: nobody

www:
  host: 127.0.0.1
  proxy: true
  secret: "Replace this with some random characters"
  trustedUrlBase: http://mailtrain.example.com
  sandboxUrlBase: http://sbox-mailtrain.example.com
  publicUrlBase: http://newsletter.example.com

mysql:
  user: mailtrain
  password: mailtrain_password
  database: mailtrainv2
redis:
  enabled: true

log:
  level: info

builtinZoneMTA:
  enabled: false

queue:
  processes: 5

Mailtrain requires you to use 3 URL endpoints.

  • mailtrain.example.com: The trusted URL used by logged-in users.
  • sbox-mailtrain.example.com: This is used to host template editors, not shown to any user.
  • newsletter.example.com: The public URL as seen by visitors.

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 --no-create-home mailtrain

Create a configuration file for the report worker.

nano ~/mailtrain/server/services/workers/reports/config/production.yaml

Add the following configurations.

log:
  level: warn
mysql:
  user: mailtrain
  password: mailtrain_password
  database: mailtrainv2

Save and close the file.

Now make sure you are in the ~/mailtrain/ directory.

cd ~/mailtrain

install the required node packages. The following is a single command. Please do not use sudo.

for idx in client shared server mvis/client mvis/server mvis/test-embed mvis/ivis-core/client mvis/ivis-core/server mvis/ivis-core/shared mvis/ivis-core/embedding; do
(cd $idx && npm install && npm audit fix)
done

If you encounter the following error message, you can ignore them.

-bash: cd: mvis/ivis-core/client: No such file or directory
-bash: cd: mvis/ivis-core/server: No such file or directory
-bash: cd: mvis/ivis-core/shared: No such file or directory
-bash: cd: mvis/ivis-core/embedding: No such file or directory

Change to the ~/mailtrain/client/ directory.

cd ~/mailtrain/client

Build the node packages.

npm run build

Copy the mailtrain folder to /var/www/.

sudo mkdir -p /var/www/

cd ~

sudo cp ~/mailtrain /var/www/mailtrainv2 -r

Then change the permission of /var/www/mailtrainv2/.

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

sudo chmod o-rwx /var/www/mailtrainv2/server/config/

Create a systemd service unit file for mailtrain.

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

Add the following lines to this file.

[Unit]
Description=Mailtrain server
After=syslog.target network.target mariadb.service redis-server.service

[Service]
Environment="NODE_ENV=production"
WorkingDirectory=/var/www/mailtrainv2/server
ExecStart=/usr/bin/node index.js
Type=simple
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

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

sudo systemctl enable --now mailtrainv2.service

Check its status. Make sure it is running.

sudo systemctl status mailtrainv2.service

Sample output:

 mailtrainv2.service - Mailtrain server
     Loaded: loaded (/etc/systemd/system/mailtrainv2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-11-01 16:42:10 HKT; 8s ago
   Main PID: 2794663 (mailtrain)
      Tasks: 25 (limit: 15514)
     Memory: 258.0M
     CGroup: /system.slice/mailtrainv2.service
             ├─2794663 mailtrain
             ├─2794693 mailtrain: worker executor
             └─2794701 /usr/bin/node /var/www/mailtrainv2/server/services/importer.js

If it’s not running, you can check the log.

sudo journalctl -eu mailtrainv2

If mailtrainv2.service is running, you can check which ports it’s using with the following command.

sudo netstat -lnpt | grep mailtrain

Step 8: Setting up Reverse Proxy

We can use Nginx or Apache web server to set up the reverse proxy.

Nginx

If you use iRedMail, then you should choose Nginx.

Install Nginx web server.

sudo apt install nginx

Create 3 Nginx virtual hosts for the 3 URL endpoints.

sudo nano /etc/nginx/sites-enabled/mailtrain.example.com.conf
sudo nano /etc/nginx/sites-enabled/sbox-mailtrain.example.com.conf
sudo nano /etc/nginx/sites-enabled/newsletter.example.com.conf

The content of the 3 files are as follows:

mailtrain.example.com.conf

server {
        listen 80;
        server_name mailtrain.example.com;

        access_log /var/log/nginx/mailtrain.access;
        error_log /var/log/nginx/mailtrain.error;

       client_max_body_size 200M;

       location / {
          proxy_pass http://127.0.0.1:3000;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

sbox-mailtrain.example.com

server {
        listen 80;
        server_name sbox-mailtrain.example.com;

        access_log /var/log/nginx/sbox-mailtrain.access;
        error_log /var/log/nginx/sbox-mailtrain.error;

       location / {
          proxy_pass http://127.0.0.1:3003;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

newsletter.example.com.conf

server {
        listen 80;
        server_name newsletter.example.com;

        access_log /var/log/nginx/newsletter.access;
        error_log /var/log/nginx/newsletter.error;

       location / {
          proxy_pass http://127.0.0.1:3004;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

Save and close the files. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

Apache

Install Apache web server.

sudo apt install apache2

Create 3 Apache virtual hosts for the 3 URL endpoints.

sudo nano /etc/apache/sites-enabled/mailtrain.example.com.conf
sudo nano /etc/apache/sites-enabled/sbox-mailtrain.example.com.conf
sudo nano /etc/apache/sites-enabled/newsletter.example.com.conf

The content of the 3 files are as follows:

mailtrain.example.com

<VirtualHost *:80>
    ServerName mailtrain.example.com
    ServerSignature Off

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/

    ErrorLog /var/log/apache2/mailtrain.linuxbabe.org.error
</VirtualHost>

sbox-mailtrain.example.com

<VirtualHost *:80>
    ServerName sbox-mailtrain.example.com
    ServerSignature Off

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3003/
    ProxyPassReverse / http://127.0.0.1:3003/

    ErrorLog /var/log/apache2/sbox.mailtrain.linuxbabe.org.error
</VirtualHost>

newsletter.example.com.conf

<VirtualHost *:80>
    ServerName newsletter.example.com
    ServerSignature Off

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3004/
    ProxyPassReverse / http://127.0.0.1:3004/

    ErrorLog /var/log/apache2/newsletter.linuxbabe.org.error
</VirtualHost>

Save and close the files. You need to enable 3 modules in Apache.

sudo a2enmod proxy proxy_http headers

Then enable the 3 virtual hosts.

sudo a2ensite mailtrain.example.com.conf newsletter.example.com.conf sbox-mailtrain.example.com.conf

Restart Apache web server.

sudo systemctl restart apache2

Step 9: Enable HTTPS

To encrypt the HTTP traffic when you visit Mailtrain server from outside, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu.

sudo apt install certbot

If you use Nginx web server, you also need to install the Certbot Nginx plugin.

sudo apt install python3-certbot-nginx

Then run the following 3 command to obtain and install TLS certificate.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d mailtrain.example.com

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d sbox-mailtrain.example.com

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

If you use Apache web server, you also need to install the Certbot Apache plugin.

sudo apt install python3-certbot-apache

Then run the following command to obtain and install TLS certificate.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d mailtrain.example.com
sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d sbox-mailtrain.example.com
sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d newsletter.example.com

Where:

  • --nginx: Use the nginx plugin.
  • --apache: Use the Apache plugin.
  • --agree-tos: Agree to terms of service.
  • --redirect: Force HTTPS by 301 redirect.
  • --hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.
  • --staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.
  • --email: Your email address, which is used for account recovery and other important notification messages.

The certificate should now be obtained and automatically installed.

mailtrain v2 https

Now you should edit the /var/www/mailtrainv2/server/config/production.yaml file and change http to https. Then restart mailtrain service.

sudo systemctl restart mailtrainv2

Step 10: Mailtrain Configuratin

Go to https://mailtrain.example.com. Login with username admin and password test. (Note: the newsletter.example.com sub-domain is used by visitors. The admin URL is https://mailtrain.example.com).

mailtrain v2 admin login

Then go to Administration -> Users page to change your account email address and password.

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.

Go to Administration -> Global Settings page to change default configurations. You should change the service address from http://localhost:3000/ to your sub-domain (https://newsletter.example.com).

Next, go to Administration -> send configurations to configure SMTP settings, so your Mailtrain can send emails. By default, Mailtrain is configured to use ZoneMTA. Click the Edit button to change the default setting.

mailtrain v2 SMTP settings

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 v2 generic SMTP

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 v2 smtp settings no encryption and authentication

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

Managing Users and Roles

Mailtrain v2 supports multiple users with granular user permissions and flexible sharing. You can create additional users in the Mailtrain web interface. By default, there are 3 roles a user can take.

  • Global master: This is the super admin user.
  • Campaign admin
  • None

Permissions associated with each role is defined in the /var/www/mailtrainv2/server/config/default.yaml file.

Fix “Greeting Never Received” Error

If you send an email campaign, and there are bounces with “Greeting never received” error, it might be that Mailtrain is sending too fast.

Greeting never received

As I explained earlier, if you want to send emails as fast as possible, you can configure Mailtrain to use more sender processes by editing the /var/www/mailtrain/server/config/production.yaml file. However, you should make sure the SMTP server can cope with the number of sender processes.

If you don’t want to reduce the number of sender processes in Mailtrain, then you need to do some performance tuning on Postfix SMTP server, so it can process emails faster. For example, you can increase the number of SMTP server processes from 100 to 150 by adding the following line in the /etc/postfix/main.cf file.

default_process_limit = 150

Then restart Postfix. It’s recommended that you don’t use more than 150 processes, or your Postfix SMTP server can be rate limited by mailbox providers and your email can be placed in the spam folder.

gmail Rate limit exceeded

Fix File Upload Error

If you encounter the “permission denied” error when uploading files in Mailtrain, then you need to run the following command.

sudo setfacl -R -m u:mailtrain:rwx /var/www/mailtrain/server/files/

You should create a Cron job.

sudo crontab -e

Add the following line at the end of this file.

* * * * * /usr/bin/setfacl -R -m u:mailtrain:rwx /var/www/mailtrainv2/server/files/

The request failed with status code 413

The HTTP status code 413 means that your POST request is too large. If you encounter this error when importing email list via CSV file, then you need to configure Nginx to allow large file upload.

sudo nano /etc/nginx/sites-enabled/mailtrain.example.com.conf

In this file, we already define the maximum file size: 200M.

client_max_body_size 200M;

If your CSV file is larger than 200M, simply increase it.

client_max_body_size 512M;

Save and close the file. Then reload Nginx.

sudo systemctl reload nginx

How to Handle Bounce Messages in Mailtrain

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

If you use an SMTP relay service to send emails, they will handle bounce messages for you. If you use your own email server to send emails, 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/server/config/production.yaml

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 an 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 Administration -> Send Configuration -> VERP Bounce Handlding. Enable VERP and enter a hostname like bounces.example.com. Save your settings.

mailtrain verp bounce handling

Next, you need to create the following records for the server hostname (bounces.your-domain.com):

  • MX record: bounces.your-domain.com
  • A record: server IP address
  • SPF record: It should be the same as the SPF record of the main domain.

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 addresses 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

Wrapping Up

I hope this tutorial helped you install Mailtrain v2 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: 4 Average: 5]

17 Responses to “How to Install Mailtrain v2 on Ubuntu 20.04 Server

  • thanks buddy, great job, i test it today by installing on a proxmox LXC.

    If I see something I will write to you.

    Thank you so much

  • Hi,

    thanks a lot for this tutorial, very helpful!.
    I’m facing two issues when trying to install node packages, here are the errors. Any idea on how to solve this ?

    
    npm ERR! prepareGitDep 1> 
    npm ERR! prepareGitDep > [email protected] prepare /home/localadmin/.npm/_cacache/tmp/git-clone-bc4c7630
    npm ERR! prepareGitDep > node -r esm build.js
    npm ERR! prepareGitDep 
    npm ERR! prepareGitDep 
    npm ERR! prepareGitDep 2> npm WARN install Usage of the `--dev` option is deprecated. Use `--also=dev` instead.
    npm ERR! prepareGitDep file:///home/localadmin/.npm/_cacache/tmp/git-clone-bc4c7630/build.js:1
    npm ERR! prepareGitDep SyntaxError: Error parsing /home/localadmin/.npm/_cacache/tmp/git-clone-bc4c7630/node_modules/fs-extra/package.json: Unexpected token , in JSON at position 2565
    npm ERR! prepareGitDep     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) {
    npm ERR! prepareGitDep   path: '/home/localadmin/.npm/_cacache/tmp/git-clone-bc4c7630/node_modules/fs-extra/package.json'
    npm ERR! prepareGitDep }
    npm ERR! prepareGitDep npm ERR! code ELIFECYCLE
    npm ERR! prepareGitDep npm ERR! errno 1
    npm ERR! prepareGitDep npm ERR! [email protected] prepare: `node -r esm build.js`
    npm ERR! prepareGitDep npm ERR! Exit status 1
    npm ERR! prepareGitDep npm ERR! 
    npm ERR! prepareGitDep npm ERR! Failed at the [email protected] prepare script.
    npm ERR! prepareGitDep npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    npm ERR! prepareGitDep 
    npm ERR! prepareGitDep npm ERR! A complete log of this run can be found in:
    npm ERR! prepareGitDep npm ERR!     /home/localadmin/.npm/_logs/2021-11-05T07_25_20_811Z-debug.log
    npm ERR! prepareGitDep 
    
    
    ---
    
    make: *** [posix.target.mk:111: Release/obj.target/posix/src/posix.o] Error 1
    make: Leaving directory '/home/localadmin/mailtrain/server/node_modules/posix/build'
    gyp ERR! build error 
    gyp ERR! stack Error: `make` failed with exit code: 2
    gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
    gyp ERR! stack     at ChildProcess.emit (events.js:400:28)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:282:12)
    gyp ERR! System Linux 5.4.0-89-generic
    gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
    gyp ERR! cwd /home/localadmin/mailtrain/server/node_modules/posix
    gyp ERR! node -v v14.18.1
    gyp ERR! node-gyp -v v5.1.0
    gyp ERR! not ok
    
    • Xiao Guoan (Admin)
      3 years ago

      Use a clean fresh server.

      • Caramboleyo
        2 years ago

        Having the same issue on multiple servers – old or new. Had to install build-essentials (?!) because make was not found.

    • Xiao Guoan (Admin)
      2 years ago

      I installed a new instance of Mailtrain V2 today following the same procedure as described in this tutorial. I also encounter some errors when using npm to build the node package.

      However, I found these errors can be ignored. Mailtrain works as usual.

      The POSIX module is used by Mailtrain to resolve user ID and group ID of the files under /var/www/mailtrianv2/ directory. If you follow my instruction, and set the mailtrain.service to run as the mailtrain user, then the POSIX module is not needed.

      sudo journalctl -eu mailtrainv2 shows:

      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info Executor Executor process started.
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info Express WWW server [trusted] listening on port 3000
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info Express WWW server [sandbox] listening on port 3003
      ubuntu node[501147]: info Express WWW server [public] listening on port 3004
      ubuntu node[501147]: info PrivilegeHelpers Changed group to "mailtrain" (0)
      ubuntu node[501147]: info PrivilegeHelpers Changed user to "mailtrain" (138)
      

      As you can see, although Mailtrain can’t resolve uid/gid because of missing POSIX module, it finally change to user mailtrain and group mailtrain.

  • Hi,
    When trying to install apache2 (with your step) it is failing to start. What should I do now?

    • Xiao Guoan (Admin)
      2 years ago

      If you installed Nginx, then you should not install Apache. Remove Apache with:

      sudo apt remove apache2
      • Lutfar Rahman
        2 years ago

        Thanks man. I have done it. Can you please tell me how to update mailtrainv2 to latest build? Also is therey any userguide to use mailtrain? Their wiki is not yet in v2. Thanks in advance.

  • Cheerz,

    great tutorial, as always! Thank you!

    one small improvement maybe…

    Permissions are wrong for:
    /var/www/mailtrainv2/server/files/

    fix with:
    sudo chown -R mailtrain:mailtrain mailtrainv2/server/files/

  • i have problem
    to run this command
    npm run build

  • Xiao Guoan (Admin)
    2 years ago

    If you encounter the following error when building node packages, you can ignore it. Mailtrain will work as usual.

    ../src/posix.cc:709:20: error: init’ was not declared in this scope; did you mean ‘int’?
      709 | NODE_MODULE(posix, init);
          |                    ^~~~
    /home/linuxbabe/.cache/node-gyp/14.20.0/include/node/node.h:793:36: note: in definition of macro ‘NODE_MODULE_X’
      793 |       (node::addon_register_func) (regfunc),                          \
          |                                    ^~~~~~~
    ../src/posix.cc:709:1: note: in expansion of macro ‘NODE_MODULE’
      709 | NODE_MODULE(posix, init);
          | ^~~~~~~~~~~
    make: *** [posix.target.mk:111: Release/obj.target/posix/src/posix.o] Error 1
    make: Leaving directory '/home/linuxbabe/mailtrain/server/node_modules/posix/build'
    gyp ERR! build error 
    gyp ERR! stack Error: `make` failed with exit code: 2
    gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
    gyp ERR! stack     at ChildProcess.emit (events.js:400:28)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:285:12)
    gyp ERR! System Linux 5.15.0-40-generic
    gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
    gyp ERR! cwd /home/linuxbabe/mailtrain/server/node_modules/posix
    gyp ERR! node -v v14.20.0
    gyp ERR! node-gyp -v v5.1.0
    
    • Xiao Guoan (Admin)
      2 years ago

      The POSIX module is used by Mailtrain to resolve user ID and group ID of the files under /var/www/mailtrian/ directory. If you follow my instruction, and set the mailtrain.service to run as the mailtrain user, then the POSIX module is not needed.

      sudo journalctl -eu mailtrain shows:

      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info Executor Executor process started.
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info Express WWW server [trusted] listening on port 3000
      ubuntu node[501147]: info PrivilegeHelpers Posix module not installed. Cannot resolve uid/gid
      ubuntu node[501147]: info Express WWW server [sandbox] listening on port 3003
      ubuntu node[501147]: info Express WWW server [public] listening on port 3004
      ubuntu node[501147]: info PrivilegeHelpers Changed group to "mailtrain" (0)
      ubuntu node[501147]: info PrivilegeHelpers Changed user to "mailtrain" (138)
      

      As you can see, although Mailtrain can’t resolve uid/gid because of missing POSIX module, it finally change to user mailtrain and group mailtrain.

  • Hello and thanks for the guide
    I ran Mailtrain setup in Ubuntu 20.04, updated.
    I access mailtrain.example.com successfully. But accessing newsletter.example.com and sbox-mailtrain.example.com only shows “Cannot get /”.

    I’ve tried both Apache and Nginx

    What can I do?

    • Xiao Guoan (Admin)
      2 years ago

      It’s an intented error. Ignore it.

      newsletter.example.com and sbox-mailtrain.example.com are not designed to show anything. You are not supposed to use them directly.

      • oleksandr
        1 year ago

        then is it possible for users to subscribe to newsletters without logging in to the site?

  • This guide is not working any more.
    Starting with

    for idx in client shared server mvis/client mvis/server mvis/test-embed mvis/ivis-core/client mvis/ivis-core/server mvis/ivis-core/shared mvis/ivis-core/embedding; do
    (cd $idx && npm install && npm audit fix)
    done

    All errors further.

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