How to Test and Fix the php mail() Function

The php mail() function allows you to send emails directly from a php script.
This tutorial shows you how to test if php mail is working correctly.

Test php mail() on your Web Server

1. Create a php test file using a text editor and save it e.g. as test.php:

 

<?PHP
$sender = 'someone@somedomain.tld';
$recipient = 'you@yourdomain.tld';

$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;

if (mail($recipient, $subject, $message, $headers))
{
    echo "Message accepted";
}
else
{
    echo "Error: Message not accepted";
}
?>

 

2. Change the $sender and $recipient in the code.

3. Upload the php file to your webserver.

4. Open the uploaded php file in your browser to execute the php script.

5. The output show either "Message accepted" or "Error: Message not accepted".

chevron_right  Message accepted:

Open your mail client to see if the message was delivered to the specified email address.
(also check your spam folder!)

 

If the message was delivered:

  • Everything is fine - php mail is working.

 

If the message was not delivered:

  • Some provider don't allow external recipients when using php mail. Change the recipient ($recipient) in the code to a local recipient. This means use an email address from the server's domain, for example if your server domain is www.yourdomain.tld then the recipient's email should be someone@yourdomain.tld.
  • Upload the modified php file and retry.
  • If it's still not working: change the sender ($sender) to a local email (use the same email as used for recipient).
  • Upload the modified php file and retry.
  • Contact your provider if it still does not work.
    Tell your provider that the standard php "mail()" function returns TRUE, but not mail will be sent.
    It's recommended to include the used php test script to show your provider, that the problem is not caused by the php script used.

chevron_right  Error: Message not accepted

 

php mail might not be enabled:

  • Login to your webserver configuration and check the php settings.
    Some php installations require that you setup a default sender in the php configuration first.

 

Some provider don't allow external recipients when using php mail:

  • Change the recipient ($recipient) in the code to a local recipient. This means use an email address from the server's domain, for example if your server domain is www.yourdomain.tld then the recipient's email should be someone@yourdomain.tld.
  • Upload the modified php file and retry.
  • If it's still not working: change the sender ($sender) to a local email (use the same email as used for recipient).
  • Upload the modified php file and retry.

 

If you have checked the php settings, tried a local recipient and it's still not working:

  • Contact your provider if you are unable to enable php mail().
    Tell your provider that the standard php "mail()" function returns FALSE.
    It's recommended to include the used php test script to show your provider, that the problem is not caused by the php script used.
Disclaimer: The information on this page is provided "as is" without warranty of any kind. Further, Arclab Software OHG does not warrant, guarantee, or make any representations regarding the use, or the results of use, in terms of correctness, accuracy, reliability, currentness, or otherwise. See: License Agreement