Postal-smtp-server

Building Postal SMTP server to send bulk emails is a good choice for email marketers. Postal is very powerful and backed by a large community of developers. It is an open-source mail server script written in JavaScript and Ruby. It can be used to build in-house SMTP server just like Mailgun, Sendgrid, Mailchimp, etc.

Postal installation is not very straight forward. The process is fairly complex. It’s because Postal needs several packages to be installed before hand, it can be used. I have tried to make it as simple as possible.

Requirement for Postal SMTP server

  • A domain name from Namecheap or Namesilo
  • A VPS or cloud with Ubuntu 18.04 & 8 GB of RAM

I am using Contabo for the purpose of demonstration.

Initial Setup

In order to start with installation, you will need to connect to the server. If you are on you have to get SSH client for this purpose. One such SSH client is Putty. But if you are on Linux or Mac, just open the terminal and type [email protected] where xx.x.x.xxx is your server IP address.

First of all switch to root user

sudo -i

After that update and upgrade your Ubuntu

apt update -y
apt upgrade -y

Next setup hostname

hostnamectl set-hostname mail.yourdomain.com

Updating DNS records

Login to domain registrar and create A record for mail.yourdomain.com

Now let’s begin with installation

Installation and Configuration of MariaDB

First of all, install MariaDB database on your server.

apt-get install mariadb-server libmysqlclient-dev -y

After installing MariaDB, it’s time to do some configuration to MariaDB

mysql_secure_installation

Answer all the questions as shown below:

    Enter current password for root (enter for none):
    Set root password? [Y/n]: N
    Remove anonymous users? [Y/n]: Y
    Disallow root login remotely? [Y/n]: Y
    Remove test database and access to it? [Y/n]:  Y
    Reload privilege tables now? [Y/n]:  Y

Next login to MariaDB to create mysql database

mysql -u root -p

Enter your root password, then create a database and user for Postal. Here I am creating database name “postal” and database user “postaluser”. Also, replace postaluser password “your_password” with a password of your choice.

CREATE DATABASE postal CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'postaluser'@'localhost' IDENTIFIED BY 'your_password';

Next, grant all the privileges to the postal database:

GRANT ALL ON postal.* TO 'postaluser'@'localhost';
GRANT ALL PRIVILEGES ON `postal-%`.* to `postaluser`@`localhost` IDENTIFIED BY "password";

After that save and exit from the MariaDB shell

FLUSH PRIVILEGES;
QUIT;

Installing Ruby

It’s time to install Ruby on the server. By default, Ruby is not available in the Ubuntu 18.04 default repository. Therefore, we need to add Ruby repository first.

apt-get install software-properties-common -y
apt-add-repository ppa:brightbox/ruby-ng

Next, update the repository and install the Ruby.

apt-get update -y
apt-get install ruby2.3 ruby2.3-dev build-essential -y

Install RabbitMQ

Now we need to install RabbitMQ for message queueing. For that, we need to install few dependencies.

apt-get install curl gnupg debian-keyring debian-archive-keyring apt-transport-https -y

Add signing keys and few repositories

sudo apt-key adv --keyserver "hkps://keys.openpgp.org" --recv-keys "0x0A9AF2115F4687BD29803A206B73A36E6026DFCA"
curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/gpg.E495BB49CC4BBE5B.key | apt-key add -
curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/gpg.9F4587F226208342.key | apt-key add -

Now we need to add apt repositories maintained by Team RabbitMQ

sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF
deb https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main
deb-src https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main
deb https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
deb-src https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
EOF

After that update the repository and install Erlang.

apt-get update -y
apt-get install -y erlang-base \
                        erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                        erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                        erlang-runtime-tools erlang-snmp erlang-ssl \
                        erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl

At last, install RabbitMQ.

apt-get install rabbitmq-server -y --fix-missing

Now we have RabbitMQ installed, let’s check the status of RabbitMQ

systemctl status rabbitmq-server

After that we need to create RabbitMQ vhost and user for postal. Simply run the following command:

rabbitmqctl add_vhost /postal
rabbitmqctl add_user postal password
rabbitmqctl set_permissions -p /postal postal ".*" ".*" ".*"

Installing Nodejs

By default, the latest version of Nodejs is not available in the Ubuntu 18.04 default repository. Therefore, add the repository for that first.

curl -sL https://deb.nodesource.com/setup_12.x | bash

Next install it.

apt-get install nodejs -y

Installing Postal

Before installing Postal, you will need to create a user for postal mail server on Ubuntu.

useradd -r -m -d /opt/postal -s /bin/bash postal

Now, enable ruby to listen on web ports.

setcap 'cap_net_bind_service=+ep' /usr/bin/ruby2.3

After that, install all the required gems with the following command:

gem install bundler

Next, install procodile with gem:

gem install procodile

Finally, install nokogiri with gem:

gem install nokogiri -v '1.7.2'

Install git with the following command

apt install git -y

Now, create a directory structure for Postal. Here all the files will be kept related to Postal

mkdir -p /opt/postal/app

It’s time to download the latest version of Postal.

wget https://postal.atech.media/packages/stable/latest.tgz

Once the download is completed, extract the downloaded file with the following command:

tar zpxvf latest.tgz -C /opt/postal/app

Next, change ownership of postal directory.

chown -R postal:postal /opt/postal

Now, create a symlink for Postal binary

ln -s /opt/postal/app/bin/postal /usr/bin/postal

After that, install all the required dependencies with the following command:

postal bundle /opt/postal/vendor/bundle

If above steps fails and you get the error, then execute the following command. This error is because mimemagic got updated.

cd /opt/postal/app
bundle update mimemagic
bundle install
cd ~

Next, generate Postal Configuration files with the following command:

postal initialize-config

Now, open Postal configuration file and edit it to reflect correct information:

nano /opt/postal/config/postal.yml

Make the following changes as per your environment:

web:
  host: postal.example.com
  # The protocol that requests to the management interface should happen on
  protocol: https

main_db:
  # Specify the connection details for your MySQL database
  host: localhost
  username: postal
  password: password
  database: postal

message_db:
  # Specify the connection details for your MySQL server that will be house the
  # message databases for mail servers.
  host: localhost
  username: postal
  password: password
  prefix: postal

rabbitmq:
  # Specify the connection details for your RabbitMQ server.
  host: 127.0.0.1
  username: postal
  password: password
  vhost: /postal
  
dns:
  # Specifies the DNS record that you have configured. Refer to the documentation at
  # https://github.com/atech/postal/wiki/Domains-&-DNS-Configuration for further
  # information about these.
  mx_records:
    - mx.postal.example.com
  smtp_server_hostname: postal.example.com
  spf_include: spf.postal.example.com
  return_path: rp.postal.example.com
  route_domain: routes.postal.example.com
  track_domain: track.postal.example.com

smtp:
  # Specify an SMTP server that can be used to send messages from the Postal management
  # system to users. You can configure this to use a Postal mail server once the
  # your installation has been set up.
  host: 127.0.0.1
  port: 2525
  username: # Complete when Postal is running and you can
  password: # generate the credentials within the interface.
  from_name: Postal
  from_address: [email protected]

Save and close the file with CTRL + X. Then, initialize database with the following command:

postal initialize

Next, you will need to create a admin user for Postal.

postal make-user

Answer all the questions as shown below:

Postal User Creator
Enter the information required to create a new Postal user.
This tool is usually only used to create your initial admin user.

E-Mail Address      : [email protected]
First Name          : Admin
Last Name           : Postal
Initial Password:   : *********

User has been created with e-mail address [email protected]

Finally, start the Postal application.

postal start

Now, check the status of Postal

postal status

Just in case, if you like to stop postal service, you can do so by

postal stop

Create Systemd Service file for Postal

Next, you will need to create a systemd service file to manage Postal service. This can be done with the following command:

nano /etc/systemd/system/postal.service

Add the following lines:

[Unit]
Description=Postal Mail Platform
After=mysql.service rabbitmq-server.service
Wants=mysql.service rabbitmq-server.service

[Service]
ExecStart=/usr/bin/postal start
ExecStop=/usr/bin/postal stop
ExecReload=/usr/bin/postal restart
User=postal
Restart=on-failure
Type=forking

[Install]
WantedBy=mysql.service rabbitmq-server.service

Save and close the file. Then, reload the systemd with the following command:

systemctl daemon-reload

Next, start Postal service and enable it to start on boot with the following command:

systemctl start postal 
systemctl enable postal 

You can check the status of Postal service with the following command:

systemctl status postal

Install and Configure Nginx

Next, you will need to install Nginx to access Postal mail server. First, install Nginx with the following command:

apt-get install nginx openssl -y

Next, copy Nginx configuration file with the following command:

cp /opt/postal/app/resource/nginx.cfg /etc/nginx/sites-available/default

Next, create a self-signed SSL certificate with the following command:

mkdir /etc/nginx/ssl/
openssl req -x509 -newkey rsa:4096 -keyout /etc/nginx/ssl/postal.key -out /etc/nginx/ssl/postal.cert -days 365 -nodes

Answer all the questions as shown below:

Generating a 4096 bit RSA private key
...............................++
.................++
writing new private key to '/etc/nginx/ssl/postal.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]:Bihar
Locality Name (eg, city) []:Patna
Organization Name (eg, company) [Internet Widgits Pty Ltd]:IT
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:Dhiraj
Email Address []:[email protected]

Next, open the Nginx default virtual host file and define your domain:

nano /etc/nginx/sites-available/default

Here, you need to make the following changes:

server_name postal.example.com;

Save and close the file. After that, restart Nginx service with the following command:

systemctl restart nginx

Access Postal Web Interface

Now, open your web browser and type the URL https://mail.yourdomain.com. You will be redirected to the admin page. Login with email id and password.

Postal-login-screen

After that, you will postal dashboard.

postal-dashboard

In, next article we will learn to create organization, domain management and SMTP creation, etc.

Video Lesson

Conclusion

Installing Postal is somewhat complex procedure. Nonetheless, it’s worth to give it a try considering the Postal features.

11 replies
      • Clement Godswill
        Clement Godswill says:

        Thanks for your reply. I bought several ips from contabo. I have followed your video and install postal successfuly. I have tried to Build a new mail server in postal, after adding a server name, I click on the build server button, it was just loading and loading without building any server. I can give you my postal login details, so you can see that my postal cannot build mail server

        Reply
  1. Jozef
    Jozef says:

    Why do you think i got this error?sudo apt-get install ruby2.3 ruby2.3-dev build-essential -y
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package ruby2.3 is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    Package ruby2.3-dev is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘ruby2.3’ has no installation candidate
    E: Package ‘ruby2.3-dev’ has no installation candidate

    Reply
  2. Jozef
    Jozef says:

    I got this error again when using this commend:
    sudo gem install nokogiri -v ‘1.7.2’
    Building native extensions. This could take a while…
    ERROR: Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.7.0/gems/nokogiri-1.7.2/ext/nokogiri
    /usr/bin/ruby2.7 -I /usr/lib/ruby/2.7.0 -r ./siteconf20210424-238639-mr7f7o.rb extconf.rb
    checking if the C compiler accepts … yes
    Building nokogiri using packaged libraries.
    Using mini_portile version 2.1.0

    Reply

Trackbacks & Pingbacks

  1. […] the article Install Postal SMTP server on Ubuntu 18.04, I already have discussed steps to install Postal Successfully. Now in this article, I will tell […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *