Use hook_cron to send daily mail but I get 4~5 the same mail every day? 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 &gt;= $only_after) {
    variable_set('custom_metrics_daily_last_send', REQUEST_TIME);
    drupal_mail('custom_metrics', 'daily', MAIL_RECIEVERS, $language);
  }
}

function custom_metrics_mail($key, &amp;$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' =&gt; $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?
D7, I struggled a lot, this is what i did, works perfect, following modules are required: Job shcheduler, Rules, and optionally Rules switch user, i use core cron; make a custom module 'MYMODULE' and place this code:

   /**
   * Implements hook_cron_job_scheduler_info().
   *
   * Do not forget to run job_scheduler_rebuild_all() on any changes here,
   * see https://drupal.org/comment/8406919#comment-8406919
   */
  // * Implements hook_cron()
  function MYMODULE_cron_job_scheduler_info() {
    $schedulers = array();
  //add as many scheduled jobs as needed.
  //$schedulers['daily_email_6_am'] = array(...);
    $schedulers['daily_email_7_am'] = array(
      'worker callback' =&gt; 'send_emails_action_7_am',
      'jobs' =&gt; array(
        array(
          'type' =&gt; 'notification',
          'id' =&gt; 1,
          'crontab' =&gt; '0 7 * * 1-6', // 7am, mon-sat
          //'crontab' =&gt; '0 * * * *', // once a day, midnigth
          //'crontab' =&gt; '*/1 * * * *', // once every minute, dev settings
          'periodic' =&gt; TRUE
        ),
      )
    );
    return $schedulers;
  }
  function send_emails_action_7_am() {
    // Do stuff.
    rules_invoke_component('rules_send_emails_component_7_am');
}


now you have a cron based scheduled job at 7 am from monday to saturday that executes a rule-component machine name: 'send_emails_component_7_am', be carefull, function calls: rules_MACHINENAME of component,  built rule component to send your emails, i  usually i built my emails with views rules module and have a great control to built email-body-value. see this answer, and this one also could be many emails, benefits of using views-rules as you can loop by views all users and filter them as needed and inject to rules whatever you imagine. emails are not 'duplicated', only 1 mail per user.

cron run as anonymous user, so, only if you have some content_access restrictions your emails won't be sent, here comes the benefit of 'Rules switch user', do pay attention with this module settings as this img suggest, i hope this helps someone.