You are here

function mail_edit_mail_alter in Mail Editor 5

Same name and namespace in other branches
  1. 6 mail_edit.module \mail_edit_mail_alter()
  2. 7 mail_edit.module \mail_edit_mail_alter()

Implementation of hook_mail_alter().

File

./mail_edit.module, line 20

Code

function mail_edit_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers) {
  global $user, $base_url;
  static $stored_variables, $alters;
  if (!isset($stored_variables)) {
    if (!empty($GLOBALS['form_values'])) {
      foreach ($GLOBALS['form_values'] as $key => $value) {
        $stored_variables["!{$key}"] = $value;
      }
    }
    $stored_variables = array(
      '!site' => variable_get('site_name', 'Drupal'),
      '!uri' => $base_url,
      '!uri_brief' => substr($base_url, strlen('http://')),
      '!date' => format_date(time()),
      '!login_uri' => url('user', NULL, NULL, TRUE),
      '!mailkey' => $mailkey,
    );
  }
  if (!isset($alters[$mailkey])) {
    $sql = "SELECT * FROM {mail_edit} m";
    if (module_exists('pm_subscriptions')) {
      $sql .= ' LEFT JOIN {pm_subscriptions_mail_edit} p ON m.mailkey = p.mailkey';
    }
    $sql .= " WHERE m.mailkey = '%s'";
    $alters[$mailkey] = db_fetch_object(db_query($sql, $mailkey));
  }
  if (!empty($alters[$mailkey])) {
    $alter = $alters[$mailkey];
    $sender = $user;
    $recipient = user_load(array(
      'mail' => $to,
    ));
    $is_privatemsg = !empty($alter->type) && $recipient;
    $variables = $stored_variables;
    $variables['!site'] = t(variable_get('site_name', 'drupal'));
    $variables['!sender_name'] = $sender->name;
    $variables['!sender_page'] = url("user/{$sender->uid}", NULL, NULL, TRUE);
    $variables['!sender_contact_page'] = empty($sender->contact) ? '(disabled)' : url("user/{$sender->uid}/contact", NULL, NULL, TRUE);
    if ($recipient) {
      $variables['!recipient_name'] = $recipient->name;
      $variables['!recipient_page'] = url("user/{$recipient->uid}", NULL, NULL, TRUE);
      $variables['!edit_uri'] = url('user/' . $account->uid . '/edit', NULL, NULL, TRUE);
      if (substr($mailkey, 0, 5) == 'user-') {
        $variables['!username'] = $recipient->name;
        $variables['!login_url'] = user_pass_reset_url($recipient);
        $variables['!mailto'] = $recipient->mail;
      }
    }
    foreach (module_implements('mail_edit_variables') as $module) {
      $function = $module . '_mail_edit_variables';
      $function($variables, $mailkey, $sender, $recipient);
    }
    if ($alter->subject) {
      subscriptions_template_preprocess($alter->subject, $variables);
      $subject = strtr($alter->subject, $variables);
    }
    if ($alter->body) {
      subscriptions_template_preprocess($alter->body, $variables);
      $body = strtr($alter->body, $variables);
    }
    if ($is_privatemsg) {
      $to = NULL;
      module_invoke('privatemsg', 'send_privatemsg', $recipient, $subject, $body, FILTER_FORMAT_DEFAULT, 0, $alter->type, $variables);
    }
  }
}