In this tutorial, you will learn how to install and use Postfix, an open-source mail transfer agent. With Postfix, you can send and receive emails and send them through third-party SMTP servers, such as gmail.com.
What we will treat
- Create a user who sends and receives emails
- Interactive installation of Postfix
- Configure Postfix via the main.cf file
- Set up mailbox for Postfix
- Set up the virtual map file
- Add a firewall rule to enable Postfix
- Mail location (in which folder the mail files are located)
- Check the postfix status
- Using Postfix over TELNET
- Install mail client
- Working with the mail client
We will install and configure Postfix on Ubuntu 16.04:
- Starting with a clean VPS with
- At least 512 MB of RAM and
- 15 GB of free space.
- You need root user access and
- DNS records for your domain must already exist, especially for PTR and MX.
Step 1: Create a non-root user
Once you are logged in as rootYou can create a new user account that receives and sends emails to and from Postfix. To create a user named postfixuserRun the following command:
Then add it to the sudo group so you can execute commands as sudo:
usermod -aG sudo postfixuser
Step 2: Interactive installation of Postfix
Postfix is very easy to install. First, update the system cache and immediately immerse yourself in the installation.
sudo apt update
sudo apt install postfix
You will be guided through a series of three screens to define basic parameters for Postfix. Press the Tab key on the keyboard to move the cursor to the OK button, then press Enter.
The first screen explains the choices you have, but you can actually select them on the next screen.
Choose an option Website,
For the above screen (email name), enter the correct name of your domain, which you have previously set up with your DNS registrar.
Step 2A (optional): Full interactive installation of Postfix
There is another command to install Postfix:
sudo dpkg-reconfigure postfix
The first three screens are also displayed. Then the following screens are displayed for more detailed control:
Enter the name of the user who should receive e-mails for the system administrator.
On this screen, you define the final destination of the e-mail. Select Ok, as the options offered are good enough.
On this screen, select No because you are using a journaling system for data.
Select OK to accept the defaults in the screen above.
Select Ok again to select 0 for the postfix file limit.
You can select the character that defines the local address extensions – just click OK.
Use the vertical cursor keys to select one of the three values. Choose Everything,
This was the last screen in the postfix interactive installation.
Step 3: Configure Postfix via the main.cf configuration file
The main configuration file for Postfix is located at / etc / postfix / main / cf, Open it for editing with this command:
sudo nano /etc/postfix/main.cf
and you will see the following:
You recognize values that we entered during the manual installation.
If you change something in this file, run the following command:
For the new values to take effect.
Load new aliases and enable and restart the Postfix service so that it is always available:
Enable sudo systemctl postfix
sudo systemctl restart postfix
Step 4: Set up Mailbox for Postfix
Once the e-mail messages arrive, they can be saved as separate files or in a large file. The former option is known as Maildir Format while the latter is known as mbox Format. To indicate that we are using the Maildir format, we need to change the value of the parameter At homemailbox_ in the file main.cf.
One way to do this is the Nano Editor. The other is to use the following command:
sudo postconf -e & # 39; home_mailbox = Maildir / & # 39;
postconf is a program that changes main.cf "from the outside", parameter by parameter.
Step 5: Set up the virtual map file
We will now connect email addresses with users on your VPS. We have already created a user named postfixuserConnect them now with two e-mail addresses: email@example.com and firstname.lastname@example.org, Change this generic name example.com on your actual domain name.)
Postfix keeps connections in the file / etc / postfix / virtual and there is a parameter virtual_alias_maps in the config file change. Taken together:
sudo postconf -e & # 39; virtual_alias_maps = hash: / etc / postfix / virtual & # 39;
Then open this file in nano:
sudo nano / etc / postfix / virtual
Add the following lines to configure the addresses:
To notify Postfix of these changes, restart the process:
sudo postmap / etc / postfix / virtual
sudo systemctl restart postfix
Step 6: Modify the firewall rules to enable Postfix
If you are using a firewall, you must configure it to allow postfix and e-mail. Firewall called & c (uncomplicated firewall) should already be installed on Ubuntu, so you can add Postfix with just one line:
sudo ufw allow Postfix
If ufw does not exist, first install it with these two commands:
sudo apt install ufw
sudo ufw activate
After installation, you would need to specify ALL ports to be operational:
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw allow 20 / tcp
sudo ufw allow 21 / tcp
sudo ufw allow Postfix
sudo ufw activate
WARNING: If you omit rows with ssh. http. https etc. You can NOT log in to your VPS server! you are highly recommended To investigate the ports your hosting provider can rely on and include them in the list above.
Step 6: E-mail Location
The mail location should be updated everything Users know about it. There are two places where we need to add the new location: /etc/bash.bashrc and /etc/profile.d/mail.sh,
You can change both files with the following command:
echo & # 39; export MAIL = ~ / Maildir & # 39; | sudo tea -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
It is added export MAIL = ~ / Maildir to these files at the end of each.
To see the result, execute
sudo cat /etc/bash.bashrc
and see export MAIL = ~ / Maildir as the last statement in the file:
The same applies /etc/profile.d/mail.sh:
sudo cat /etc/profile.d/mail.sh
At startup, Ubuntu reads and executes any files ending with .sh in the /etc/profile.d/ folder. Instead of rebooting your VPS, run
reload mail.sh in the current session.
Step 7: Switch to a specific user
This is a simple command, but it will save you many hours debugging. We switch to a specific user and send and receive emails. If you try to do this root You will most likely get this error:
Can not canonize "/ root / Maildir"
The way out is to switch to a non-root user, as follows:
Step 8: Check Postfix Status
sudo postfix status
will tell us if Postfix is running or not. A typical output would be:
If it is not running, restart it:
sudo postfix start
Step 9: Use Postfix over TELNET
We have Postfix installed and configured, but is it working right?
Assuming port 25 of your VPS is open, we can test Postfix with a service called TELNET.
telnet localhost 25
Here is a typical edition that you will see:
The line beginning with 500 is an error, because I purposely hit the Enter key. Telnet expects you to enter an e-mail line by line:
Email from: root @ localhost
rcpt to: fmaster @ localhost
Subject: My first mail on Postfix
How is your day?
I hope it's all right!
A dot at the end signals the end of the input and leaving In the last line we return to the terminal command line.
With everybody leavingYour message will be queued, ie sent as an e-mail.
Step 10: Install a Mail Client
To see e-mails and do something with them, we need to install an e-mail client. There are several applications to consider that are most popular slug and mailutils, They all lead to the same virtual package mailx, and all are activated by command mail in the command line.
sudo apt install mailutils
will give us a command mail utility.
Another competitor, s-nail, can be installed with the following command:
sudo apt install s-nail
Open the configuration file s-nail.rc:
sudo nano /etc/s-nail.rc
Then add the following lines at the end of the file:
set folder = ~ / Maildir
set record = + sent
Step 11: Working with the Mail Client
Let's send our first email about that mail Command:
echo & # 39; init & # 39; | mail -s & # 39; init & # 39; -Snorecord postfixuser
From this picture
we see that we first had four messages (sent over Telnet in my case) and that we have five messages in the folder after executing the above command. This means that Postfix and the mail program are working properly.
The question mark at the beginning of the line means that we can enter commands to the mail client, for example:
- enter – Show the message,
- H – go back a message,
- d – delete current message,
- File + sent – message that was sent
- q – leave the terminal.
It is also possible to send messages mail, like this:
Echo "Beginning" | mails & # 39; Great News Ahead & # 39; email@example.com
The message will be sent, but you will most likely notice first that you have received new messages:
When you open the message, it explains what went wrong. Number 550 for mistakes can mean something like that
- Verification failed
- Unrouteable address
- Verification of the sender failed
The incoming server has tried to see who is sending emails and has refused to accept it without any additional information. This is to be expected in times of spam worldwide.
What do you do next?
You have now run Postfix and one or two e-mail clients. They are all command-line based and are only for administrators. Your users need a graphical interface to send emails. For this we need not only Postfix, but also some additional programs. One such program for SMTP transmission is Dovecot, or you can connect Postfix to a service such as gmail.com to send an e-mail from a proxy address.
Dusko Savic is a technical writer and flutter programmer.