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.
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
Fedora/CentOS/RHEL:
sudo yum install postfix
Ubuntu:
sudo apt-get install postfix
MacOSX: Postfix is already installed by default.
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.
Enabling this, you can use any email address ending with "@localhost" or "@localhost.com". Example: here, [email protected] is unique account. But while testing systems, we can use any address like [email protected], [email protected] etc, because all will be redirected to [email protected].
Now follow the below steps:
- 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
- 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:
a. Open
/etc/postfix/main.cf
:b. And check if this line is enabled, or add it if not exists:sudo vim /etc/postfix/main.cf
virtual_alias_maps = hash:/etc/postfix/virtual
- Activate it using following:
sudo postmap /etc/postfix/virtual
- Reload postfix: sudo systemctl restart postfix
sudo systemctl restart postfix
#or
sudo service postifx restart
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
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.
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.
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