Testing mail() On Apache

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:


<?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>");
}
?>

view raw

gistfile1.txt

hosted with ❤ by GitHub

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. 🙁

Leave a Reply

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

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax