A mail template is a similar concept to a mail merge document. The mail is written using placeholders for information that changes between each specific instance of the mail. The mail template is stored then when a mail is to be sent values are substituted for the placeholders.

Mail templating in Drupal is currently (as of Drupal 5) implemented by storing a subject and body in the database somewhere (typically in the variable table). Editing the templates is done with code similar to that below:

$default_subject = 'My default subject at !site';
$default_body = "Dear !user,\n\nThis is the default body for the mail.\n\n!site team";

$form['my_mail_subject'] = array(
  '#type' => 'textfield',
  '#default_value' => variable_get('my_mail_subject', $default_subject), ...
);

$form['mail_mail_body'] = array(
  '#type' => 'textarea',
  '#default_value' => variable_get('mail_mail_body', $default_body), ...
);

In the new implementation this becomes much simpler. The form interface is provided by centralised code. You call one function and tell some information about the mail such as the mail type (e.g user welcome mail or shipping notification), title and description of the form's select element and the variable (in the variable table) holding the mail ID.

define('MAIL_VARNAME_MY_MAIL', 'my_mail_variable');
...
$form[MAIL_VARNAME_MY_MAIL] = mail_selection_for_variable('form id', 'my mail', t('Template for my mail'), MAIL_VARNAME_MY_MAIL, t('This mail is for sending information to site members about special offers'));

Sending mail in the current implementation goes something like:

$default_subject = 'My default subject at !site';
$default_body = "Dear !user,\n\nThis is the default body for the mail.\n\n!site team";
$variables = my_mail_load_variables(...);
$subject = t(variable_get('my_mail_subject', $default_subject), $variables);
$body = t(variable_get('mail_mail_body', $default_body), $variables);

$mail_success = drupal_mail('my-mail', $address, $subject, $body, $from);

With the new templating system this block becomes:

$mail_success = mail_send_variable(MAIL_VARNAME_MY_MAIL, $address, my_mail_load_variables(...));

As you can see it's going to make things simpler for module implementors to add mail sending code to their modules.

My battle plan for Drupal 6 is to integrate the above changes and include support for multiple languages.

Comments

firfin’s picture

Why is this functionality deprecated? This usually means it is replaced by something later and greater.
Can someone tell where to find this now?