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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
 
// Set this to your timezone
date_default_timezone_set('America/New_York');
 
// Start at 8:00 AM (24-hour time)
$startTime = mktime(8, 0, 0);
 
// End at 5:00 PM (24-hour time)
$endTime = mktime(17, 0, 0);
 
 
$currentTime = time();
 
// Do not send the email if it is outside of the allowed hours
if($currentTime < $startTime || $currentTime > $endTime)
{
	print('Not sending an email after hours.');
	die();
}
 
// 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.');
	die();
}
 
// Info of person to receive the tests
define('TO_EMAIL',		'me@test.com');
define('TO_NAME',		'John Doe');
 
// Info of person sending the tests
define('FROM_EMAIL',	'webmaster@serversendingtests.com');
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 yourusername@youremailprovider.com if you would like to disable these automated tests.';
 
$result = mail(TO_NAME . ' <' . TO_EMAIL . '>', $subject, $message, 'From: ' . FROM_NAME . ' <' . FROM_EMAIL . '>');
var_dump($result);

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

*/15 	* 	* 	* 	* 	wget http://myserver.com/path/to/script.php

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

Share