PHP check if mail() function is enabled on your server

To check if mail function is enabled on your apache server you can try one of the following:

Check your php.ini like this:

<?php
   phpinfo();
?>

You should search for this in the list sendmail_path that has the default value /usr/sbin/sendmail -t -i

You can also try to manual set it to this value by changing the php.ini file. To do this go to /etc/php5/apache2/php.ini and uncomment the sendmail_path line like this

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "/usr/sbin/sendmail -t -i"

If it did not work for you, try to find out if the function exists in the first place.

<?php
if ( function_exists( 'mail' ) )
{
    echo 'mail() is available';
}
else
{
    echo 'mail() has been disabled';
}
?>

And finally you can test what the php mail() function returns.

<?php
    /*
     * Enable error reporting
     */
    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );

    /*
     * Setup email addresses and change it to your own
     */
    $from = "your_email@yourdomain.com";
    $to = "send_to@testdomain.com";
    $subject = "Simple test for mail function";
    $message = "This is a test to check if php mail function sends out the email";
    $headers = "From:" . $from;

    /*
     * Test php mail function to see if it returns "true" or "false"
     * Remember that if mail returns true does not guarantee
     * that you will also receive the email
     */
    if(mail($to,$subject,$message, $headers))
    {
    	echo "Test email send.";
    } 
    else 
    {
    	echo "Failed to send.";
    }
?>

But more important than this is if the email arrives to your inbox (or Spam folder).

I have also find myself in need to check the mail function a WordPress platform. You can do this on WordPress by installing Check Email plugin. Although it has not been updated lately it still works and it does the job that it was installed for.

check mail

Like I said, a positive result does not mean that the email will be received. There are much more things to check here.

Hope this helped you. If not please let me know how you finally solve it.

 

Leave a Reply

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