You are here

function regcode_mailer_mail_action in Registration codes 7

Same name and namespace in other branches
  1. 6.2 regcode_mailer/regcode_mailer.module \regcode_mailer_mail_action()
  2. 7.2 regcode_mailer/regcode_mailer.module \regcode_mailer_mail_action()

Implements the mail action.

File

regcode_mailer/regcode_mailer.module, line 109
Main code functionality and hooks of regcode_mailer module.

Code

function regcode_mailer_mail_action(&$object, &$context = array()) {
  static $i = 0;

  // Check we have enough emails for this action.
  if (!isset($context['emails'][$i])) {
    drupal_set_message(t('Ran out of emails, unable to send code (%code).', array(
      '%code' => $object->code,
    )));
    return FALSE;
  }

  // Params for the email.
  $params = array(
    'regcode' => $object,
    'message' => $context['message'],
    'subject' => $context['subject'],
  );

  // Send.
  $email = $context['emails'][$i];
  drupal_mail('regcode_mailer', 'regcode', $email, language_default(), $params);
  drupal_set_message(t('Sent message with code %code to %email', array(
    '%code' => $object->code,
    '%email' => $email,
  )));

  // Update the database.
  db_insert('regcode_mailer')
    ->fields(array(
    'rid' => $object->rid,
    'mailed' => REQUEST_TIME,
    'recipient' => $email,
  ))
    ->execute();

  // Tag the code.
  foreach ($context['terms'] as $term_id) {
    $record = array(
      'rid' => $object->rid,
      'tid' => $term_id,
    );
    db_merge('regcode_term')
      ->key($record)
      ->fields($record)
      ->execute();
  }
  $i++;
}