Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save surajitbasak109/ab3cbc181c362aaa3e67f406d25aacf8 to your computer and use it in GitHub Desktop.
Save surajitbasak109/ab3cbc181c362aaa3e67f406d25aacf8 to your computer and use it in GitHub Desktop.
Setup Local Mail Server using Postfix for Laravel

Setup Local Mail Server using Postfix for Laravel

You can follow these steps to create your own local mail server using postfix.

Before that we need to learn what is Postfix? Postfix is a mail transfer agent (MTA) that routes and delivers electronic mail. And Mail transfer agent is a software that transfers electronic mail from one computer to another computer using Simple Mail Transfer Protocol(SMTP).

Here we are going to use local mail for that and we will use sendmail driver for our laravel project.

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

Step 1: Select Local only Step 2: 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, john@localhost.com is unique account. But while testing systems, we can use any address like jane@localhost.com, foo@localhos.com etc, because all will be redirected to john@localhost.com.

Now follow the below steps:

  1. Create a new file if doesn't exists:
sudo <vim/nano/gedit/subl/code> /etc/postfix/virtual

Here I have provided different types of text editors. First two editors are terminal based and if you are unfamiliar with them you can use gedit or sublime text or Visual Studio Code, but you need to have those programs installed on your operating system. So, if you want to use vim you can follow the command below:

sudo vim /etc/postfix/virtual
  1. Add the following 2 lines content, replacing <your-user> with your Unix user account:
@localhost <your-user>
@localhost.com <your-user>
  1. Save and close the file.
  2. Configure postifx to read this file: a. Open /etc/postfix/main.cf:
    sudo vim /etc/postfix/main.cf
    b. And check if this line is enabled, or add it if not exists:
    virtual_alias_maps = hash:/etc/postfix/virtual
    
  3. Activate it using following:
sudo postmap /etc/postfix/virtual
  1. Reload postfix: sudo systemctl restart postfix
sudo systemctl restart postfix
#or
sudo service postifx restart

5 - Install Thunderbird

Now we need a Graphical User Interface(GUI) to send and recieve messages. For this we need Mozilla Thunderbird. It is free, open source, cross platform email client developed by Mozilla Foundation. If you don't have thunderbird in your linux machine you can install it using following command:

sudo apt install thunderbird

6 - Configure Thunderbird

Step 1: Skip the welcome screen (click in the button to use existing accounts); Step 2: Click in the Settings button at top right (similar to Chrome settings) then click on Preferences > Account Settings Step 3: Under Account Actions choose "Add Other Account" Step 4: Select "Unix Mailspool (Movemail)" Step 5: Your account will be @localhost (of course, replace with your user account). Don't use @(none), use @localhost Step 6: 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 jose@localhost Click on "Get Mail" and you'll see the message at Inbox.

8 - Change .env file to get email from laravel project

Open your .env file from the laravel proeject and add/replace below lines:

MAIL_DRIVER=sendmail
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=john@localhost
MAIL_FROM_NAME=John
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment