Postfix in Ubuntu: Use Postfix, a well-liked open-source Mail Transfer Agent (MTA), to distribute and route email on a Linux system. Around 25% of internet-based public mail servers are thought to run Postfix.
You’ll discover how to set up Postfix on an Ubuntu 20.04 server in this manual. Installing s-nail, a Mail User Agent (MUA) or email client, will then use to verify that Postfix can route mail successfully.
Keep in mind that the objective of this article is to assist you in rapidly setting up Postfix with minimal email capabilities. By the conclusion of this article, you won’t have a fully functional email server, but you will have some of the essential parts.
Postfix in Ubuntu: Prerequisites
Owning a mail server requires a sophisticated setup and ongoing maintenance. It’s more practicable for the majority of customers to rely on a paid mail service. We recommend reading this post on why you might not want to build your own mail server if you’re thinking about doing so.
If you’re certain you want to install and set up Postfix using the instructions in this article, you must first have the following:
Postfix mail server operating on a server running Ubuntu 20.04. This server should have a firewall set up using UFW and a non-root user with sudo rights. To set this up, go to our basic server setup instructions for Ubuntu 20.04.
A Fully Qualified Domain Name pointing at the server running Ubuntu 20.04. By using our Domains and DNS Networking instructions, you may get assistance setting up your domain name with DigitalOcean. Be aware that you must have an MX record pointing to your mail server if you intend to view your mail from a remote place.
It should note that this guide assumes you are setting up a server with the FQDN mail.example.com. Make careful to update example.com or mail.example.com as appropriate to match your unique FQDN.
Postfix in Ubuntu: Installing Postfix in Step 1
Postfix may install via APT because it is part of Ubuntu’s default repository.
sudo apt update
then use the subsequent command to install the postfix package. Keep in mind that in this installation procedure, we are passing the environmental option DEBIAN PRIORITY=low. As a result, the installation procedure will request you to set up a few extra settings:
sudo DEBIAN_PRIORITY=low apt install postfix
A number of interactive questions will display during the installation process.
Use the following details to complete your prompts for the purposes of this tutorial:
- General mail configuration: Since this fits our infrastructure needs, select Internet Site.
- When only the account element of the address provides, the system mail name is the base domain that utilizes to create an email address that is legitimate. For illustration, suppose your server’s hostname is mail.example.com. In order for Postfix to utilize the address [email protected] when given the login user1, you need probably to configure the system mail name to example.com.
- Postmaster and root mail recipient: Mail addressed to root@ and postmaster@ will route to this Linux account. Utilize your main account.
- Other locations for which mail should accept: This specifies the locations for which mail should accept by this Postfix instance. Add more domains here if necessary that this server will charge for receiving. If not, the default will work just fine.
- Make mail queue updates synchronously required? Accept No here since you are probably using a journaled filesystem
- regional networks The networks that your mail server is set up to relay messages for listing below. In most cases, the default will sufficient. But if you do decide to change it, make sure to be extremely picky about the network range.
- Use the mailbox size limit to set a message size restriction. Setting it to 0 disables any size limitation.
- The character that can use to divide the standard part of the address from an extension is the local address extension character (used to create dynamic aliases). For the purposes of this lesson, + will suffice.
- Useful Internet protocols Select whether or not to limit Postfix’s supported IP version. Pick all for the sake of this lesson.
Postfix in Ubuntu: To be clear, the following settings were applied throughout this manual:
- What is the standard mail configuration?: Online resource
- Name of the system mail: example.com (not mail.example.com)
- the receiver of postmaster and root mail is your principal Linux account’s username (Sammy in our examples)
- Additional recipients for mail include $myhostname, example.com, mail.example.com, localhost.example.com, and localhost.
- On the mail queue, force synchronous updates.: No
- Local networks: 104 [::1] 128 127.0.0.0/8 [::ffff:127.0.0.0]
- Limit for mailbox size: 0
- Character for local address extension: +using all internet protocols
Please take note that you may modify these settings at any time by typing:
sudo dpkg-reconfigure postfix
After the installation is complete, you can adjust the Postfix settings a little bit.
Changing the Postfix Configuration in Step 2
You can now change several parameters that were not requested during the package installation procedure. The /etc/postfix/main.cf file contains definitions for several of Postfix’s configuration options. You can query or change configuration settings by using Postfix’s postconf command rather than manually editing this file.
Set the location of the mailbox for your Ubuntu non-root user first. The Maildir format, which divides messages into independent files that subsequently move between directories based on user action, will use in this article. The box format, which saves all messages in a single file, is an alternate choice that isn’t discussed in this article.
Put Maildir/ in the home mailbox variable. Within your user’s home directory, you will later establish a directory structure with that name. Set up home mailbox by keying in:
sudo postconf -e 'home_mailbox= Maildir/'
Set the location of the virtual alias maps table, which associates Linux system accounts with various email addresses, next. Run the following command to associate the table’s location with the /etc/postfix/virtual hash database file:
sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virtual'
You may now build the virtual maps file and start mapping email addresses to user accounts on your Linux system after defining the location of the file in your main. cf file. Using your favorite text editor—we’ll use nano in this example—create the file as follows:
sudo nano /etc/postfix/virtual
List any addresses for which you desire to accept email, followed by a space and the Linux user to whom you wish the mail to be sent.
You might set up your file in the following way, for instance, if you wanted to receive emails from the addresses [email protected] and adm[email protected] and deliver those emails to the Sammy Linux user:
[email protected] sammy [email protected] sammy
Save and close the file once you’ve assigned each address to the proper server account. To accomplish this if you are using nano, hit CTRL + X, Y, then ENTER.
Use the mapping by keying in:
sudo postmap /etc/postfix/virtual
To ensure that all of your modifications have been implemented, restart the Postfix process:
sudo systemctl restart postfix
You should have configured a firewall using UFW if you followed the Initial Server Setup tutorial for the requirement. You must write a firewall rule to allow an exemption for Postfix since this firewall by default will prevent external connections to services on your server unless such connection expressly authorizing.
By typing: you can authorize connections to the service.
sudo ufw allow Postfix
Postfix is now set up and prepared to receive connections from outside sources. You are not yet preparing to try it out with a mail client, though. You’ll need to make a few adjustments to the configuration of your Ubuntu server before you can install a client and use it to interact with the mail being sent to your server.
Step 3: Initializing the Maildir Structure and Installing the Mail Client
This step will guide you through the procedure of installing the s-nail package so that you may interact with the mail that is being sent. This is a robust version of the BSD mail client that can appropriately handle the Maildir format.
However, it needs wise to confirm that your MAIL environment variable is set correctly before installing the client. In order to determine where to locate mail for your user, s-nail will search for this variable.
You must set the variable in the /etc/bash. bashrc file and add it to a file within /etc/profile.d to ensure that it is set by default for all users in order to guarantee that the MAIL variable is set regardless of how you access your account, such as through ssh, su, su -, or sudo.
Type: include the variable in these files.
echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
sudo apt install s-nail
sudo nano /etc/s-nail.rc
Add the following choices to the file’s bottom:
. . . set emptystart set folder=Maildir set record=+sent
So what do these lines do?
Allows the client to open even with an empty inbox when an empty start is set.
Sets the internal folder variable to the Maildir directory using the command set folder=Maildir.
When the folder variable is set to Maildir, set record=+sent generates a sent box file for keeping sent mail in that directory. Save and shut the file when you’re done. You are now prepared to set up the Maildir structure on your machine.
Sending oneself an email with the s-nail command is a simple method to build the Maildir structure in your home directory. You should block writing to the Maildir for this first email as the transmitting file is not accessible until after the Maildir has been built. Pass the -Snorecord option to do this.
By passing a string to the s-nail command, the email is sent. To choose your Linux user as the receiver, change the command:
echo 'init' | s-nail -s 'init' -Snorecord sammy
OutputCan't canonicalize "/home/sammy/Maildir"
By looking in your /Maildir directory, you can verify that the directory was establishing:
ls -R ~/Maildir
Output/home/sammy/Maildir/: cur new tmp /home/sammy/Maildir/cur: /home/sammy/Maildir/new: 1463177269.Vfd01I40e4dM691221.mail.example.com /home/sammy/Maildir/tmp:
Testing the Client: Step 4
Use the s-nail command to open the client:
Outputs-nail version v14.9.15. Type `?' for help "/home/sammy/Maildir": 1 message 1 new >N 1 [email protected] 2020-05-19 15:40 14/392 init
To see the message, press the ENTER key:
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: <201605132207[email protected]> From: [email protected] init
When you type h and press ENTER, the message list will appear again.
Output>R 1 [email protected] 2020-05-19 15:40 14/392 init
Write your message, then save and exit the document.
The message should then pipe to the s-nail process using the cat command. You may achieve this using the example that follows, which makes use of these choices:
-s: This specifies the email message’s subject line.
-r: Changing the email’s “From:” line is optional. This area will automatically fill in with the Linux user that you are currently signed in as. You can override this with a legitimate address, such as the one you set in the /etc/postfix/virtual file, by using the -r option. The command below uses [email protected] as an example.
Additionally, make sure [email protected] change to a working email address that you have access to:
cat ~/test_message | s-nail -s 'Test email subject line' -r [email protected] [email protected]
Then, go to the email address’s inbox where the message was sent. Your message is there nearly instantly, as you will see.
Output+[/home/sammy/Maildir/]sent: 1 message 1 new ▸N 1 [email protected] 2020-05-19 15:47 12/297 Test email subject line
Your Ubuntu 20.04 server is now set to use Postfix. New system administrators may find it challenging to manage email servers, but with this setup, you should have enough MTA email capability to get going.
Do You Need More Help?
Visit our forum, describe your question briefly and get answers
WordPress with Nginx 413 Request Entity too Large Fix
Install PHP 7.3 and the necessary PHP extensions on an Ubuntu Nginx server for WordPress.
Create Your Own Email Server Using Ubuntu: Postfix Installation