How to Install Postfix on Ubuntu

Installation and configuration of postfix on the ubuntu server.

After Installing of LEMP.

  • During the installation process, you will be prompted to select the type of mail server you want to configure. Select “Internet Site” and press Enter. You will be prompted to enter the fully qualified domain name (FQDN) of your mail server. Enter the FQDN and press Enter.
  • To see the FQDN form of your hostname  – hostname -f
  • If your ubuntu server doesnot have a FQDN then use the following command to ser one – sudo hostnamectl set-hostname <your-fqdn>      eg. mail.yourdomain.com   – You might need to logout and login again to see the changes.
  • Make sure you have MX record setup at your DNS server – MX record       @           mail.yourdomain.com

A record
An A record maps an FQDN to an IP address.
mail.yourdomain.com <IP-address>

AAAA record
If your server uses IPv6 address, it’s also a good idea to add AAAA record for mail.yourdomain.com.
mail.yourdomain.com <IPv6-address>

PTR record
A pointer record, or PTR record, maps an IP address to an FQDN. It’s the counterpart to the A record and is used for reverse DNS (rDNS) lookup.

To check the PTR record for an IP address, you can use the following command.

dig -x <IP> +short

or

host <IP>

  • sudo apt update
  • sudo apt install postfix -y

You will be asked to selete a type of mail postfix configuration

Next, enter your domain name for the system mail name eg. the yourdomain.com   not mail.yourdomain.com

after entered system mail name select OK.

Once postfix installed, it will be automatic started and a /etc/postfix/main.cf file will be generated.

to Check postfix version :

postconf mail_version

The ss (Socket statistics) utility show that the postfix master process is listening on TCP port

sudo ss -lnpt | grep master

Open TCP Port 25 (inbound) in Firewall

The inbound TCP port 25 needs to be open, so Postfix can receive emails from other SMTP servers. Ubuntu doesn’t enable a firewall by default. If you have enabled the UFW firewall, you need to open port 25 (inbound) with the following command.

sudo ufw allow 25/tcp

Checking If TCP Port 25 (outbound) is blocked

The outbound TCP port 25 needs to be open, so Postfix can send emails to other SMTP servers. The outbound TCP port 25 is controlled by your hosting provider, we can install the telnet utility to check if it’s open or blocked.
sudo apt install telnet

Run the following command on your mail server.
>telnet gmail-smtp-in.l.google.com 25

If it’s not blocked, you would see messages like below, which indicate a connection is successfully established. (Hint: Type in quit and press Enter to close the connection.)
>Trying 74.125.68.26…
Connected to gmail-smtp-in.l.google.com.
Escape character is ‘^]’.
220 mx.google.com ESMTP y22si1641751pll.208 – gsmtp
If port 25 (outbound) is blocked, you would see something like:
>Trying 2607:f8b0:400e:c06::1a…
Trying 74.125.195.27…
telnet: Unable to connect to remote host: Connection timed out

 

Sending Test mail

echo "test email" | sendmail your-account@gmail.com


From CLI you can use MUA (mail user agent) to send and read emails using the utility  : mailutils

sudo apt-get install mailutils

To Send email, type :
mail -a FROM:your-account@yourdomain.com username@gmail.com

after entering all require infomration press CTRL+D and mail will send this message.

To read mail : just type mail at CLI

Here’s how to use the mail program to manage your mailbox.

  • To read the first email message, type 1. If only parts of the message is displayed, press Enter to show the remaining part of the message.
  • To display message headers starting from message 1, type h.
  • To show the last screenful of messages, type h$ or z.
  • To read the next email message, type n.
  • To delete message 1, type d 1.
  • To delete message 1, 2 and 3, type d 1 2 3.
  • To delete messages from 1 to 10, type d 1-10.
  • To replay to message 1, type reply 1.
  • To exit out of mail, type q.

How To Increase Attachment Size Limit

Default message size =

message_size_limit = 10240000

To allow attachment of 50MB in size, run the following command
sudo postconf -e message_size_limit=52428800
Note: that the message_size_limit should not be larger than the mailbox_size_limit, otherwise Postfix might not be able to receive emails. The default value of mailbox_size_limit is 51200000 bytes (about 48MB) in the upstream Postfix package. On Ubuntu, the default value is set to 0, as can be seen with postconf | grep mailbox_size_limit
sudo systemctl restart postfix

Setting the Postfix Hostname

sudo nano /etc/postfix/main.cf

Find the myhostname parameter and set mail.yourdomain.com as the value. It’s not recommended to use the apex domain yourdomain.com as myhostname.
myhostname = mail.yourdomain.com

sudo systemctl restart postfix

Creating Email Alias

sudo nano /etc/aliases
By default, there are only two lines in this file.
# See man 5 aliases for format
postmaster: root
Normally we don’t use the root email address. Instead, the postmaster can use a normal login name to access emails. So you can add the following line. Replace username with your real username. 
root:   username

So IT would looks like this :
# See man 5 aliases for format 
postmaster: root
root: username

Save the file and rebuild the alias

sudo newaliases

Using IPv4 Only

If your mail server doesn’t have a public IPv6 address, it’s better to disable IPv6 in Postfix to prevent unnecessary IPv6 connections. Simply run the following command to disable IPv6 in Postfix.

sudo postconf -e "inet_protocols = ipv4"
sudo systemctl restart postfix

Enable Submission Service in Postfix

To send emails from a desktop email client, we need to enable the submission service of Postfix so that the email client can submit emails to Postfix SMTP server. Edit the master.cf file.

sudo nano /etc/postfix/master.cf

In submission section, uncomment or add the following lines. Please allow at least one whitespace (tab or spacebar) before -o. by default submission section is commented out. you can copy the following lines and paste them into master.cf file.

submission     inet     n    -    y    -    -    smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_tls_wrappermode=no
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
  -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
  -o smtpd_sasl_type=dovecot
  -o smtpd_sasl_path=private/auth

Microsoft Outlook mail client only supports submission over port 465. If you are going to use Microsoft Outlook, then you also need to enable submission service on port 465 by adding the following lines in the file.

smtps     inet  n       -       y       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
  -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
  -o smtpd_sasl_type=dovecot
  -o smtpd_sasl_path=private/auth

Your master.cf file submission looks like the below:

Save & close.

Restart Postfix

sudo systemctl restart postfix

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Liked this post? Share with others!

Do you want to boost your business today?

This is your chance to invite visitors to contact you. Tell them you’ll be happy to answer all their questions as soon as possible.