How to Build Email Server on Ubuntu
At the very basic level, you need to set up Postfix SMTP server, also known as a MTA (Message Transport Agent). Postfix is a state-of-the-art message transport agent (MTA), also known as SMTP server. It is responsible for transporting messages from a mail user agent (or mail client) to a remote SMTP server.
The Postfix application design is modular, with each module running at the lowest possible privilege level required to get the job done. Postfix was designed with security in mind. Postfix integrates tightly with Unix.
Step 1: Initial Steps
Postfix does not provide functionalities that Unix already provides. So, you need to properly setup your Ubuntu server.
Set Hostname for Ubuntu Server
By default, Postfix uses your server’s hostname to identify itself when communicating with other MTAs. Hostname can have two forms: a single word or FQDN (Fully Qualified Domain Name). The single word form is used mostly on personal computers. FQDN consists of two parts: a node name and a domain name.
For example: mail.domainname.com
Here, mail is the node name, domainname.com is the domain name. FQDN is mostly used on Internet-facing servers and you should use FQDN on mail servers. FQDN will appear in the smtpd banner. Some MTAs reject message if your Postfix does not provide FQDN in smtpd banner. Some MTAs also query DNS to see if FQDN in the smtpd banner resolves to the IP of your mail server.
Enter the following command to see the FQDN form of your hostname.
hostname -f
If your ubuntu server doesn’t have a FQDN yet, you can use hostnamectl to set one.
sudo hostnamectl set-hostname your-fqdn
Set System Time
When sending messages, Postfix checks system time and appends a timestamp for the messages. This timestamp also appears in Postfix logs (/var/log/mail.log).
Use date command to check the time zone settings and current system time on Ubuntu server.
Setup DNS Records for Your Mail Server
An MX record tells other MTAs that your mail server, mail.domainname.com, is responsible for email delivery for your domain.
A common name for a mail host is mail.domainname.com.
You can specify more than one MX record and set priority for your mail servers. A lower number means higher priority.
MX record @ mail.domainname.com
An A record maps a FQDN to an IP address.
mail.domainname.com <IP-address>
A pointer record, or PTR record, maps an IP address to a FQDN. It is the counterpart to the A record and is used for reverse DNS lookup.
To check the PTR record for an IP address:
dig -x <IP> +short
or
host <IP>
Because you get IP address from your hosting provider, not from your domain registrar, so you must set PTR record for your IP in the control panel of your hosting provider.
Step 2: Install Postfix
On your ubuntu server, run the following two commands:
sudo apt-get update
sudo apt-get install postfix -y
You will be asked to select a type for mail configuration. In most cases, select the second type: Internet Site.
Next, enter your domain name for the system mail name (domainname.com). It is the name after @ symbol. This domain name will be appended to addresses that doesn’t have domain name specified.
After installed, Postfix will be automatically started and a /etc/postfix/main.cf file will be generated. Now, you can check Postfix version with this command:
sudo postconf mail_version
Step 3: Send Test Mail
You can now send and receive email from the command line.
If your Ubuntu server has a user account called user1, then the email address for this user is user1@domainname.com. You can send an email to root user root@yourdomain.com. You can also send emails to Gmail, or any other email service.
echo "test email" | sendmail your-account@gmail.com
You can also reply to this test email to see if Postfix can receive email messages.
The inbox for each user is located at /var/spool/mail/<username> and /var/mail/<username> file.
If you are unsure where to look for the inbox, use this command.
postconf mail_spool_directory
The Postfix mail log is stored at /var/log/mail.log.