PHP script to send test emails on a schedule to make sure you are able to receive email

Ever wondered if your email was actually working? I’ve had numerous people over the past few years wonder the same thing. Ideally you would use a consistent, dependable email service provider (think Gmail, Hotmail, Yahoo!, etc.). But sometimes that isn’t an option. If you think you might be receiving email intermittently or not at all, feel free to use this script to send test emails to yourself on a regular basis. If you get the test emails, you know at least someone can get to you.

	print('Not sending an email after hours.');

// Get the current day of the week as an index (0=Sunday, 6=Saturday)
$dayOfWeek = date('w');

// Do not send the email on weekends
if($dayOfWeek == 0 || $dayOfWeek == 6)
	print('Not sending an email on the weekends.');

// Info of person to receive the tests
define('TO_EMAIL',		'');
define('TO_NAME',		'John Doe');

// Info of person sending the tests
define('FROM_EMAIL',	'');
define('FROM_NAME',	'Email Tester');

// Example: 8:00 am on 1 Nov 2010
$subject = 'Test: ' . date('g:i a \o\n j M Y');

$message = 'This email was automatically generated. Please send an email to if you would like to disable these automated tests.';

$result = mail(TO_NAME . ' <' . TO_EMAIL . '>', $subject, $message, 'From: ' . FROM_NAME . ' <' . FROM_EMAIL . '>');

And finally, setup a cron job that runs on a regular basis (every 15 minutes in this case):

*/15 	* 	* 	* 	* 	wget

I realize that you could do some of the scheduling being done in the PHP script in the cron job definition, but I wanted to have more control over that for future features (think pulling schedules from a database for a multi-user environment).



Leave a Reply

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