Updated March 2017: fixed the code to display from gist snippet.
Here is a simple way of testing if your mail() function is working. I’ve used this script to test if the mail servers are properly working. From time to time I’m asked to migrate websites to new DNS’s. When I’m setting up the MX Records, I need to test to make sure the server sends / receives e-mails.
To test outgoing mail using PHP’s mail() function, simply create testmail.php and use this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$to = "test@email.com"; | |
$header = "From: {$to}"; | |
$subject = "Test Subject Here"; | |
$body = "Hi this is a test email to see if the mail() function is working properly on your site."; | |
if (mail($to, $subject, $body, $header)) { | |
echo ("<p>Message successfully sent!</p>"); | |
} else { | |
echo("<p>Message delivery failed…</p>"); | |
} | |
?> |
Very straight forward, set $to variable to your own e-mail. Edit the $subject and $body if needed. The if conditional will e-mail you if the mail() function exists! Otherwise, you get fail. 🙁