I'd like to make all of my partners in my company receive daily metrics like new users, pageviews everyday, I wrote a custom module using hook_cron.
This is my code:
function custom_metrics_cron() {
global $language;
$default = strtotime('-24 Hours', REQUEST_TIME);
$last_send = variable_get('custom_metrics_daily_last_send', $default);
$only_after = strtotime('+24 Hours', $last_send);
if (REQUEST_TIME >= $only_after) {
variable_set('custom_metrics_daily_last_send', REQUEST_TIME);
drupal_mail('custom_metrics', 'daily', MAIL_RECIEVERS, $language);
}
}
function custom_metrics_mail($key, &$message, $params) {
$date = date('m-d-Y', strtotime('-1 Day', time()));
$metrics = custom_metrics_get_metrics();
switch($key) {
case 'daily':
$message['subject'] = t('Daily Metrics: !date', array('!date' => $date));
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$message['body'][] = custom_metrics_get_metrics();
break;
}
}
It works okay, I get the mails everyday, but the problem is I get not only one mail, I already set the variable and make it send ONE mail every 24 hours but I still get 4~5 mails every 24 hours. Can anyone tell me where does I do wrong?