Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup a Local Only SMTP Email Server (Linux, Unix, Mac)

Setup a Local Only SMTP Email Server (Linux, Unix, Mac)

1 - Point localhost.com to your machine

Most of programs will not accept an email using just @localhost as domain. So, edit /etc/hosts file to make the domain localhost.com point to your machine, including this content to the file:

127.0.0.1 localhost.com

2 - Install Postfix

Fedora/CentOS/RHEL: sudo yum install postfix

Ubuntu: sudo apt-get install postfix

MacOSX: Postfix is already installed by default.

3 - Configure Postfix to Local only

  • During postfix install process, the configure text dialog will display five options:

    General type of mail configuration: 
    
    No configuration
    Internet Site
    Internet with smarthost
    Satellite system
    Local only
    
  • Select "Local Only".

  • For the domain name, use the default suggested and finish the install.

4 - Configure a Catch-all Address

Enabling this, you can use any email address ending with "@localhost" or "@localhost.com".

Example: here, my unique account is rael@localhost.com. But while testing systems, I can use any address like joe@localhost.com, foo@localhost.com, etc, because all will be redirected to rael@localhost.com

  • If not exists, create file /etc/postfix/virtual: sudo nano /etc/postfix/virtual
  • Add the following 2 lines content, replacing <your-user> with your Unix user account:
@localhost <your-user>
@localhost.com <your-user>
  • Save and close the file.
  • Configure postifx to read this file:
    • Open /etc/postfix/main.cf: sudo nano /etc/postfix/main.cf
    • And check if this line is enabled, or add it if not exists: virtual_alias_maps = hash:/etc/postfix/virtual
  • Activate it: sudo postmap /etc/postfix/virtual
  • Reload postfix: sudo systemctl restart postfix
  • If you're under non systemd distro, like Ubuntu 14.04, service restart command probably is: sudo service postfix reload

5 - Install Thunderbird

Ubuntu: sudo apt-get install thunderbird

6 - Configure Thunderbird

  • Skip the welcome screen (click in the button to use existing accounts);
  • Click in the Settings button at top right (similar to Chrome settings) then click on Preferences > Account Settings
  • Under Account Actions choose "Add Other Account"
  • Select "Unix Mailspool (Movemail)"
  • Your account will be <your-user>@localhost (of course, replace <your-user> with your user account). Don't use <your-user>@(none), use <your-user>@localhost
  • Ingoing and Outgoing server will be: localhost
  • Restart (close and reopen) Thunderbird.

7 - Start your Mail Spool file

  • This step have two purposes: test your install and stop the Unable to locate mail spool file. message.
  • Using Thunderbird, send new email to <your-user>@localhost, replacing <your-user> with your user account
  • Click on "Get Mail"
  • Test catch-all: send new email to averagejoe@localhost
  • Click on "Get Mail" and you'll see the message at Inbox.
@dipakpatilsymc

This comment has been minimized.

Copy link

@dipakpatilsymc dipakpatilsymc commented Dec 30, 2015

Great article! Nice and easy.
Also its easy to configure multiple domains, I did step 1 and 4 for abc.com and worked like charm.
Now I receive emails directed to anyone@localhost.com and anyone@abc.com in same inbox, which I wanted.

@dangerouse

This comment has been minimized.

Copy link

@dangerouse dangerouse commented Apr 27, 2016

How about using xx.lvh.me or xip.io and then you can skip editing the /etc/hosts file?

@Feyisayo

This comment has been minimized.

Copy link

@Feyisayo Feyisayo commented Jul 30, 2016

I followed these instructions and it worked like a charm

Thanks a lot

@bpcrao

This comment has been minimized.

Copy link

@bpcrao bpcrao commented Nov 6, 2016

Helpful, thanks a lot saved my time.

@jonathanbossenger

This comment has been minimized.

Copy link

@jonathanbossenger jonathanbossenger commented Feb 11, 2017

Brilliant stuff, saved me hours of Googling. Thanks

@roopezero

This comment has been minimized.

Copy link

@roopezero roopezero commented Feb 21, 2017

Worked fine for a while, but for some reason this stopped working for me after rebooting. Thunderbird keeps being "Unable to locate mail spool file." even though it exists in /var/mail/user_name. This problem persists even after removing /home/user_name/.thunderbird and reinstalling both postfix and thunderbird.

Any ideas what could cause this?

EDIT: Got it fixed. Turned out my environment variable $MAIL pointed at /home/user_name/Mail instead of /var/mail/user_name. I fixed this by creating a symbolic link to /var/mail/user_name with ln -s /var/mail/user_name /home/user_name/Mail

@chriskerley78910

This comment has been minimized.

Copy link

@chriskerley78910 chriskerley78910 commented Mar 13, 2017

it works!, thanks man.

@wilensky

This comment has been minimized.

Copy link

@wilensky wilensky commented Mar 22, 2017

Seal of apporval

@raelgc

This comment has been minimized.

Copy link
Owner Author

@raelgc raelgc commented Apr 27, 2017

@roopezero I usually get this message Unable to locate mail spool file until I send an email to my local account and receive it (this will start the local pool). It's the last step in this tutorial :)

@heartbeathero

This comment has been minimized.

Copy link

@heartbeathero heartbeathero commented May 4, 2017

Is "ssudo service postfix reload" correct?

@dayze

This comment has been minimized.

Copy link

@dayze dayze commented Jun 8, 2017

Thanks, save me a lot of time ;)

@burrscurr

This comment has been minimized.

Copy link

@burrscurr burrscurr commented Jun 9, 2017

I had a problem which occured because I already had thunderbird installed and some mail accounts configured there. In the localhost mail configuration part was some non-local smtp server from another mail account selected. This could be fixed with adding a custom smtp server (also in Account settings). Server is localhost, port 25 (can be found out with nmap localhost). Then select this localhost server as smtp server in your local host account.

Even with my struggle this saved a lot of time for me :)

@JonhZono

This comment has been minimized.

Copy link

@JonhZono JonhZono commented Jun 19, 2017

After we set up this local mail server, how about everyone can access that mail server or not, eg.test@localhost.com.
Can we provide the access right to anyone to access or use that mail or not?

@ChathurikaA

This comment has been minimized.

Copy link

@ChathurikaA ChathurikaA commented Jul 13, 2017

I configured local Email Server as you mentioned . But I still get "Unable to locate mail spool file" in Thunderbird . Can you please show me a way to fix it. Also couldn't we create pop3 or Imap store as incoming mail server and smtp server as outgoing mail server in Locally.

@superjoefly

This comment has been minimized.

Copy link

@superjoefly superjoefly commented Aug 5, 2017

@roopezero I had the same issue...thanks for the fix 👍

@minitauros

This comment has been minimized.

Copy link

@minitauros minitauros commented Aug 10, 2017

I'm getting an error regarding the SMPT settings. It reads

Sending of the message failed.
The message could not be sent because connecting to Outgoing server (SMTP) localhost failed. The server may be unavailable or is refusing SMTP connections. Please verify that your Outgoing server (SMTP) settings are correct and try again.

Any idea how I can test what is wrong here? When I use for example PHP to send mail, I do receive it, but when I use Thunderbird to send mail, I cannot send it.

SMTP settings in Thunderbird are:

Server Name localhost
Port 587
Connection security none
Authentication method Password, transmitted insecurely
User Name My user name


EDIT Changing the port number to 25 fixed it. While I was typing this all of a sudden I realized :). Just leaving this message here in case anyone else runs into this problem.

@pogster

This comment has been minimized.

Copy link

@pogster pogster commented Nov 8, 2017

Port 25 was also a really important information for me. Thanks for the guide.

@MegzKay

This comment has been minimized.

Copy link

@MegzKay MegzKay commented Jan 5, 2018

Thank you so much for this really easy to follow guide. I find it rare to find such a simple guide like this for Linux systems. Great job

@lykre

This comment has been minimized.

Copy link

@lykre lykre commented Jan 26, 2018

Hi,

thank you for this guide!

I had to run

sudo postmap /etc/postfix/virtual

to create a file called virtual.db or else the server would reject the e-mail I was trying to send. It is also very helpful to check your /var/log/mail.log if things aren't working as expected.

@Azimboy

This comment has been minimized.

Copy link

@Azimboy Azimboy commented Jan 30, 2018

Great article! Many thanks!

@shahnazmshariff

This comment has been minimized.

Copy link

@shahnazmshariff shahnazmshariff commented Feb 10, 2018

Great tutorial! I want to use webmail client (Roundcube/ after logic webmail pro) instead of Thunderbird. Could you, if possible, help me out with this setup, please?

@qoomon

This comment has been minimized.

Copy link

@qoomon qoomon commented Feb 19, 2018

just use localhost.org

@zuhairkareem

This comment has been minimized.

Copy link

@zuhairkareem zuhairkareem commented Feb 20, 2018

I made a mistake by giving custom "user" name in /etc/postfix/virtual instead of the correct Linux username and was getting an unknown user error.

to=<customusername@localhost>, orig_to=<customusername@localhost>, relay=local, delay=0.1, delays=0.07/0/0/0.03, dsn=5.1.1, status=bounced (unknown user: "customusername")

As soon as I gave correct one the problem was solved.
Thanks for the fantastic tutorial.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 9, 2018

Before starting/restarting postfix I had to do sudo newaliases as per ArchWiki. Otherwise Thunderbird always complains about Unable to locate mail spool file.

@mjrulesamrat

This comment has been minimized.

Copy link

@mjrulesamrat mjrulesamrat commented Aug 3, 2018

Awesome. Worked like charm!
id -u -n
To get Unix user account name to replace

@bochove

This comment has been minimized.

Copy link

@bochove bochove commented Nov 1, 2018

Is it also possible to catch everything for all domains?
Say I have some application connecting to localhost:25 te send e-mail and I want everything from all domains whatsoever to be delivered to this account.

Under no circumstance may any e-mail actually be deliverable to anything else.

Edit: Found out how to do that:
/etc/postfix/virtual should then contain:
/.*/

and the /etc/postfix/main.cf should instead have:
virtual_alias_maps = pcre:/etc/postfix/virtual

For this to work you should off course also do:
sudo apt-get install postfix-pcre

Now all e-mail to whatever domain you can think of will be delivered to this mailbox.

@aaronfranke

This comment has been minimized.

Copy link

@aaronfranke aaronfranke commented Jan 5, 2019

During postfix install process, the configure text dialog will display five options:

What if I aleady had it installed? How do I re-prompt the configuration?

This guide doesn't say how I can set up a system to get E-mail sent to an arbitrary E-mail address. I don't just want to read E-mails locally with Thunderbird. I want E-mails to be sent to e.g. a Gmail or Yahoo E-mail address.

@FiruzShoev

This comment has been minimized.

Copy link

@FiruzShoev FiruzShoev commented Jan 16, 2019

Really clear instructions. Thank you very much!

@raelgc

This comment has been minimized.

Copy link
Owner Author

@raelgc raelgc commented Jan 23, 2019

@aaronfranke:

This guide doesn't say how I can set up a system to get E-mail sent to an arbitrary E-mail address. I don't just want to read E-mails locally with Thunderbird. I want E-mails to be sent to e.g. a Gmail or Yahoo E-mail address.

This tutorial is named "Setup a Local Only Email Server". If you don't want a local only email server, it's not for you.

@XGamerYU

This comment has been minimized.

Copy link

@XGamerYU XGamerYU commented Jan 23, 2019

Hello there, thanks for the tutorial.
I have an issue... I setup everything as needed, port is 25... outgoing server is localhost , I can send the mail without troubles, to me, but whenever I try to get the mail, it says "Unable to locate mail spool file" - no matter what I do... is there anything I can do... thanks in advance!

EDIT:
I tried doing it on a virtual machine and it worked without any troubles. Thanks a lot for the guide, also, had to use port 25 on the outgoing server. Thanks.

@davidpustai

This comment has been minimized.

Copy link

@davidpustai davidpustai commented Mar 21, 2019

Thanks a lot! Finally found a useful article on this topic. :)

@AzirGar

This comment has been minimized.

Copy link

@AzirGar AzirGar commented Mar 21, 2019

I have a question, How do I configure the server so that it has Internet access, or can send to other types of domains such as gmail, outlook. etc

@chrispito

This comment has been minimized.

Copy link

@chrispito chrispito commented May 7, 2019

Thank's very much for this short tuto.

@lrusik

This comment has been minimized.

Copy link

@lrusik lrusik commented Aug 1, 2019

Clear and simple! Thanks a lot

@aqeel-ahmad-plc

This comment has been minimized.

Copy link

@aqeel-ahmad-plc aqeel-ahmad-plc commented Sep 19, 2019

Clearly documented, just follow it step by step and it works.

@wokidev

This comment has been minimized.

Copy link

@wokidev wokidev commented Jan 4, 2020

works flawlessly (ubuntu 19.10) Thank you! :)

@erickclasen

This comment has been minimized.

Copy link

@erickclasen erickclasen commented Mar 24, 2020

Works great. I really liked the instructions, very easy to set up. I was glad I found this as, I thought it might be tricky and with these instructions it was a few minutes on each machine.

I tried it on a desktop and a server.

Server
On the server, which only has a CLI, I wound up using mail instead of T-bird, installed via sudo apt-get install mailutils
It can be tested by sending a message to yourself by using...
mail -s "test" (your user name)@localhost
It works great to get the CRON messages on the machine.
Just type mail and you get a CLI email list. mail basics are, q to quit, m to compose, enter and spacebar to move through messages. Entering question mark ?, brings up command help.

Desktop Install
One gotcha that caught me is that I already had T-bird installed and therefore it had a default SMTP server already. For me this required what I would call step 6A to add a local STMP server.
6A. In the pane above "Account Actions" scroll, using the bar to the bottom "Outgoing Server (SMTP)".
Click Add
For description I wrote Local SMTP
Server Name: localhost
Port: 25
Username : (the user name)@localhost
Authentication Method: Password, transmitted insecurely
Connection Security: None

Then I went back into the account settings for the mail set up in step 6 and set the Outgoing Server (SMTP) to the Local SMTP

Also in /etc/hosts you can put in localhost.com as a alias and it works fine, like this...
127.0.0.1 localhost localhost.com

@teezzan

This comment has been minimized.

Copy link

@teezzan teezzan commented Apr 15, 2020

Awesome... Thanks... Port 25 is important..;)

@Uysim

This comment has been minimized.

Copy link

@Uysim Uysim commented Apr 30, 2020

Anyone manage to have it work on Mac. I got this error

An error occurred while sending mail. 
The mail server responded: 530 5.7.0 Must issue a STARTTLS command first. 
Please verify that your email address is correct in your account settings and try again.
@raelgc

This comment has been minimized.

Copy link
Owner Author

@raelgc raelgc commented May 1, 2020

@Uysim It appears that your server (or client) are using the secure port instead of port 25. Additionally, check the authentication settings.

@popovserhii

This comment has been minimized.

Copy link

@popovserhii popovserhii commented Jun 12, 2020

For testing you can also use next command:
echo "This is the body of the email" | mail -s "This is the subject line" your_email_address

After that, go to Thunderbird and click Get Messages

@ombak

This comment has been minimized.

Copy link

@ombak ombak commented Jul 23, 2020

I follow this tutorial in Debian 10, but when I try to use thunderbird I always get Unable to locate mail spool file.

This my /etc/host

127.0.0.1 localhost
127.0.0.1 localhost.com

My hostname:

localhost

This my /etc/postfix/main.cf

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = localhost.localdomain
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = example.com, $myhostname, localhost.localdomain, localhost.localdomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
default_transport = error
relay_transport = error
inet_protocols = all

Thanks

@rpopov

This comment has been minimized.

Copy link

@rpopov rpopov commented Nov 19, 2020

Thank you! Worked perfectly well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.