Postfix is a very popular open source Mail Transfer Agent (MTA) that can be used to route and deliver email on a Linux system. It is estimated that around 25% of public mail servers on the internet run Postfix.
In this guide, we’ll teach you how to get up and running quickly with Postfix on an Ubuntu 14.04 server.
In order to follow this guide, you should have a Fully Qualified Domain Name pointed at your Ubuntu 14.04 server. You can find help on setting up your domain name with DigitalOcean by clicking here.
The installation process of Postfix on Ubuntu 14.04 is easy because the software is in Ubuntu’s default package repositories.
Since this is our first operation with apt
in this session, we’re going to update our local package index and then install the Postfix package:
sudo apt-get update
sudo apt-get install postfix
You will be asked what type of mail configuration you want to have for your server. For our purposes, we’re going to choose “Internet Site” because the description is the best match for our server.
Next, you will be asked for the Fully Qualified Domain Name (FQDN) for your server. This is your full domain name (like example.com
). Technically, a FQDN is required to end with a dot, but Postfix does not need this. So we can just enter it like:
example.com
The software will now be configured using the settings you provided. This takes care of the installation, but we still have to configure other items that we were not prompted for during installation.
We are going to need to change some basic settings in the main Postfix configuration file.
Begin by opening this file with root privileges in your text editor:
sudo nano /etc/postfix/main.cf
First, we need to find the myhostname
parameter. During the configuration, the FQDN we selected was added to the mydestination
parameter, but myhostname
remained set to localhost
. We want to point this to our FQDN too:
<pre> myhostname = <span class=“highlight”>example.com</span> </pre>
If you would like to configuring mail to be forwarded to other domains or wish to deliver to addresses that don’t map 1-to-1 with system accounts, we can remove the alias_maps
parameter and replace it with virtual_alias_maps
. We would then need to change the location of the hash to /etc/postfix/virtual
:
virtual_alias_maps = hash:/etc/postfix/virtual
As we said above, the mydestination
parameter has been modified with the FQDN you entered during installation. This parameter holds any domains that this installation of Postfix is going to be responsible for. It is configured for the FQDN and the localhost.
One important parameter to mention is the mynetworks
parameter. This defines the computers that are able to use this mail server. It should be set to local only (127.0.0.0/8
and the other representations). Modifying this to allow other hosts to use this is a huge vulnerability that can lead to extreme cases of spam.
To be clear, the line should be set like this. This should be set automatically, but double check the value in your file:
<pre> mynetworks = <span class=“highlight”>127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128</span> </pre>
We can configure additional email addresses by creating aliases. These aliases can be used to deliver mail to other user accounts on the system.
If you wish to utilize this functionality, make sure that you configured the virtual_alias_maps
directive like we demonstrated above. We will use this file to configure our address mappings. Create the file by typing:
sudo nano /etc/postfix/virtual
In this file, you can specify emails that you wish to create on the left-hand side, and username to deliver the mail to on the right-hand side, like this:
<pre> <span class=“highlight”>blah@example.com username1</span> </pre>
For our installation, we’re going to create a few email addresses and route them to some user accounts. We can also set up certain addresses to forward to multiple accounts by using a comma-separated list:
blah@example.com demouser
dinosaurs@example.com demouser
roar@example.com root
contact@example.com demouser,root
Save and close the file when you are finished.
Now, we can implement our mapping by calling this command:
sudo postmap /etc/postfix/virtual
Now, we can reload our service to read our changes:
sudo service postfix restart
You can test that your server can receive and route mail correctly by sending mail from your regular email address to one of your user accounts on the server or one of the aliases you set up.
Once you send an email to:
<pre> <span class=“highlight”>demouser</span>@<span class=“highlight”>your_server_domain.com</span> </pre>
You should get mail delivered to a file that matches the delivery username in /var/mail
. For instance, we could read this message by looking at this file:
nano /var/mail/demouser
This will contain all of the email messages, including the headers, in one big file. If you want to consume your email in a more friendly way, you might want to install a few helper programs:
sudo apt-get install mailutils
This will give you access to the mail
program that you can use to check your inbox:
mail
This will give you an interface to interact with your mail.
You should now have basic email functionality configured on your server.
It is important to secure your server and make sure that Postfix is not configured as an open relay. Mail servers are heavily targeted by attackers because they can send out massive amounts of spam email, so be sure to set up a firewall and implement other security measures to protect your server. You can learn about some security options here.
<div class=“author”>By Justin Ellingwood</div>
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!
What records Do I have to write in the dns? What servers do I have to write in outlook?
Thanks
My mail just vanishes into thin air… I set up my server the way youdescribed, but sent mail never gets delivered.
I have the same issue. I tried adding ‘inet_protocols = ipv4’ to see if sending via IPv6 was the issue but that didn’t work either.
Hi Justin,
I would like to operate two different droplets - one for www and one for email. This would mean having to set up MX records that point to a different server than what they do for www. Is this possible with Postfix? I assume I can just point the domain to the MX record(s) of the Postfix email server?
Also, will there be a follow up tutorial for Dovecot? My understanding is Postfix allows you to receive and send on the server but Dovecot then allows you to configure POP3/IMAP so that email can be accessed by other means?
Thanks in advance.
Andrew
I follow this tutorial but I’m only receiving emails; when I try to send I get no error message, but does not send anything. Does anyone know how to solve this? thanks
@luizhrmello: How are you trying to send the message? Did you try:
<pre> echo “Subject: test” | /usr/lib/sendmail -v user@destination.com Example </pre>
Could you pastebin the contents of <code>/var/log/mail.err</code> and <code>/var/log/mail.log</code> ?
Perfect! All this worked first time for me, no problems :)
I also followed Andrew SB’s instruction for sending email and this worked fine. The server seemed to hang for 30s or so but nothing major - I received the email.
Also, I did install ‘sendmail’ with
sudo apt-get install sendmail
Not sure if this was required but I presume it is?
Thanks again, folks.
Well…This is not working for me.AT ALL
At first…it kept giving me X-Postfix; unknown user: “username” error as a reply mail.
So after some searching, I changed my main.cf settings as below
mydestination = localhost, localhost.localdomain
(before : mydestination = example.com, localhost, localhost.localdomain)
After that…I didn’t get any reply mails from postfix saying about errors, But I didn’t receive any mail either…When I send an email to the server,it just vanishes.
I uninstalled SendMail too, because it gave errors while starting postfix mail system…
Im getting tired of this…Can anyone help me ?
@prabhashwara: Remember to replace the values in red with the ones that match your domain and users.
Of course I did it…I just posted it here as example.com
I still couldn’t find any solution.It keeps telling X-Postfix; unknown user: “mymail”
@prabhashwara: Removing the domain from the “mydestination” will work if you are just using this server to send mail, but you won’t be able to actually receive mail. If you want to receive mail, the domain should be there and the user must actually exist on the server or be routed to an actual user via the virtual_alias_maps as set up above. Is the user “mymail” in /etc/postfix/virtual ?
Doesn’t work
I followed the above tutorial and able to receive email using thunder bird on my windows 7 laptop but when I try to send email I get error message:
An error occurred while sending mail. The mail server responded: 4.7.1 user@hotmail.com: Relay access denied. Please check the message recipient user@hotmail.com and try again.
Is this something to do with relay setting and how can I fix this? I want to be able to use any machine either connected on my local network or from the internet to send and receive email using the domain I setup and pointed to my server?
@twix92: Are you trying to send mail from the server or from your local mail client? It sounds like you want to set up IMAP. Check out adding Dovecot to your installation:
https://www.digitalocean.com/community/articles/how-to-set-up-a-postfix-e-mail-server-with-dovecot
@Andrew SB Thanks for the quick reply. I’m trying to send mail from the client which is on a windows 7 laptop on the same router as my network. I still want to be able to send mail from any client either when I’m at home using my own network or I’m on the move work, phone and any other remote location.
Dovecot is installed and running, do I need to setup that as well? So doesn’t postfix handle both incoming and outgoing mail?
@twix92: Postfix handles the sending and receiving on the server side, while Dovecot allows you to access the mail via IMAP from another machine. This article only gets you set up with the simple ability to send and receive mail on the server. You need to be able to authenticate remotely. Take a look at the postfix configuration in that linked article with Dovecot.
This is now taking me to unexplored territory. The entries listed in master.cf in that tutorial you linked do not match the master.cf I have on my system. If I add the line -o smtpd_sasl_type=dovecot and save the file postfix gives an error when trying to reload the file or restart the server.
I have been at this since yesterday and still no success, as I understood from the various reading I done everywhere that I need to allow authenticating between the client and the server!
All sorted now, after few more hours of reading. Just had to install saslauthd for ubuntu 14.04 and after installed mail-stack-delivery which configured postfix to use ssl via socket.
Good tutorial.
The following command quits the mailutils mail program when finished reading your emails:
‘quit’
Ok, I’ve done the installation, but I don’t get it working. I’ve checked that the installation , and is correct, so suspect that my problem comes with the DNS,
When i Ask for for the FQDN i get: hostname.domain.tld
But maybe I did it wrong the MX configuration. What should I write ? Here is my actual configuration: https://gist.github.com/merqurio/6cccbb09981ebe0c9743
Thanks for your help !!
@gabriel.maeztu: When you say it doesn’t work, could you be more specific? Are you able to send mail from the command line? When you try to send an email to your newly created address does it bounce? Are you seeing any error messages in /var/log/mail.err ?
@astarr It worked for me but I wanted to forward messages to another email accounts. But it doesn’t work. Should I have to configure something at MX records? I only configured the main.cf and virtual file.
Many thanks for this tutorial.
I wanted to forward all messages to another email account. It’s really easy, you just need to edit the virtual file:
sudo nano /etc/postfix/virtual
Then, you can add:
emailA @ example.com emailB @ example.com
(remove the spaces between the @)
All sent messages to left-side email will be forwarded to right-side email.
Hope it helps.
hello i made all like the tutorial but can´t recieve mail, always get Mail Delivery Subsystem
The error that the other server returned was: 550 5.1.1 xxx@xxxxxx.com: Recipient address rejected: User unknown in local recipient table
Works great for me! Soon Server Admins won’t even have to learn anything… they’ll just follow D.O. tutorials :-p
This tutorial does work, however it fails to mention that you must open your port that you are using (smtp is port 25), that may help some people that are not receiving mail.
thanks for tut. I am trying to choose right mail server for using on Ubuntu and want to know what about iCloud like email aliases auto setup? It is mean that when i first time setup my iCloud email acount from OSX Desktop Apple Mail client or iOS mobile mail client there auto added my all iCloud email aliases and i can choose any one when sending emails. Can we add like this functionality to Postfix or another Linux mail server?
I am missing something. I install and follow instructions exactly. I can receive mail to the user I specify but I can’t send mail from command line. I installed the mail utility and do a cntrl-D and a ~. escape command and no error shows but nothing reaches my mailbox. I can recieve but can’t send.
However, to be noted I can reply to a message that was send to me.
i also notice that the email that is received from my reply has a name of Ubuntu and the name of the email is my VM… Very confusing of how this all works.
I am not able to get it working correctly as my myhostname directive is being ignored.
mail sends but it has a name of Ubuntu and and my computer name as name.
almost there need a little help.
maybe this will help someone save time.
useradd --create-home -s /sbin/nologin contact; passwd contact. The /sbin/nologin option will prevent the user from logging in to your server from ssh.
sudo iptables -D INPUT -j DROP sudo iptables -A INPUT -p tcp --dport 25 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 465 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 587 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 143 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 993 -j ACCEPT sudo iptables -A INPUT -j DROP
enjoy your new mail. next thing you want to do is configure it so it doesn’t end up in the spam folder of gmail.
(search for ) -> Check your SPF record (first google answer) :)
please guide me dovecot mail server?
ok, I installed, and can send emails to my gmail account but cannot send to another email provider like hotmail.
Can anybudy help me with that?
Thanks
I can only receive mail after following the tutorial. I know that my ISP blocks outgoing on port 25, so is the solution to relay on port 465? Just simply opening the port doesn’t solve the problem, so I’m obviously missing something. I have a godaddy domain that indentifies my home server, so maybe I need to speak to them about altering the dns records? Any thoughts are appreciated as I am clearly confused :-(
Thanks.
I am getting error: 550 5.1.1 vincenzo@xxxxxx.com: Recipient address rejected: User unknown in local recipient table
Why is this? Do i need to create virtual user vincenzo as noted in this article? Please help.
realy cool! all tutorials make life easier! big thx!
I’ve been trying to get my emails from the server to simply forward to my gmail.
So far I haven’t managed to see any incoming emails to the server (either forwarded or to a specified user)
But I have managed to get the server to send out an email using
I’ve spent a good many hours trying to just get something to break so I can try and see if there is an error anywhere, but unfortunately no luck on that front.
So anything on how I might get this working would be greatly appreciated :)
Have you added an MX record that points to your droplet to your domain? If not, create two DNS records like this:
Thanks Kamaln, I’d followed another tutorial that said that I should set the mx record to mail1.mydomain.com.
Having it as priority 5 mail.mydomian.com. has everything working on the DNS side :)
Getting error when sending mail from gmail: mail.log entries: Oct 29 03:18:25 w2p postfix/master[26682]: daemon started – version 2.11.0, configuration /etc/postfix Oct 29 03:21:10 w2p postfix/smtpd[26703]: connect from mail-pa0-f44.google.com[209.85.220.44] Oct 29 03:21:11 w2p postfix/smtpd[26703]: NOQUEUE: reject: RCPT from mail-pa0-f44.google.com[209.85.220.44]: 550 5.1.1 snapdeal@way2pets.com: Recipient address rejec$ Oct 29 03:21:12 w2p postfix/smtpd[26703]: disconnect from mail-pa0-f44.google.com[209.85.220.44] Oct 29 03:24:32 w2p postfix/anvil[26706]: statistics: max connection rate 1/60s for (smtp:209.85.220.44) at Oct 29 03:21:10 Oct 29 03:24:32 w2p postfix/anvil[26706]: statistics: max connection count 1 for (smtp:209.85.220.44) at Oct 29 03:21:10 Oct 29 03:24:32 w2p postfix/anvil[26706]: statistics: max cache size 1 at Oct 29 03:21:10 Oct 29 03:25:56 w2p postfix/master[26682]: terminating on signal 15 Oct 29 03:25:56 w2p postfix/master[26856]: daemon started – version 2.11.0, configuration /etc/postfix Oct 29 03:30:44 w2p postfix/smtpd[26867]: connect from mail-ob0-f181.google.com[209.85.214.181] Oct 29 03:30:46 w2p postfix/smtpd[26867]: NOQUEUE: reject: RCPT from mail--ob0-f181.google.com[209.85.214.181]: 550 5.1.1 snapdeal@way2pets.com: Recipient address rej$ Oct 29 03:30:46 w2p postfix/smtpd[26867]: disconnect from mail-ob0-f181.google.com[209.85.214.181] Oct 29 03:34:06 w2p postfix/anvil[26870]: statistics: max connection rate 1/60s for (smtp:209.85.214.181) at Oct 29 03:30:44 Oct 29 03:34:06 w2p postfix/anvil[26870]: statistics: max connection count 1 for (smtp:209.85.214.181) at Oct 29 03:30:44 Oct 29 03:34:06 w2p postfix/anvil[26870]: statistics: max cache size 1 at Oct 29 03:30:44
is there no way to use postfix without a domain ? i just want to use the wan ip and dont have a name ?
It’s possible to use postfix with just an IP address, but you will only be able to send emails and not receive them. If you only need to send emails, just install Postfix and accept the defaults:
Not working for me aswell. When I look at my mail.log, I see errormessages like ‘Address already in use’. Can’t find a way to configure properly
Hi,
I’ve followed this tutorial and i can send emails using for example this: echo “Subject: test” | /usr/lib/sendmail -v buy@nelsondaires.pt
i also can receive and read emails to root@nelsondaires.pt.
i add an A record to @ to my droplet IP and also a MX record to mail with priority 5
why i can’t send emails to buy@nelsondaires.pt with my personal gmail account?
any clues?
Thanks,
Hello, i follow this tutorial ,and i found maybe a issue in /var/mail/root, i don’t know why system auto ad c1 before my domain name , in /var/mail/root:(for example xxxxxx.com is my domain name) X-Original-To: root@clxxxxxx.com Delivered-To: root@clxxxxxx.com maybe it’s the issue,so system can’t send email or receive email, how to solve this? many thanks!
Follow instruction and found the following error in the mail log
postfix/master[3778]: warning /usr/lib/postfix/smtpd: bad command startup – throttling postfix/smtpd[6263]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled postfix/smtpd[6263]: fatal: open dictionary: expecting “type: name” form insted of “hash:” warning: process /usr/lib/postfix/smtpd pid 6263 exit status 1
Please advise how to fix
This dont work at all
Hi,
What if in my droplet, i have 5 domains hosted there? If i use this guide, then i will only have email generated came from only 1 domain. For example:
I have 5 Wordpress sites in the droplet. One of the site is example.com and the rest are example2.com, example3.com, example4.com and example5.com
Using this guide, all email notifications will came from example.com. I don’t want this.
I’m having trouble receiving from certain emails. I sent myself an email from hotmail which made it through fine. But when I test from a server I have setup through mediatemple I have the following problems.
This is from Tunderbird logs when I send from mediatemple email
0[d0f140]: SMTP entering state: 0 0[d0f140]: SMTP Response: 550 Unrouteable address 0[d0f140]: SMTP entering state: 6 0[d0f140]: SMTP Send: QUIT
When I check mail.log I can see
Feb 26 09:46:12 Personal postfix/smtpd[5233]: connect from webserver1.grimmtheory.com[23.253.57.140] Feb 26 09:46:12 Personal postfix/smtpd[5233]: disconnect from webserver1.grimmtheory.com[23.253.57.140]
An immediate disconnection. I can see when I send through hotmail it all get processed fine. Any thoughts?
I thought it might be I have the incorrect ports open, but I don’t think its the case.
25/tcp ALLOW Anywhere
Any help would be appreciated
It doesn’t work for me, can anyone help me with it. See my Stackoverflow question.
When installing Ubuntu LEMP 14.04 the myhostname get’s set automatically contrary to the text above, so one less thing to change!
I see it’s been asked here but I can’t find an answer. What records should I have in the DNS section in order for this to work. The first guide provided here gives an option to add 5 MX records, but as I understand these are for a domain that uses google mail servers.
So can anybody help me (and probably others) out with this?
Thanks!
Does not go to plan with my… I use the command
sudo apt-get install postfix
And it results into 'postfix is already the newest version, does not ask me anything about an domein… Please help me out! I tryed to install it by another tutorial but that gone wrong, so maby i should remove Postfix compleatly, but i dont know how. Please help me out on this one!
Thanks for this, I am however having a slight problem with my from address.
I have a Nagios server called
nagios.vassie.home
, it is sending emails fromnagios@nagios.vassie.home
how do I correct this?Here is a copy of my main.cf
Sending email fine. Was having issues receiving email. Sending email find. Did some troubleshooting. Realised I didn’t have the correct username in /etc/postfix/virtual. Updated that, still couldn’t receive. Then went on to mxtoolbox and looked up my domain to confirm MX records had propagated. It had. Did an SMTP test and this failed. Realised I had UFW on.
sudo ufw allow postfix
thensudo service postfix restart
and now I am receiving email. Thanks for the post.MISSING FROM TUTORIAL
You will need to allow SMTP so you need to open port 25 in iptables
If you are using UFW the command is as follows
Hope this helps!
Had to comment out #mailbox_command = procmail -a “$EXTENSION” in main.cf in order to receive mail as they were going to procmail which I do not have configured.
This comment has been deleted
Hey, i get emails sent to me, so i Can se its Working, but how do i set it up for Remote Access, like a mailbrowser like iMail
Hi,
I have finished installing postfix, but I am still confused about how to set up my mx records :(. I have my name servers properly set and my A records are working, but this is my first run on creating an e-mail server. My domain name is sharingtohear.com and I set the MX records as following- sharingtohear.com. priority = 0. When I try sending a test email, I am receiving a response that the domain name is being recognized, but the server is rejecting the e-mail, What do I need to do to fix this?
Thank you !
To everyone having trouble receiving emails (from outside) on your DO server after installing Postfix you need this:
inet_ interfaces = <your_ip>, 127.0.0.1
No space before interfaces. It will set Postfix listening on port 25 of your external IP. That’s it.
Not working. I forgot how I fixed it the last time…SENDMAIL just hangs the server for a decade, so I can’t use that. POSTFIX does absolutely nothing.
Somebody please delete this incomplete guide
I’m totally confused. I completed the steps (not sure correctly). I try to send a test email and I receive a message from the mailer daemon. I also tried to send myself and email and it sends, but I never receive it. Can anyone help me? My server is 162.243.230.157.