Postfix is a popular open source mail transfer agent (MTA) that can be used to route and deliver email on Linux systems. It is estimated that approximately 25% of public mail servers on the Internet run Postfix.
This article describes how to install and configure Postfix on an Ubuntu 20.04 server. Then, you will install s-nail (Mail User Agent (MUA), also known as email client) to test whether Postfix can route mail correctly.
Please note that the goal of this article is to use only some basic email functions to help you start Postfix and run it quickly. Finally, although you don't have a fully functional email server, you have some basic components of this type of setup as a basic application and entry.
Although it was experimented under ubuntu 20, other versions and other linux should be applicable.
Prerequisites:
Installed ubuntu20, and have a non-root sudo user. If not, create one under root
adduser sammy
usermod -aG sudo sammy
This creates a user with sudo privileges
The global domain name points to your Ubuntu 20.04 server. You need to make sure that the DNS has an MX record pointing to the mail server.
Install Postfix
Update the local apt package:
sudo apt update
Install Postfix
sudo DEBIAN_PRIORITY=low apt install postfix
This installs DEBIAN_PRIORITY=low, so there are some settings we need.
There are many hints during the installation process, we chose this way:
General type of mail configuration?: Select Internet Site
System mail name : It is assumed here that the domain name is www.example.com and the mail service name is mail.example. Here just fill in example.com. In the future, the mailbox of your user user will be [email protected]
Root and postmaster mail recipient : For example, if your user name is sammy, then fill in sammy, so that the mailboxes of root users and others are received by sammy.
Other destinations to accept mail for : Just choose the default
Force synchronous updates on mail queue?: 选择 No
Local networks : The default is fine.
Mailbox size limit : The number of mailboxes is limited, 0 means no limit.
Local address extension character : + is the plus sign, do not change.
Internet protocols to use: 选All
Write the setting parameter table again:
- General type of mail configuration?: Internet Site
- System mail name:
example.com
(notmail.example.com
) - Root and postmaster mail recipient: The username of your primary Linux account (sammy in our examples)
- Other destinations to accept mail for:
$myhostname, example.com, mail.example.com, localhost.example.com, localhost
- Force synchronous updates on mail queue?: No
- Local networks:
127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
- Mailbox size limit:
0
- Local address extension character:
+
- Internet protocols to use: all
It doesn't matter if you choose the wrong one, you can modify it with the following command:
sudo dpkg-reconfigure postfix
Change Postfix configuration
Set up the main mail directory
sudo postconf -e 'home_mailbox= Maildir/'
Create alias table
sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virtual'
Edit alias file
sudo nano /etc/postfix/virtual
The content is as follows:
[email protected] sammy
[email protected] sammy
What does this mean? The emails of these two users, contact, admin, are forwarded to sammy to receive.
Enable alias mapping table:
sudo postmap /etc/postfix/virtual
Restart Postfix for the changes to take effect:
sudo systemctl restart postfix
Turn on the firewall:
sudo ufw allow Postfix
Install the mail client and initialize the Maildir structure
In order to interact with the mail to be sent, this step will guide and complete the installation of the s-nail software package. This is a versatile variant of the BSD xmail client, which can handle the Maildir format correctly.
However, before installing the client, make sure that the MAIL environment variable is set correctly. s-nail will look for this variable to find out where to find mail for your users.
To ensure that you can set the MAIL variable no matter how you access the account (whether through ssh, su, su- or sudo), you need to set the variable in the /etc/bash.bashrc file and add and save it to /etc/profile. d in the file to ensure that the file is set for all users by default.
echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
The result of this operation is: /etc/bash.bashrc and /etc/profile.d/mail.sh These two files have MAIL=~/Maildir added at the end. You can open these two files and have a look
To set the variables above to take effect in the current session, execute the following command:
source /etc/profile.d/mail.sh
Install now:
sudo apt install s-nail
Before running:
sudo nano /etc/s-nail.rc
The content is to add the following 3 lines to the end. If you copy, remove a dot, which originally meant that other content remains unchanged.
. . .
set emptystart
set folder=Maildir
set record=+sent
The meaning of 3 lines is:
set emptystart: even if the inbox is empty, the client is allowed to open
set folder = Maildir: set the Maildir directory as the internal folder variable
set record = + sent to create a sent mbox file to store the sent mail in In any directory set as a folder variable, in this case Maildir
Send an email by passing a string to the s-nail command. Adjust the command to mark your Linux user as a recipient:
echo 'init' | s-nail -s 'init' -Snorecord sammy
According to the original text, you may get the following prompt:
Can't canonicalize "/home/sammy/Maildir"
This is normal.
I didn't get this prompt.
Take a look at the establishment of ~/Maildir:
ls -R ~ / Maildir
The directory structure should be as follows:
/home/sammy/Maildir/:
cur new tmp
/home/sammy/Maildir/cur:
/home/sammy/Maildir/new:
1463177269.Vfd01I40e4dM691221.mail.example.com
/home/sammy/Maildir/tmp:
It may take some time, or whatever, I always have nothing, not even the Maildir directory.
I don't know why, so I did the previous one again, and suddenly said that I received a new mail, and then I saw this directory. I wonder if I need to wait for some time , and it is the same for the first time. When I received it, there were several emails. If not, just re-execute.
Test client
carried out:
s-nail
has the following output:
[-- Message 1 -- 14 lines, 369 bytes --]:
From [email protected] Tue May 19 15:40:48 2020
Date: Tue, 19 May 2020 15:40:48 +0000
To: [email protected]
Subject: init
Message-Id: <[email protected]>
From: [email protected]
init
You can press Enter to return to the list:
?h
>R 1 [email protected] 2020-05-19 15:40 14/392 init
The information is marked with R and is read.
d Press Enter to delete the current mail
?d
q Enter, exit
?q
The following test sends a file:
Edit first
nano ~/test_message
The content is as follows:
Hello,
This is a test. Please confirm receipt!
Options for sending mail:
-s: This defines the subject line
of the email -r: Optional change to the "From:" field of the email. By default, this field will be populated with the Linux user you used to log in. The -r option allows you to overwrite this address with a valid address (for example, one of the addresses defined in the /etc/postfix/virtual file). To illustrate, the following command uses [email protected]
Also, please make sure to change [email protected] to a valid email address that you have access to :
cat ~/test_message | s-nail -s 'Test email subject line' -r [email protected] [email protected]
Now verify the delivery:
s-nail
Look at the email message sent:
? file +sent
You should see a message similar to the following:
+[/home/sammy/Maildir/]sent: 1 message 1 new
▸N 1 [email protected] 2020-05-19 15:47 12/297 Test email subject line
This article refers to learning How To Install and Configure Postfix on Ubuntu 20.04
The introduction is complete.