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.
$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).
Leave a Reply