You are here

function birthdays_mail in Birthdays 6

Same name and namespace in other branches
  1. 7 birthdays.module \birthdays_mail()

Implementation of hook_mail().

File

./birthdays.mail.inc, line 12
This file contains the files necessary to e-mail the administrator his periodical listings and/or the users on their birthday.

Code

function birthdays_mail($key, &$message, $params) {
  global $_birthdays_field;
  $language = $message['language'];
  switch ($key) {

    // Send message to administrator.
    case 'admin_message':
      $message['subject'] = t('Upcoming Birthdays');
      switch ($params['period']) {
        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;
      }
      $message['body'][] = t('@period, the following users are having their birthday:', array(
        '@period' => $period,
      ), $language->language);

      // Build list of users
      foreach ($params['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['body'][] = ($params['period'] == BIRTHDAYS_ADMIN_MAIL_DAILY ? '' : _birthdays_show_date($account->{$_birthdays_field->name}, $account) . ': ') . $account->name . ' (' . $age . ')';
      }
      break;

    // Send message to user.
    case 'user_message':

      // Get the content for the placeholders.
      $variables = user_mail_tokens($params['account'], $language);
      $message['subject'] = _birthdays_mail_text('subject', $language, $variables);
      $message['body'] = _birthdays_mail_text('message', $language, $variables);
  }
}