You are here

function mail_edit_mail_alter in Mail Editor 6

Same name and namespace in other branches
  1. 5 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 76
Main file for the Mail Editor module.

Code

function mail_edit_mail_alter(&$mail) {
  _mail_edit_include();

  // Find out if this mail id in our registry.
  $query = "SELECT * FROM {mail_edit_registry} where id = '%s'";
  $result = db_query($query, $mail['id']);
  $mail_data = db_fetch_object($result);
  if (!$mail_data) {

    // If we dont have registry record of mail id in question we shouldnt attempt to alter it.
    return;
  }

  // Load mail template if we have altered it.
  $query = "SELECT * FROM {mail_edit} WHERE id = '%s' AND language = '%s'";
  $arg[] = $mail['id'];
  $arg[] = $mail['language']->language;
  $template = db_fetch_object(db_query($query, $arg));

  // Do not try to alter anything if we didn't modify this mail template.
  if ($template) {

    // Fetch our token values.
    $tokens = module_invoke($mail_data->module, 'mail_edit_tokens_value', $mail_data->mailkey, $mail);
    uksort($tokens, '_mail_edit_sort_long_keys_first');
    $result = new stdClass();
    $result->tokens = array_keys($tokens);
    $result->values = array_values($tokens);

    // Perform replacement of tokens.
    $mail['subject'] = str_replace($result->tokens, $result->values, $template->subject);
    unset($mail['body']);
    $mail['body'][] = str_replace($result->tokens, $result->values, $template->body);
  }
}