Mail edit + HTML mail + token

function modulename_mailkeys() {
    return array(
      'modulename_category_mail' => t('email alert'),
    );
  return NULL;
}
 
function modulename_mail_edit_text($mailkey, $language) {
  global $base_url;
  $message = array();
  switch ($mailkey) {
    case 'modulename_category_mail':
      $message['subject'] = 'A node is created [node:title]';
      $message['body'] = 'node link:[node:url], your selected category : [modulename:maincat]';
    break;
  }
  return $message;
}
 
function modulename_mail_edit_token_types($mailkey) {
  return array('modulename', 'node');
}
 
function modulename_token_info() {
 $type = array(
    'name' => t('modulename'),
    'description' => t('Tokens from modulename module.'),
  );
  return array(
    'types' => array('modulename' => $type),
    'tokens' => array('modulename' => $obj),
  );
}
 
function modulename_token_list($type) {
  $tokens = array();
  if ($type == 'modulename') {
    $tokens['maincat'] = array(
      'maincat' => t('Main category'),
    );
  }
}
 
function modulename_tokens($type, $tokens, $data = array(), options = array()) {
  $values = array();
  foreach ($tokens as $tokens_key => $tokens_val) {
    if ($type == 'modulename') {
      switch ($tokens_key) {
        case "maincat":
          $values[$tokens_val] = $data['modulename']['maincat'];
        break;
      }
    }
  }
  return $values;
}
 
function modulename_sendmail() {
  $maincat = 'cat1';
  $subcat = 'cat2';
  $module = 'modulename';
  $key = 'modulename_category_mail';
  $language = language_default();
  $params['modulename'] = array('maincat' => $maincat);
  $from = NULL;
  $send = TRUE;
  $email = variable_get('site_mail', ''); //send to
  drupal_mail($module, $key, $email, $language, $params, $from, $send);
}
 
/**
 * Send HTML Mail
 * You need *mailsystem, *HTML Mail module enabled.
 * 1. goto admin/config/system/mailsystem
 * 2. Add new setting, select modulename, input "Key" as "modulename_category_mail"
 * 3. After save, "modulename_category_mail" selects HTMLMailSystem
 */
Help Share this Article