Postfix is a mail transfer agent (MTA), an application used to send and receive email. In this tutorial, we will install and configure Postfix so that it can be used to send emails by local applications only — that is, those installed on the same server that Postfix is installed on.
Why would you want to do that?
If you’re already using a third-party email provider for sending and receiving emails, you do not need to run your own mail server. However, if you manage a cloud server on which you have installed applications that need to send email notifications, running a local, send-only SMTP server is a good alternative to using a 3rd party email service provider or running a full-blown SMTP server.
In this tutorial, you’ll learn how to install and configure Postfix as a send-only SMTP server.
Note: As of June 22, 2022, DigitalOcean is blocking SMTP for all new accounts. As a part of this new policy, we have partnered with SendGrid so our customers can still send emails with ease. You can learn more about this partnership and get started using SendGrid by checking out our DigitalOcean’s SendGrid Marketplace App.
To follow this tutorial, you will need:
One Ubuntu 16.04 Droplet set up with the Ubuntu 16.04 initial setup guide, including creating a sudo non-root user
A valid domain name, like example.com
, pointing to your server. You can set that up by following this host name tutorial
Note that your server’s hostname should match this domain or subdomain. You can verify the server’s hostname by typing hostname
at the command prompt. The output should match the name you gave the Droplet when it was being created.
In this step, you’ll learn how to install Postfix. The most efficient way to install Postfix and other programs needed for testing email is to install the mailutils
package.
First, update the package database:
Finally, install Postfix. Installing mailtuils
will install Postfix as well as a few other programs needed for Postfix to function.
Near the end of the installation process, you will be presented with a window that looks exactly like the one in the image below. The default option is Internet Site. That’s the recommended option for this tutorial, so press TAB
, then ENTER
.
After that, you’ll get another window just like the one in the next image. The System mail name should be the same as the name you assigned to the server when you were creating it. If it shows a subdomain like subdomain.example.com
, change it to just example.com
. When you’ve finished, press TAB
, then ENTER
.
After installation has completed successfully, proceed to step two.
In this step, you’ll read how to configure Postfix to process requests to send emails only from the server on which it is running, that is, from localhost.
For that to happen, Postfix needs to be configured to listen only on the loopback interface, the virtual network interface that the server uses to communicate internally. To make the change, open the main Postfix configuration file using nano
or your favorite text editor.
With the file open, scroll down until you see the following section.
. . .
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
. . .
Change the line that reads inet_interfaces = all
to inet_interfaces = loopback-only
.
. . .
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
. . .
Another directive you’ll need to modify is mydestination
, which is used to specify the list of domains that are delivered via the local_transport
mail delivery transport. By default, the values are similar to these:
/etc/postfix/main.cf. . .
mydestination = $myhostname, example.com, localhost.com, , localhost
. . .
The recommended defaults for that scenario are given in the code block below, so modify yours to match:
/etc/postfix/main.cf. . .
mydestination = $myhostname, localhost.$mydomain, $mydomain
. . .
Save and close the file.
If you’re hosting multiple domains on a single server, the other domains can also be passed to Postfix using the mydestination
directive. However, to configure Postfix in a manner that scales and that does not present issues for such a setup involves additional configurations that are beyond the scope of this article.
Finally, restart Postfix.
In this step, you’ll test whether Postfix can send emails to an external email account using the mail
command, which is part of the mailutils
package that was installed in Step 1.
To send a test email, type:
In performing your own test(s), you may use the body and subject line text as-is, or change them to your liking. However, in place of your_email_address
, use a valid email address. The domain part can be gmail.com
, fastmail.com
, yahoo.com
, or any other email service provider that you use.
Now check the email address where you sent the test message. You should see the message in your inbox. If not, check your spam folder.
Note that with this configuration, the address in the From field for the test emails you send will be sammy@example.com
, where sammy is your Linux username and the domain part is the server’s hostname. If you change your username, the From address will also change.
The last thing we want to set up is forwarding, so you’ll get emails sent to root on the system at your personal, external email address.
To configure Postfix so that system-generated emails will be sent to your email address, you need to edit the /etc/aliases
file.
The full contents of the file on a default installation of Ubuntu 16.04 are as follows:
# See man 5 aliases for format
postmaster: root
With that setting, system generated emails are sent to the root user. What you want to do is edit it so that those emails are rerouted to your email address. To accomplish that, edit the file so that it reads:
/etc/aliases# See man 5 aliases for format
postmaster: root
root: your_email_address
Replace your_email_address
with your personal email address. When finished, save and close the file. For the change to take effect, run the following command:
You may now test that it works by sending an email to the root account using:
You should receive the email at your email address. If not, check your spam folder.
That’s all it takes to set up a send-only email server using Postfix. You may want to take some additional steps to protect your domain from spammers.
If your use case is to receive notifications from your server at a single address, emails being marked as spam is a major issue because you can whitelist them. However, if your use case is to send emails to potential site users (such as confirmation emails for a message board sign-up), you should definitely set up SPF records and DKIM so your server’s emails are more likely to be seen as legitimate.
How To use an SPF Record to Prevent Spoofing & Improve E-mail Reliability
How To Install and Configure DKIM with Postfix on Debian Wheezy Though that article was written for Debian Wheezy, the same steps apply for Ubuntu 16.04.
If configured correctly, this makes it difficult to send spam with an address that appears to originate from your domain. Doing these additional configuration steps will also make it more likely for common mail providers to see emails from your server as legitimate.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Excelent!
the best gan
Many thanks for this very useful tutorial!
This guide works too for Debian 8 Jessie, with one difference:
The package mailutils does not contain postfix, so the postfix package has to be installed too:
sudo apt-get install postfix
Two remarks:
mydestination =
line in/etc/postfix/main.cf
will look likemydestination = $mydomain, localhost.$mydomain, $mydomain
sudo less /var/log/mail.log
gave me valuable information for troubleshooting.
This comment has been deleted
I cant test postfix my nail log shows
I cant test postfix my mail log shows this error : Connection timed out
It could be that your fw blocking smtp port - in this case it’s 25.
is there a vay to fix this problem i have the same problem
I had to stop sendmail, purge uninstall sendmail, force reinstall postfix, and then reboot the machine. No longer have the exact commands, as I pieced it together from a lot of different sources, but do try the reboot. Even after I installed postfix, sendmail was still holding the 25 port.
This comment has been deleted
Exits with non-zero status.
Followed this tutorial to the tee but no matter what postfix always attempts to send the mail out as user@hostname (i.e. someone@machine rather than someone@domain.com). As a result, all mail is bounced with “Sender address rejected: need fully-qualified address”.
/etc/mailname has the correct domain. Droplet was renamed from just a hostname to a FQDN
Still did not solve the problem, even after purging and reinstalling mailutils and postfix.
Any ideas where I may have gone wrong or what I can do to solve this?
Have you configured the smtp_generic_maps directive in the /etc/postfix/main.cf and set it to use the filepath correctly?
I have a problem when you have a server domain.com and a mail.domain.com and you try to install a send only postfix installation on domain.com.
When u set up, even when i set up as system.domain.com for postfix destination, and I try to send a email to somethingoruser@domain.com (which has a MX record pointing to mail.domain.com), it will say it doesn’t have a user.
No idea how to fix this.
I found that even on Ubuntu 16.04 postfix was not installed with mailutils and I had to install that separately.
I also had some trouble with sendmail being on port25 even though I thought I had stopped it, uninstalled it and reinstalled postfix.
doing a
sudo reboot
on the server helped solve that. now I need to resolve the “from” address as it’s saying it’s from the droplet name and not from my domain. baby steps.In the second to last paragraph you say:
If your use case is to receive notifications from your server at a single address, emails being marked as spam is a major issue because you can whitelist them. However, if your use case is to send emails to potential site users (such as confirmation emails for a message board sign-up), you should definitely set up SPF records and DKIM so your server’s emails are more likely to be seen as legitimate.
But I think you meant
If your use case is to receive notifications from your server at a single address, emails being marked as spam isn’t a major issue because you can whitelist them. However, if your use case is to send emails to potential site users (such as confirmation emails for a message board sign-up), you should definitely set up SPF records and DKIM so your server’s emails are more likely to be seen as legitimate.
Can anybody guide me about the postfix configuration on Ubuntu 16.04 (Apache2)…? I have 2 domains in 1 DO droplet. Domain1: instamust.com & domain2: sociomust.com. Domain1 points to Gsuite mail server (through MX) and domain2 points to Zoho (through their MX). I have set these values correctly in respective DNS section. But still now can’t able to send/receive emails in domain based email a/c (for both domains) from both of my website’s contact form. Any help will be highly appreciated. Thanks
But i am getting emails to my Yahoo/Gmail address when set as Cc in those Contact Form. But in Spam folder- Very worried!
Error: relay=local, delay=0.09, delays=0.04/0.01/0/0.04, dsn=5.1.1, status=bounced (unknown user: “webmaster”
I followed this tutorial to the letter on Ubuntu 16 but emails are being sent from user@www.domain.com instead of what should be user@domain.com
what to do ?
Add this to
/etc/postfix/mail.cf
That will strip any of the domain part before domain.com.
Then run
sudo systemctl reload postfix.service
There’s one real problem with this walk-through, otherwise I’d be using this configuration for server notification and monitoring emails:
Every single email is sent unencrypted, which is part of why many email services kick the emails as spam.
Off the top of my head, I’m not even certain they can be encrypted using this configuration. I’m currently trying out different possible configurations on a VM. I’ll let everyone know how it goes.
In addition to the instructions above, create your own self-signed certs, use LetsEncrypt, or use purchased certificates/keys (make sure you use wildcard or certificates specific to your hostname/FQDN), and edit main.cf to include:
It cleared the warnings from the emails for me, and shows TLS was used.
Edit: Corrected to show
smtp_use_tls=yes
, notsmtpd_use_tls=yes
Perfect, thanks for this.
I am using Postfix with more domains on one droplet and I added the certificate of the main domain. Works without any problem for the rest of the domains too.
Brilliant, works like a charm, thanks Kevinruffus!
I see this for mydestination: $myhostname, severname, localhost.localdomain, , localhost
What do I do?
I’m having some issues following this tutorial. I’ve followed the tutorial successfully through Step 3. But Step 4 doesn’t seem to work. I’m new to Postfix and not an admin so completely possible I am doing this completely wrong.
This works for me. I get an email from doing the following. echo “This is the body of the email” | mail -s “This is the subject line” myemail@gmail.com
This doesn’t work: echo “This is the body of the email” | mail -s “This is the subject line” root
Any clues?
This is a month late, but why are you trying to send mail to ‘root’?
I followed this tutorial completely, but still my emails are not going to my email address.
Although emails are being saved to local Maildir directory.
Can you please help me sending emails to any email address from my server. I am using php to send emails.
If there is any other tutorial to follow to do what I want?
I have a LEMP 16.04 installed with virtual hosts. There are several wordpress websites on this droplet.
Will this setup work for each seperate wordpress installation to send emails successfully? If so, which domain should I choose as System mail name? Does it matter which one I use?
On my installation of Ubuntu 16.04 (not on your servers), the installation of mailutils didn’t produce a postfix configuration screen.
I had the same, fixed it by removing postfix and mailutils first:
sudo apt-get purge --auto-remove postfix sudo apt-get purge --auto-remove mailutils
To run the postfix configuration program, run the command:
Excellent guide, thank you.
Minor note, I think there was an error in this sentence
I’m assuming you meant “not a major issue”.
Thanks for the great article.
I followed this tutorial to the letter on Ubuntu 16 but emails are being sent from user@www.domain.com instead of what should be user@domain.com
what to do ?
From the comments it seems several people are having problems routing mail sent to their domain to an external mail service rather than locally. Perhaps the tutorial could be updated to show how to do this?
I think the problem is Postfix tries to use the
local_transport
delivery method, and delivers the mail to the local mail directory instead of resolving the domain to the remote email service.To fix this, I changed
mydestination
in/etc/postfix/main.cf
to:Thanks! Followed the tutorial above but had the same problem as most people commenting above. This response fixed it for me, thanks! :)
Hello ! I am searching to configure postfix on a machine hosting gitlab for mail notification, forwarding them on an external smtp server (xx.mail.ovh.net, but our mail are in the format xxxxx@oursociety.fr ) . At first I set the standard postfix configuration null client. But it set the external server as a relay host. It does not work with the external smtp server, as they do not allow it. They say we can send directly with postfix, without anymore details. Your solution seems to be the right one. But It is not clear how the mail is forwarded to your external SMTP server ? Don’t you have to set it somewhere ?
My machine is not part of a domain, we have an entry in our DNS to point to it “gitlabmachine”. The hostname is set to the same entry name : gitlabmachine.
So, how do I set postfix to allow gitlab (executed under identity git) to send mail to the external server ?
You say “A valid domain name, like example.com, pointing to your server.” => which one? gitlabmachine or xx.mail.ovh.net ? I set it to “oursociety.fr” System mail name : oursociety.fr mydestination = oursociety.fr
aliases : root: intranet@oursociety.fr admin: intranet@oursociety.fr
But on the test the result is :
Hi guys,
Just want to ask if there is a way to encrypt the email? What do I need to configure or what to install? Thanks.
For send-only MTA, Is there any advantages to using postfix over say nullmailer or ssmtp with Google Apps? Any input will be much appreciated.
First, thank you very much for this wonderful tutorial.
I have a question. Let me try to explain my question through a example: NGINX Server Blocks in available three different live websites:
Droplet name is also different: Test-Dad
How should I install and configure Postfix as a Send-Only SMTP Server?
Thank you!
i followed this but there is only 1 problem. it doesnt forward the emails to my personal email “XXXX@outlook.com” any idea?
Of course I copied the only mispelled command:
mailtuils
should bemailutils
.Super helpful!
For some reason I can’t get anything sent to my gmail account, all I get is this:
Final-Recipient: rfc822; derekrife@gmail.com Original-Recipient: rfc822;derekrife@gmail.com Action: failed Status: 5.7.1 Remote-MTA: dns; gmail-smtp-in.l.google.com Diagnostic-Code: smtp; 550-5.7.1 [104.131.67.181 1] Our system has detected an unusual rate of 550-5.7.1 unsolicited mail originating from your IP address. To protect our 550-5.7.1 users from spam, mail sent from your IP address has been blocked. 550-5.7.1 Please visit 550-5.7.1 https://support.google.com/mail/?p=UnsolicitedIPError to review our 550 5.7.1 Bulk Email Senders Guidelines. k66si2537580qkf.457 - gsmtp
Anyone got any clues on how to fix this?
I’ve tried, and it works with msn, but not gmail.
I had the same. Check the message above from @lobeless14 , it fixed it for me.
Great tutorial! I’m having still a problem to send mail outside of my local network. My university provide me with a local VM on which I run ubuntu 16.04 with postfix. emails are fine from root to other users and users to users but will not send to **@gmail.com because it says “Connection timed out.” I already change postfix.cf file to add proper tls keys, etc, and change all to ipv4 on last line for inet_protocol.
Here is example of maillog entry: Feb 13 15:50:33 myNetwork postfix/smtp[21654]: 18554DFE60: to=traumann@gmail.com, relay=none, delay=150, delays=0.01/0/150/0, dsn=4.4.1, status=deferred (connect to alt4.gmail-smtp-in.l.google.com[64.233.190.26]:25: Connection timed out)
I think issue is related to my domain being a .myschool.local rather than a public facing network, like .myschool.edu But school is not blocking anything. So maybe google not like traffik from …local ?
Any ideas would be great.
Thanks,
traumann
I followed the tutorial and I am able to send the emails to my personal email. But, The email ID is moved to the spam folder. Also, the sender is user@hostname instead I want to make it as user@domain.com. Can someone help me solve those two issues?
Hello,
I have Tested this method and it keeps sending mails to junk. I have tried it on yahoo and gmail, still sends all to junk. What other configuration do i need to do to achieve inbox ? Please someone should help me out on this. Thanks.
Richard.
it’s likely your IP address is blacklisted. this can be a real pain sometimes…
here are a couple of helpful links:
Hello everyone,
I believe I followed this guide correctly and I have set up an SPF record and configured DKIM. Yet, every email is received in my Spam folder when sent to Gmail adresses. Worse, when sent to other private mail providers, the mail isn’t even received and I get an ‘Undelivered Mail Returned to Sender’ mail with the error ‘This sender address is not valid (in reply to RCPT TO command)’.
Any ideas where I may have gone wrong or what I can do to solve this?
A quick way to set the hostname:
(Source)
I followed these instructions, however the From field for the test email is “sammy@hostname.example.com”, not “sammy@example.com”.
Why the need to install mailutils and not just postfix? Installing mailutils also installed MySQL, and I do not want MySQL on two of my three droplets for this project. I only need a means to either send mail or relay it to our GApps to send the mail. (From what I have read, there is no need for MySQL if using Postfix in a send-only situation.)
Aannd for that part, “However, to configure Postfix in a manner that scales and that does not present issues for such a setup involves additional configurations that are beyond the scope of this article.”, I asked @linuxbabe (twitter) and she kindly made a tutorial for us: https://www.linuxbabe.com/mail-server/postfix-send-only-multiple-domains-ubuntu
You need this command:
sudo dpkg-reconfigure postfix
to open configuration window.For some reason I can’t get anything sent to my gmail account, all I get is this:
Final-Recipient: rfc822; derekrife@gmail.com Original-Recipient: rfc822;derekrife@gmail.com Action: failed Status: 5.7.1 Remote-MTA: dns; gmail-smtp-in.l.google.com Diagnostic-Code: smtp; 550-5.7.1 [104.131.67.181 1] Our system has detected an unusual rate of 550-5.7.1 unsolicited mail originating from your IP address. To protect our 550-5.7.1 users from spam, mail sent from your IP address has been blocked. 550-5.7.1 Please visit 550-5.7.1 https://support.google.com/mail/?p=UnsolicitedIPError to review our 550 5.7.1 Bulk Email Senders Guidelines. k66si2537580qkf.457 - gsmtp
Anyone got any clues on how to fix this?
I’ve tried, and it works with msn, but not gmail.
I had the same. Check the message above from @lobeless14 , it fixed it for me.
Great tutorial! I’m having still a problem to send mail outside of my local network. My university provide me with a local VM on which I run ubuntu 16.04 with postfix. emails are fine from root to other users and users to users but will not send to **@gmail.com because it says “Connection timed out.” I already change postfix.cf file to add proper tls keys, etc, and change all to ipv4 on last line for inet_protocol.
Here is example of maillog entry: Feb 13 15:50:33 myNetwork postfix/smtp[21654]: 18554DFE60: to=traumann@gmail.com, relay=none, delay=150, delays=0.01/0/150/0, dsn=4.4.1, status=deferred (connect to alt4.gmail-smtp-in.l.google.com[64.233.190.26]:25: Connection timed out)
I think issue is related to my domain being a .myschool.local rather than a public facing network, like .myschool.edu But school is not blocking anything. So maybe google not like traffik from …local ?
Any ideas would be great.
Thanks,
traumann
I followed the tutorial and I am able to send the emails to my personal email. But, The email ID is moved to the spam folder. Also, the sender is user@hostname instead I want to make it as user@domain.com. Can someone help me solve those two issues?
Hello,
I have Tested this method and it keeps sending mails to junk. I have tried it on yahoo and gmail, still sends all to junk. What other configuration do i need to do to achieve inbox ? Please someone should help me out on this. Thanks.
Richard.
it’s likely your IP address is blacklisted. this can be a real pain sometimes…
here are a couple of helpful links:
Hello everyone,
I believe I followed this guide correctly and I have set up an SPF record and configured DKIM. Yet, every email is received in my Spam folder when sent to Gmail adresses. Worse, when sent to other private mail providers, the mail isn’t even received and I get an ‘Undelivered Mail Returned to Sender’ mail with the error ‘This sender address is not valid (in reply to RCPT TO command)’.
Any ideas where I may have gone wrong or what I can do to solve this?
A quick way to set the hostname:
(Source)
I followed these instructions, however the From field for the test email is “sammy@hostname.example.com”, not “sammy@example.com”.
Why the need to install mailutils and not just postfix? Installing mailutils also installed MySQL, and I do not want MySQL on two of my three droplets for this project. I only need a means to either send mail or relay it to our GApps to send the mail. (From what I have read, there is no need for MySQL if using Postfix in a send-only situation.)
Aannd for that part, “However, to configure Postfix in a manner that scales and that does not present issues for such a setup involves additional configurations that are beyond the scope of this article.”, I asked @linuxbabe (twitter) and she kindly made a tutorial for us: https://www.linuxbabe.com/mail-server/postfix-send-only-multiple-domains-ubuntu
You need this command:
sudo dpkg-reconfigure postfix
to open configuration window.what are you saying. I need smtp credentials for it. Why do I need this procedure. Email to root only. I need to get smtp host
Hola!, que tal? Instalé postfix para solo enviar correos, como lo marca en el tutorial, pero necesito que desde un backend con nodejs poder usar este servicio, pero no me deja, hay alguna configuración que me falta por hacer?