After spending some time to set up your web server and writing up the scripts, the PHP mail function is not sending emails out as expected. Tutorials from all over the Internet show different solutions, and just what the heck is happening!? How do we fix it?
PHP mail requires an SMTP server to send emails out, and there are 2 solutions:
- Install a local SMTP server.
- Windows – Use a free email server such as hMail, then configure it to accept SMTP forwarding.
- Windows – Alternatively, use Papercut for local testing.
- Linux – Use sendmail or postfix,
sudo apt-get install postfix.
- Use a remote SMTP server, simply point the SMTP settings in the php.ini file to the mail server.
That is the gist of it, but let us go through the actual steps on fixing the mail problem – Read on!
REAL QUICK GUIDE
TABLE OF CONTENTS
|Local SMTP Server||Remote SMTP Server||Useful Bits|
INSTALLING A LOCAL SMTP SERVER
All right, let us get started with the first solution – Installing a mail server on your own machine.
XAMPP USERS – ENABLE SMTP RELAY IN MERCURY
WINDOWS USERS – HMAIL SERVER
But otherwise, there are plenty of free email servers online, one that I often use is called hMailServer. Simply download, install the server, and we need to do some settings with it:
Under settings > protocols > enable SMTP. You can also switch on POP3 and/or IMAP if you intend to receive emails.
Under advanced > IP Ranges > My computer > Enable SMTP and uncheck all “require SMTP authentication”.
P.S. For security purposes, I will recommend to keep “external to local” and “external to external” checked… But for a local test server, go ahead and leave them all unchecked.
WINDOWS USERS – PAPERCUT SMTP
Alternatively, try out Papercut SMTP. Papercut is probably the fastest and fuss-free SMTP server for testing – While it can be configured to relay email out, I don’t really recommend using this for a production server…
LINUX USERS – POSTFIX OR SENDMAIL
For you guys who are on Linux, simply install Sendmail or Postfix –
sudo apt-get install postfix
But different flavors of Linux has a different package manager –
RPM, just use whichever is correct.
UPDATE THE PHP.INI FILE
[mail function] SMTP=localhost smtp_port=25 ; For Win32 only. sendmail_from = email@example.com
Finally in the php.ini file, simply ensure that SMTP is pointing to
USING A REMOTE SMTP SERVER
Don’t want to install anything? Then use an existing SMTP server that you have access to.
POINT PHP.INI TO THE SMTP SERVER
To use existing SMTP server, just update the
php.ini file and point the SMTP server accordingly. For example, we can actually point to the Gmail SMTP server:
[mail function] SMTP=smtp.gmail.com smtp_port=587 auth_username=YOUR-ID@gmail.com auth_password=YOUR-PASSWORD
That’s it, but I will not recommend using your personal Gmail, Yahoo, or Outlook accounts on production servers… At least use one of their business accounts.
EXTRA – GMAIL AUTHENTICATION
Is Google rejecting the SMTP requests? Authentication failure? That is because Google will simply not allow any Tom, Dick, and Harry to access your email account. Thankfully, we only need to do some security setting stuff to get past this issue.
ENABLE 2-STEP AUTHENTICATION
Firstly, enable the 2-step authentication on your Google account if you have not already done so. That is basically, sending a PIN code to your mobile phone when Google detects login from an unknown device.
CREATE AN APP PASSWORD
But of course, we are not going to answer a PIN code challenge whenever we try to send an email from the server… So what we are going to do instead, is to create an app password.
Select “Other (Custom Name)” under the drop-down menu.
You can name it whatever you want…
Finally, just copy that email/password into
That’s all for the main tutorial, and here is a small section on some extras that may be useful to you.
TROUBLESHOOTING – TURN ON MAIL LOG
Emails still not sending out? Time to get some eyes and clues on where the problem is. In the
php.ini file, set a destination to generate the mail log.
mail.log = path/to/mail.log
That’s all. Do a test mail send, then check the error logs.
TONE DOWN FIREWALLS, ANTI-VIRUS, CHECK SPAM FOLDER
Is a firewall or anti-virus blocking the SMTP request?
- Windows – Check and allow an exception in Windows firewall.
- Linux – Allow an exception in
- Elsewhere – Maybe a hardware firewall that is somewhere in the network, or anti-virus stopping the SMTP send.
INFOGRAPHIC CHEAT SHEET
Thank you for reading, and we have come to the end of this guide. I hope that it has helped to solve your email problems, and if you have anything to share with this guide, please feel free to comment below. Good luck and happy coding!