By Justin Ellingwood and Hanif Jetha
Postfix is a 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 18.04 server.
In order to follow this guide, you should have access to a non-root user with sudo
privileges. You can follow our Ubuntu 18.04 initial server setup guide to create the necessary user.
In order to properly configure Postfix, you will need a Fully Qualified Domain Name pointed at your Ubuntu 18.04 server. You can find help on setting up your domain name with DigitalOcean by following this guide. If you plan on accepting mail, you will need to make sure you have an MX record pointing to your mail server as well.
For the purposes of this tutorial, we will assume that you are configuring a host that has the FQDN of mail.example.com
.
Postfix is included in Ubuntu’s default repositories, so installation is simple.
To begin, update your local apt
package cache and then install the software. We will be passing in the DEBIAN_PRIORITY=low
environmental variable into our installation command in order to answer some additional prompts:
- sudo apt update
- sudo DEBIAN_PRIORITY=low apt install postfix
Use the following information to fill in your prompts correctly for your environment:
mail.example.com
, but we probably want to set the system mail name to example.com
so that given the username user1
, Postfix will use the address user1@example.com
.root@
and postmaster@
. Use your primary account for this. In our case, sammy.To be explicit, these are the settings we’ll use for this guide:
If you need to ever return to re-adjust these settings, you can do so by typing:
- sudo dpkg-reconfigure postfix
The prompts will be pre-populated with your previous responses.
When you are finished, we can now do a bit more configuration to set up our system how we’d like it.
Next, we can adjust some settings that the package did not prompt us for.
To begin, we can set the mailbox. We will use the Maildir format, which separates messages into individual files that are then moved between directories based on user action. The other option is the mbox format (which we won’t cover here) which stores all messages within a single file.
We will set the home_mailbox
variable to Maildir/
which will create a directory structure under that name within the user’s home directory. The postconf
command can be used to query or set configuration settings. Configure home_mailbox
by typing:
- sudo postconf -e 'home_mailbox= Maildir/'
Next, we can set the location of the virtual_alias_maps
table. This table maps arbitrary email accounts to Linux system accounts. We will create this table at /etc/postfix/virtual
. Again, we can use the postconf
command:
- sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virtual'
Next, we can set up the virtual maps file. Open the file in your text editor:
- sudo nano /etc/postfix/virtual
The virtual alias map table uses a very simple format. On the left, you can list any addresses that you wish to accept email for. Afterwards, separated by whitespace, enter the Linux user you’d like that mail delivered to.
For example, if you would like to accept email at contact@example.com
and admin@example.com
and would like to have those emails delivered to the sammy
Linux user, you could set up your file like this:
contact@example.com sammy
admin@example.com sammy
After you’ve mapped all of the addresses to the appropriate server accounts, save and close the file.
We can apply the mapping by typing:
- sudo postmap /etc/postfix/virtual
Restart the Postfix process to be sure that all of our changes have been applied:
- sudo systemctl restart postfix
If you are running the UFW firewall, as configured in the initial server setup guide, we’ll have to allow an exception for Postfix.
You can allow connections to the service by typing:
- sudo ufw allow Postfix
The Postfix server component is installed and ready. Next, we will set up a client that can handle the mail that Postfix will process.
Before we install a client, we should make sure our MAIL
environmental variable is set correctly. The client will inspect this variable to figure out where to look for user’s mail.
In order for the variable to be set regardless of how you access your account (through ssh
, su
, su -
, sudo
, etc.) we need to set the variable in a few different locations. We’ll add it to /etc/bash.bashrc
and a file within /etc/profile.d
to make sure each user has this configured.
To add the variable to these files, type:
- echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
To read the variable into your current session, you can source the /etc/profile.d/mail.sh
file:
- source /etc/profile.d/mail.sh
In order to interact with the mail being delivered, we will install the s-nail
package. This is a variant of the BSD xmail
client, which is feature-rich, can handle the Maildir format correctly, and is mostly backwards compatible. The GNU version of mail
has some limitations, such as always saving read mail to the mbox format regardless of the source format.
To install the s-nail
package, type:
- sudo apt install s-nail
We should adjust a few settings. Open the /etc/s-nail.rc
file in your editor:
- sudo nano /etc/s-nail.rc
Towards the bottom of the file, add the following options:
. . .
set emptystart
set folder=Maildir
set record=+sent
This will allow the client to open even with an empty inbox. It will also set the Maildir
directory to the internal folder
variable and then use this to create a sent
mbox file within that, for storing sent mail.
Save and close the file when you are finished.
Now, we can test the client out.
The easiest way to create the Maildir structure within our home directory is to send ourselves an email. We can do this with the s-nail
command. Because the sent
file will only be available once the Maildir is created, we should disable writing to that for our initial email. We can do this by passing the -Snorecord
option.
Send the email by piping a string to the s-nail
command. Adjust the command to mark your Linux user as the recipient:
- echo 'init' | s-nail -s 'init' -Snorecord sammy
You may get the following response:
OutputCan't canonicalize "/home/sammy/Maildir"
This is normal and may only appear when sending this first message. We can check to make sure the directory was created by looking for our ~/Maildir
directory:
- ls -R ~/Maildir
You should see the directory structure has been created and that a new message file is in the ~/Maildir/new
directory:
Output/home/sammy/Maildir/:
cur new tmp
/home/sammy/Maildir/cur:
/home/sammy/Maildir/new:
1463177269.Vfd01I40e4dM691221.mail.example.com
/home/sammy/Maildir/tmp:
It looks like our mail has been delivered.
Use the client to check your mail:
- s-nail
You should see your new message waiting:
Outputs-nail version v14.8.6. Type ? for help.
"/home/sammy/Maildir": 1 message 1 new
>N 1 sammy@example.com Wed Dec 31 19:00 14/369 init
Just hitting ENTER
should display your message:
Output[-- Message 1 -- 14 lines, 369 bytes --]:
From sammy@example.com Wed Dec 31 19:00:00 1969
Date: Fri, 13 May 2016 18:07:49 -0400
To: sammy@example.com
Subject: init
Message-Id: <20160513220749.A278F228D9@mail.example.com>
From: sammy@example.com
init
You can get back to your message list by typing h
, and then ENTER
:
- h
Outputs-nail version v14.8.6. Type ? for help.
"/home/sammy/Maildir": 1 message 1 new
>R 1 sammy@example.com Wed Dec 31 19:00 14/369 init
Since this message isn’t very useful, we can delete it with d
, and then ENTER
:
- d
Quit to get back to the terminal by typing q
and then ENTER
:
- q
You can test sending mail by typing a message in a text editor:
- nano ~/test_message
Inside, enter some text you’d like to email:
Hello,
This is a test. Please confirm receipt!
Using the cat
command, we can pipe the message to the s-nail
process. This will send the message as your Linux user by default. You can adjust the “From” field with the -r
flag if you want to modify that value to something else:
- cat ~/test_message | s-nail -s 'Test email subject line' -r from_field_account user@email.com
The options above are:
-s
: The subject line of the email-r
: An optional change to the “From:” field of the email. By default, the Linux user you are logged in as will be used to populate this field. The -r
option allows you to override this.user@email.com
: The account to send the email to. Change this to be a valid account you have access to.You can view your sent messages within your s-nail
client. Start the interactive client again by typing:
- s-nail
Afterwards, view your sent messages by typing:
- file +sent
You can manage sent mail using the same commands you use for incoming mail.
You should now have Postfix configured on your Ubuntu 18.04 server. Managing email servers can be a tough task for beginning administrators, but with this configuration, you should have basic MTA email functionality to get you started.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Former Senior Technical Writer at DigitalOcean, specializing in DevOps topics across multiple Linux distributions, including Ubuntu 18.04, 20.04, 22.04, as well as Debian 10 and 11.
DevOps Engineer, Former Technical Writer and Editor at DigitalOcean. Expertise in topics including Ubuntu, Kubernetes, Docker, CentOS.
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!
I Have followed the tutorial and everything worked. However, I want to use another mail exchange like outlook so i can get an interface to send and receive emails. The problem is i didn’t see a section where i can configure my password to login to an email exchange.
No dice for me. Followed along as instructed. Got to: echo 'init' | s-nail -s 'init' -Snorecord MYUSER
and there was no output at all. Using ls -R ~/Maildir
showed me that the dir wasn’t getting made. I made it myself using mkdir -p ~/Maildir/{cur,new,tmp}
.
That got things sort of working, but the s-nail thing still isn’t successful sending mail to the inbox.
What are the memory/CPU/storage requirements for a small Postfix server? Can I run it on a 1GB/1vCPU/25GB droplet, or does it need more power? Can I run it on a server with other applications on it (say, an Nginx server getting a moderate amount of traffic) or does it need a dedicated droplet?
Spent an hour trying to figure out why this wasn’t delivering mail to ~/Maildir
after following along. Turns out that a line in /etc/postfix/main.cf
executes a command that takes precedence over the home_mailbox= Maildir/
setting… If you added a snippet about it above I’m sure it would save people some trouble.
To fix my issue I had to comment out mailbox_command = procmail -a "$EXTENSION"
in /etc/postfix/main.cf
. After restarting postfix with sudo /etc/init.d/postfix restart
everything worked and ~/Maildir
was created.
Hello, I install postfix on an local server for a test. All your describe process works but I don’t recieve the mail. I think, perhaps it’s my box firewall but when I sent an email from the same PC with a smtp configuration into cakephp, I receive the mail. Have you an idea about what does’nt work ? Thank you for your help !
I have done all the steps again, made Maildir/{cur,new,tmp} by myself and checked if mailbox_command = procmail -a “$EXTENSION” is commented in /etc/postfix/main.cf
but it does not work for me. I dont know why :(
cant send or rechieves messages to/from my gmail account.
I followed the exact steps but still I’m not able to receive any email. I can see sended emails with ? file +sent
but I didn’t receive anything
I have a LAMP stack with Ubuntu 18.04 and I am having trouble getting this to work.
I followed the instructions to the letter but haven’t gotten a successful send.
At first, the ~/Maildir directory wasn’t created automatically. I had to manually create it.
Sending the init email via echo 'init' | s-nail -s 'init' -Snorecord MYUSER
doesn’t work – no emails get sent to the ~/Maildir/new folder.
I was able to send an email using cat ~/test_message | s-nail -s 'Test email subject line' -r from_field_account user@gmail.com
. However, I haven’t received the email yet in my gmail inbox. The sent
file in Maildir/sent did actually log this outgoing message.
Any suggestions?
While following this tutorial, I too came across the issue of “Maildir” was not created, test mail not received. In my situation, the cause of the issue was an existing user account on my system named “postmaster” (for other purposes).
The log (/var/log/mail.log) provided me the clue of “alias database loop for root” as to why the message bounced. As soon as I removed the other account and made a new one to use with postfix and redone the config, all worked as expected, hope this saves someone much pain…
Tried to follow the instructions but when I get to the Step 7 it only created a ~/Maildir sent entry. When I try to use s-nail to send a mail it gives Maildir is a directory the verbose gives additional mentioning .mailrc no file. Any clues and help appreciated
I run this command first
sudo apt update
Then I run this command in order to answer the prompt questions
sudo DEBIAN_PRIORITY=low apt install postfix
But the problem is the questions do not prompt after this command, and it says
*amir@myname:~# sudo DEBIAN_PRIORITY=low apt install postfix
Reading package lists… Done
Building dependency tree
Reading state information… Done
postfix is already the newest version (3.3.0-1ubuntu0.2).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
amir@myname:~# *
instead.
Does anyone know the reason? How can I fix it?
Thanks!
This is still work?, i have finish the setup.
I can receive the email, but cant send an email to gmail account.
The log is :
connect to gmail-smtp-in.l.google.com[74.125.130.27]:25: Connection timed out
Hi everyone. I’ve been trying to put this together, but I couldn’t. I follow the tutorial to the letter, but when entering the first command of step 7:
echo 'init' | s-nail -s 'init' -Samrecord sammy
But i get this answer:
ls: cannot access '/ root / Maildir': No such file or directory
I am working on a Droplet with Ubuntu 16 with Magento 2.
I need help since I’m having sales but I can’t configure the emails.
Thanks in advice,
Just chiming in to say that yeah, this tutorial didn’t work. Maybe it should be edited to include some of the suggestions in the comment stream?
Hi, it worked great !! I have to install the mailutils command to test the sending emails and it worked. However, when I send an email to my gmail account, it receives and marks as Spam. Is there a option to whitelist the SMTP so Gmail or another mail hosting doesn’t mark as spam the emails I send? Thanks.
my hostname is different to my domain name . lets say that my hostname is “flaskapp” and domain name and dropletname is testmail123.com , is it required to change the droplet, hostname , and domainname to same one ? If yes , how to change it ?
I tried to send email to my personal gmail id . but it never reached . Instead i got the following message from the gmail "Our system has detected an unusual rate of 550-5.7.28 unsolicited mail originating from your IP address. To protect our 550-5.7.28 users from spam, mail sent from your IP address has been blocked. 550-5.7.28 Please visit 550-5.7.28 " I only tried to send once . what is the work around ? at least you should have warned about it ?
echo 'init' | s-nail -s 'init' -Snorecord USRNAME
I received this:
s-nail: Panic: Cannot seed the *SSL PseudoRandomNumberGenerator, RAND_status() is 0!
Please set *ssl-rand-file* to a file with sufficient entropy.
On a machine with entropy: "$ dd if=/dev/urandom of=FILE bs=1024 count=1"
Is there any ideas about this error?
The main reason why this wasn’t working for me was because sendmail was already installed and running on the same port by default on this droplet.
Just have to tail the log to see the issue: tail -f /var/log/mail.log
localhost postfix/master[17695]: fatal: bind 127.0.0.1 port 25: Address already in use
localhost postfix/master[17694]: fatal: daemon initialization failure
localhost postfix/postfix-script[17696]: fatal: mail system startup failed
Amazon guides tend to recommend uninstalling sendmail entirely, but I was able to get this working on my droplet by killing the sendmail service:
sudo killall sendmail-mta
sudo service postfix restart
My advice is to keep on tailing the mail.log
file to figure out what the subsequent errors are and then fix each error as they occur (I also needed to add the user to the mail
group and manually create the Maildir
folder for the user due to permission issues - both of these were mentioned in above comments).
i have two queries!
Should I follow these instructions just after logging-in using my un:root and pw:…, or i have to move to somewhere else before following it?
In step 1, in Root and postmaster mail recipient: ‘sammy’ what would be my username? Is it the username which i use to login into my phpmyadmin or i can keep anything!
Thanks
When I m using below command
echo ‘init’ | s-nail -s ‘init’ -Snorecord my-user-name
I get this error:
mail: unrecognized option ‘-S’
I follow the step by step. when echo ‘init’ | s-nail -s ‘init’ -Snorecord leon it show nothing, and I check and can’t find the Maildir. Then I could do nothing.
After some time, I fix no ~/Maildir folder issue. Now I could send email to myself. cat ~/message | s-nail -s ‘Test email subject line’ -r leon@liwensoft.com leon@liwensoft.com But I could not send to outside, like this: cat ~/message | s-nail -s ‘Test email subject line’ -r leon@liwensoft.com zeng_liwen@hotmail.com It show
1 Mail Delivery Syst 81/3167 Undelivered Mail Returned to Sender
The log is follow:
Aug 23 16:19:37 wordpress-s-2vcpu-2gb-tor1-01 postfix/qmgr[9853]: 6438C4006C: from=<leon@liwensoft.com>, size=343, nrcpt=1 (queue active)
Aug 23 16:19:37 wordpress-s-2vcpu-2gb-tor1-01 postfix/smtp[9906]: 6438C4006C: to=<zeng_liwen@hotmail.com>, relay=hotmail-com.olc.protection.outlook.com[104.47.70.33]:2$
Aug 23 16:19:37 wordpress-s-2vcpu-2gb-tor1-01 postfix/smtp[9906]: 6438C4006C: lost connection with hotmail-com.olc.protection.outlook.com[104.47.70.33] while sending R$
Aug 23 16:19:37 wordpress-s-2vcpu-2gb-tor1-01 postfix/cleanup[9904]: 81DAF40071: message-id=<20200823161937.81DAF40071@WordPressb35cc4jkns202003168747c5>
Aug 23 16:19:37 wordpress-s-2vcpu-2gb-tor1-01 postfix/bounce[9907]: 6438C4006C: sender non-delivery notification: 81DAF40071
Aug 23 16:19:37 wordpress-s-2vcpu-2gb-tor1-01 postfix/qmgr[9853]: 81DAF40071: from=<>, size=3162, nrcpt=1 (queue active)
Aug 23 16:19:37 wordpress-s-2vcpu-2gb-tor1-01 postfix/qmgr[9853]: 6438C4006C: removed
Aug
I try to clean the mail.log with : sudo rm /var/log/mail.log Then I could get log anymore.
I have done this at least 3 times and it works. I receive a lot of spam but I get messages. Then down the road, it stops working. Today I found this message in the mail directory on the server: host gmail-smtp-in.l.google.com[74.125.195.26] said: 550-5.7.28 [xxx.xx.xxx.xx 1] Our system has detected an unusual rate of 550-5.7.28 unsolicited mail originating from your IP address. To protect our 550-5.7.28 users from spam, mail sent from your IP address has been blocked. 550-5.7.28 Please visit 550-5.7.28 https://support.google.com/mail/?p=UnsolicitedIPError...has anyone else experienced this?
I’ve done up to this stage: $echo ‘init’ | s-nail -s ‘init’ -Snorecord sammy
But I get the following: ls: cannot access ‘/home/sammy/Maildir’: No such file or directory
Please note that when I created a new user I literally used the same name “Sammy” (as in the tutorial)
Thanks for this. Followed to the letter and worked like a charm. Problem though, is I’m getting “Undelivered Mail Returned to Sender” from all the external addresses I’ve been sending test emails to.
The error in the response is: Access denied - Invalid HELO name
I’m guessing this has got to be a next level of configuration somewhere, but can you please direct me to a reference perhaps? hopefully a similar well-written guide. Thanks
Good day! I did everything according to the instructions from this link https://www.digitalocean.com/communi...buntu-18-04-ru. I’ll clarify, installed the postfix, then created two new users and added them to the mail group as root (the root was also added to the mail group). Next, I did everything according to the instructions (I tried it twice from scratch, once I followed the instruction on behalf of the root, the other from just the main account), when I start to do the Maildir initialization item, if I send a letter from the root or the main account to myself , then the cur, new and tmp folders are created, and if I do initialization from the created users, then these folders are not created, even Maildir is not created and letters do not come anywhere. I also tried to issue full rights to the folders of created users and added them to the list of admins (sudoers), nothing changes. If I write ls -R ~ / Maildir (when I am in the account of the created user), I get a message: “ls: cannot access ‘/home/mailNDO/Maildir’: No such file or directory”. Help, please, I’ve already tried a bunch of different instructions and the same thing every time. Even in the main.cf file, My destinations left only localhost, local.domain and the same thing. Things I have tried:
after manyhours of trying to get postfix working on ubuntu both here and linode I finally learned abou the effect of chroot implementation on postfix the postfix sorce distribution contains a set of scripts under examples/chroot-setup that creates a /etc/under the chroot filesystem at /var/spool/postfix and then copies /etc/resources there like my ssl certificates etc /passwd file the general complaint is that the ubuntu package maintainers forgot this step it is documented in the o’rilry book difinative guide to postfix basically the processes in master.cf that are chinrooted cannot see the rest of the server filesystem which is the purpose of chroot so some existing resources need to be copied there /var/spool/postfix/etc I ended up building my own script using the examplescripts as a guide then I added an rsync script to coppy my passwd file after i had a user not found that was added after the initial copy. hope I helped someone:(
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.