You are here

function _birthdays_send_admin_message in Birthdays 5

Same name and namespace in other branches
  1. 6 birthdays.mail.inc \_birthdays_send_admin_message()

Sends e-mail to administrator once a day as reminder about the upcoming 7 days

@todo Add frequency options (once a day, every week, monthly)

1 call to _birthdays_send_admin_message()
birthdays_cron in ./birthdays.module
Implementation of hook_cron().

File

./birthdays.module, line 1191
The Birthdays module allows users to add their birthday to their profile. It lists birthdays on a seperate page and in different blocks. Users can receive an e-mail on their birthday automatically, and the administrator can receive daily reminders of…

Code

function _birthdays_send_admin_message($days = 1) {
  global $_birthdays_field;

  // Only proceed when admin messages are enabled
  if ($frequency = variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED)) {

    // Select period string.
    switch ($frequency) {
      case BIRTHDAYS_ADMIN_MAIL_DAILY:
        $period = t('Today');
        break;
      case BIRTHDAYS_ADMIN_MAIL_WEEKLY:
        $period = t('This week');
        break;
      case BIRTHDAYS_ADMIN_MAIL_MONTHLY:
        $period = t('This month');
        break;
    }

    // Get birthdays
    $accounts = birthdays_get_birthdays_by_days($days);

    // Set message
    $message = t("@period, the following users are having their birthday:\n", array(
      '@period' => $period,
    ));

    // Build list of users
    foreach ($accounts as $uid) {
      $account = user_load(array(
        'uid' => $uid,
      ));

      // Don't show the year
      $account->{$_birthdays_field->name}['year'] = NULL;

      // Calculate the age to be.
      $age = $account->age + !($account->{$_birthdays_field->name}['day'] == format_date(time(), 'custom', 'j') && $account->{$_birthdays_field->name}['month'] == format_date(time(), 'custom', 'n'));

      // Set the message. In the case of daily messages, don't add the date.
      $message .= "\n" . ($frequency == BIRTHDAYS_ADMIN_MAIL_DAILY ? '' : _birthdays_show_date($account->{$_birthdays_field->name}, $account) . ': ') . $account->name . ' (' . $age . ')';
    }

    // If there were any users:
    if (count($accounts) > 0) {

      // Get site e-mail to send reminder to and from
      $from = variable_get('site_mail', ini_get('sendmail_from'));
      $to = $from;
      $subject = t("Upcoming Birthdays");

      // Send the mail
      drupal_mail('birthdays_admin_mail', $to, $subject, $message, $from);

      // Log action
      watchdog('Birthdays', t('Sent birthday overview e-mail to admin.'), WATCHDOG_NOTICE, ' ');
    }
  }
}