PHP Mail Not Working Not Sending (How To Fix It!)

After spending some time setting 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 a mail delivery server (SMTP) to send out emails, and there are 2 possible solutions:

  1. Install a local SMTP server.
    • Windows – Use Papercut for local testing.
    • Linux – Use sendmail or postfix, sudo apt-get install postfix.
  2. 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!

 

 

TABLE OF CONTENTS

 

INSTALLING A LOCAL SMTP SERVER

All right, let us get started with the first solution – Installing a mail server on your own machine.

 

WINDOWS USERS – PAPERCUT SMTP FOR LOCAL TESTING

For Windows users, 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 – YUM or RPM, just use whichever is correct.

 

 

UPDATE THE PHP.INI FILE

php.ini
[mail function]
SMTP=localhost
smtp_port=25
; For Win32 only.
sendmail_from = doge@codeboxx.com

Finally in the php.ini file, simply ensure that SMTP is pointing to localhost. Also for the Windows users, set sendmail_from or you will get a “bad message return path” error message.

 

OTHER MAIL SERVERS – FOR PRODUCTION SERVERS

Need to set up an “actual mail server”, and not a “test mail server”? Here are a few to consider:

 

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 an existing SMTP server, just update the php.ini file and point to the SMTP server accordingly. For example, we can actually point to the Gmail SMTP server:

php.ini
[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…

 

DONE!

Finally, just copy that email/password into php.ini.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras that may be useful to you.

 

PHP MAIL DEBUGGING

Are the emails still not sent out? It’s time to get some eyes and clues on where the problem is. To do debugging on PHP mail:

  1. In your PHP script, check the error message after sending out the email – if (!mail(TO, SUBJECT, MESSAGE)) { print_r(error_get_last()); }
  2. Set mail.log = FOLDER/mail.log in php.ini.
  3. Also, set a mail log on the SMTP server itself.

That’s all. Do a test mail send and trace the log files – Did PHP send out the email? Did the SMTP server send out the email? Are the configurations correct? Lastly, also very important – Did you send it to the correct email address, is it even a valid email?

 

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 the Windows firewall.
  • Linux – Allow an exception in iptables.
  • Elsewhere – Maybe a hardware firewall that is somewhere in the network, or an anti-virus stopping the SMTP send.

 

THE END

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!

Leave a Comment

Your email address will not be published. Required fields are marked *