Sending email from your server using Postfix

Postfix is one of the most widely used SMTP server software. It's free and open-source.


When you need to send emails from your virtual machine or dedicated server, you’ll need some kind of SMTP server software. At Glesys, we typically use and recommend Postfix.

Installation and configuration of Postfix

First, create a DNS record for your server, for example, smtp.mydomain.com.

Then, install Postfix using these commands (in Debian or Ubuntu):

Command
sudo apt-get update
sudo apt-get install postfix

When the installation runs, you’ll be asked what type of configuration to use for Postfix. Choose Internet Site.

After the installation is completed on your server, there are still some configuration steps to perform. This is especially important to reduce the risk of your email being marked as spam.

Start by changing the values for myorigin, myhostname, and mydestination in the file /etc/postfix/main.cf:

Lines to modify in /etc/postfix/main.cf
myhostname = smtp.mydomain.com
myorigin = smtp.mydomain.com
mydestination = localhost

The directives myorigin and myhostname specifies the domain that the mail server claims the email originates from.

Some mail programs use the hostname in /etc/mailname as the sender when sending outbound email. To make sure your emails don't bounce, set the hostname in /etc/mailname to the same value as myhostname and myorigin above, in this example, smtp.mydomain.com.

After you have made the above changes, restart Postfix with sudo systemctl restart postfix.

PTR record

It’s important to set a correct PTR record for the IP address your server uses. You can do this in our control panel under Networking → IP Addresses.

The PTR record’s value should be your server’s hostname, e.g. smtp.mydomain.com.

Note the trailing . after the domain name.

Some email services require the domain to have an SPF record. This record lists the IP addresses that are permitted to send mail on behalf of the domain, helping to prevent spammers from hijacking the domain and sending bulk mail.

You can also set this up in our control panel under DNS.

Create a TXT‑type record containing:

Logging and troubleshooting

To make troubleshooting a bit easier, you can add the following line at the end of /etc/postfix/main.cf:

Then restart Postfix with sudo systemctl restart postfix. You can then see all the emails flowing in and out of the server in the log file /var/log/mail.log.

Last updated

Was this helpful?