Instantly share code, notes, and snippets.

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

Setup a Local Only 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.

Show comment
Hide comment
@dipakpatilsymc

dipakpatilsymc 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.

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.

Show comment
Hide comment
@dangerouse

dangerouse Apr 27, 2016

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

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.

Show comment
Hide comment
@Feyisayo

Feyisayo Jul 30, 2016

I followed these instructions and it worked like a charm

Thanks a lot

Feyisayo commented Jul 30, 2016

I followed these instructions and it worked like a charm

Thanks a lot

@bpcrao

This comment has been minimized.

Show comment
Hide comment
@bpcrao

bpcrao Nov 6, 2016

Helpful, thanks a lot saved my time.

bpcrao commented Nov 6, 2016

Helpful, thanks a lot saved my time.

@jonathanbossenger

This comment has been minimized.

Show comment
Hide comment
@jonathanbossenger

jonathanbossenger Feb 11, 2017

Brilliant stuff, saved me hours of Googling. Thanks

jonathanbossenger commented Feb 11, 2017

Brilliant stuff, saved me hours of Googling. Thanks

@roopezero

This comment has been minimized.

Show comment
Hide comment
@roopezero

roopezero 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

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.

Show comment
Hide comment
@chriskerley78910

chriskerley78910 Mar 13, 2017

it works!, thanks man.

chriskerley78910 commented Mar 13, 2017

it works!, thanks man.

@wilensky

This comment has been minimized.

Show comment
Hide comment
@wilensky

wilensky commented Mar 22, 2017

Seal of apporval

@raelgc

This comment has been minimized.

Show comment
Hide comment
@raelgc

raelgc 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 :)

Owner

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.

Show comment
Hide comment
@heartbeathero

heartbeathero May 4, 2017

Is "ssudo service postfix reload" correct?

heartbeathero commented May 4, 2017

Is "ssudo service postfix reload" correct?

@dayze

This comment has been minimized.

Show comment
Hide comment
@dayze

dayze Jun 8, 2017

Thanks, save me a lot of time ;)

dayze commented Jun 8, 2017

Thanks, save me a lot of time ;)

@burrscurr

This comment has been minimized.

Show comment
Hide comment
@burrscurr

burrscurr 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 :)

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.

Show comment
Hide comment
@JonhZono

JonhZono 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?

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.

Show comment
Hide comment
@ChathurikaA

ChathurikaA 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.

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.

Show comment
Hide comment
@superjoefly

superjoefly Aug 5, 2017

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

superjoefly commented Aug 5, 2017

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

@minitauros

This comment has been minimized.

Show comment
Hide comment
@minitauros

minitauros 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.

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.

Show comment
Hide comment
@pogster

pogster Nov 8, 2017

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

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.

Show comment
Hide comment
@MegzKay

MegzKay 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

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.

Show comment
Hide comment
@lykre

lykre 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.

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.

Show comment
Hide comment
@Azimboy

Azimboy Jan 30, 2018

Great article! Many thanks!

Azimboy commented Jan 30, 2018

Great article! Many thanks!

@shahnazmshariff

This comment has been minimized.

Show comment
Hide comment
@shahnazmshariff

shahnazmshariff 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?

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.

Show comment
Hide comment
@qoomon

qoomon Feb 19, 2018

just use localhost.org

qoomon commented Feb 19, 2018

just use localhost.org

@zuhairkareem

This comment has been minimized.

Show comment
Hide comment
@zuhairkareem

zuhairkareem 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.

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.

@light2yellow

This comment has been minimized.

Show comment
Hide comment
@light2yellow

light2yellow 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.

light2yellow 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.

Show comment
Hide comment
@mjrulesamrat

mjrulesamrat Aug 3, 2018

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

mjrulesamrat commented Aug 3, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment