Home/ApacheHostingPHP/PHP mail() not working?

PHP mail() not working?

** If you are using any dedicated server, cloud instance (like AWS, Linode, Digital Ocean Droplet, etc.), and PHP mail() function or wp_mail() isn’t working for you? If yes, then this resource can help you **

First, we need to understand how php mail() works.

PHP mail function invokes any mail server to send the email.
On *nix it invokes the sendmail binary, which then uses the mail configuration to route the email. On Windows, it sends to an SMTP server. In both cases, the sysadmin sets up the mail system.

We will talk about Linux here. Although Sendmail isn’t that reliable for production mails but for development servers it is the best and quickest choice.

So if PHP mail() isn’t working on your server then possibly sendmail or other SMTP isn’t configured behind the scene. So to make the mail function work, we need to fix what is responsible for sending emails.

Install Sendmail on Debian/Centos

  • Follow this guide: https://lukepeters.me/blog/getting-the-php-mail-function-to-work-on-ubuntu
    In any case it didn’t install on ubuntu: Stack Overflow Answer
    Another issue we might face is dpkg not working:
    “dpkg frontend is locked by another process”
    this is because the initial install command didn’t finish successfully. Now we need to kill the process which is using dpkg and occupied it.
    Check this or manually list all processes to find out the id:
    ps -aux
    sudo ps -a
    The Kill it using: kill -9 PID
  • Then follow first step again to install sendmail.

** Gmail, Hotmail etc. don’t accept emails if no proper authentication is used. So:
1. ADD SPF record to your DNS for your sever’s IP address.

2. DKIM Authentication.

There are several articles out there for setting up DKIM for sendmail but only this one worked for me.

Configure DKIM with Sendmail on Debian

Install and Configure DKIM with sendmail on Debian

PDF in case the reference website is down: Download

** Read the comments in case of any errors.

If this is done correctly then php mail function will start sending emails. (maybe in the spam folder)

Leave a Reply

We'll try to resolve your queries asap.

Leave a Reply

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

Recent Posts

2