Own server part 5 — SMTP mail server

SMTPGET
5 min readJun 24, 2022

In the previous four parts of the cycle, we set up a web server, ftp and encrypted the connection using Let's Encrypt.

In the next, current, fifth part of the cycle, we will install our own SMTP server Provider. Due to the complexity of the configuration, we will install the IMAP server in a separate, sixth part.

What is SMTP?
From Wikipedia:

SMTP (Simple Mail Transfer Protocol) - a communication protocol describing the way of sending e-mail on the Internet ... ... SMTP is a relatively simple, text protocol in which at least one recipient of the message is specified (in most cases its existence is verified), and then transmits the content of the message.

What is an SMTP server?
The Mail Transport Agent (MTA) or SMTP server service serves two purposes:
- Outgoing email: is responsible for forwarding email from an email client / email user agent (MUA) such as Mozilla Thunderbird and Microsoft Outlook to a remote server SMTP.
- Incoming e-mail: also accepts e-mail from other SMTP servers.

The following programs can be used to support the SMTP server: Exim, Sendmail, Postfix, Msmtp, Masqmail, Qmail, etc.
I will use Postfix for our purposes.

What is Postfix?
Postfix by Witese Venemy is a mail server that functions as an alternative to the popular Sendmail. Postfix excels in being fast, easy to administer, and secure, while being Sendmail compatible enough not to upset users. Although it looks like Sendmail from the outside, it is completely different on the inside.

Hostname configuration for the mail server
In the first part of the cycle, we configured the hostname consistent with the name of our domain (here: linuxiara.pl).
In order for your server to be recognized in the network as an e-mail server, the domain name must have, for example, "mail" in its name, so the full domain name of the mail server should be, for example, mail.linuxiar.pl

To do this, you need to reconfigure the hostname, as we did in the first part, pointing to the new (sub) -domain:
hostnamectl set-hostname mail.linuxiarze.pl

Also change the files:
/etc/hostname

changing the hostname to a new one and:
/etc/hosts

changing:
127.0.0.1 linuxiarze.pl

lna:
127.0.0.1 mail.linuxiarze.pl

Changing MX and A records
When registering / parking your domain with your server provider, make sure you have or add, if not, your mail domain's MX record, which should look like this:
Record Type Name Mail Server Priority

MX @ mail.linuxiarze.pl 0

Also add a new A record, which looks like this:
Record Type Name value

A mail

Postfix installation
apt update

apt install postfix

Basic Postfix configuration will be required during installation:
1. General type of mail configuration: Internet Site
2. System mail name (without the "mail" suffix, here): linuxiar.pl
The domain configuration in point 2 will save your email address nick@linuxiar.pl , where
the nickname will be used, which user (except root) you created in the previous part 3 FTP server (here: lolek), so the configured e-mail address is: lolek@linuxiar.pl

The 'ss' (Socket Statistics) utility will tell us if the Postfix master process is listening for TCP on port 25:
sudo ss -lnpt | grep master

Firewall configuration
We installed the UFW packet filter overlay in the first part of the cycle, so now we will add rules for receiving mail from other SMTP servers:
ufw allow 25/tcp

Port 25
test To check if Postfix is ​​sending messages we will use Telnet:
apt install telnet

Perform a test, e.g. to the Gmail mail server:
telnet gmail-smtp-in.l.google.com 25

Feedback message type:
telnet: Unable to connect to remote host: Connection timed out

means port 25 is still closed. In this case, contact the server service provider to open this port.

Otherwise, the port is open and the SMTP server is ready for operation.

Mail operation test
To perform a test e-mail sending, you can use the installed Postfix or another program, eg Mailutils.

a. Postfix includes the Sendmail file, so the shipment will be made from the user account of the system you are currently working on (root).

To send an email from an ordinary user's account, e.g. the one you already have (here: lolek), log into his account:
su - lolek

Now send an email using Postfix:
echo "mail testowy" | sendmail twoje-konto@serwer.com

b. To send an email using Mailutils, install this package:
apt install mailutils

then send the email:
echo "mail testowy" | mail -s "Mój pierwszy mail testowy" twoje-konto@serwer.com

where:
- your-konto@serwer.com is an active email account on another server

To leave the regular user account and return to the root account:
exit

If your email does not reach the external mailbox, check the log:
tail -n 20 /var/log/mail.log

or:
nano /var/log/mail.log

Reading received e-mails
We will use Mailutils for this purpose - if you haven't installed it, do it now.

What is Mailutils?
GNU Mailutils is a rich and powerful protocol-independent mail structure. It includes a number of useful e-mail libraries, clients and servers.

We've just reworked Sending Mail with Mailutils (the mail command).
To read the received mail, use the same command, that is:
mail

Remember that the above command will read the received e-mail messages of the user on whose account you are logged in.

Setting the hostname for smtp relay service
By default, the Postfix SMTP server uses the hostname of the operating system. However, the operating system's hostname can change, so it's good practice to set the hostname directly in your Postfix configuration file.

Open the main Postfix configuration file with a text editor:
nano /etc/postfix/main.cf

and change the line:
myhostname = linuxiarze.pl

on:
myhostname = mail.linuxiarze.pl

After saving the changes, restart Postfix:
systemctl restart postfix

Creating an e-mail alias
There are some required aliases that must be configured when operating the e-mail server. You can add an email alias in the / etc / aliases file, which is a special Postfix lookup table file using a Sendmail compatible format.
nano /etc/aliases

By default, there is a configuration there:
postmaster: root

thanks to which e-mails for postmaster@linuxiar.pl will be delivered to root@linuxiar.pl

We usually don't use the root email address. Instead, the postmaster can use the normal login name to access the emails. So you can add the following line, replacing the username with your real username.
root: lolek

(enter your system user name instead of lolek)

Thanks to this, e-mails for postmaster@linuxiar.pl and root@linuxiar.pl will be delivered to lolek@linuxiar.pl

Activate new aliases:
newaliases

Also, remember to select 'No Configuration' in the future when updating the Postfix package to keep the current SMTP server settings.

In case of questions, please refer to the man pages:
man postfix

man mailutils

man sendmail

man telnet

To sum up today's tutorial, we can now receive mail from other mail servers, read it and send our own using the program in the text console.

In the next part of the Custom Server series, I will develop the topic of e-mail and install an IMAP server , which will allow us to handle incoming and outgoing e-mail using an e-mail client (Thunderbird, Evolution, Claws-Mail, Geary, etc.) on the home computer.

--

--

SMTPGET
0 Followers

SMTPGET provides safe and secure services in the field of SMTP. With SMTPGET you will get the right tools and servers which is required for optimizing inbox.