Thursday, 9 July 2015

How to create html mail template for Drupal?

In Drupal 7 by default we can't send html mail, since Drupal will convert all emails to plain text before sending. This could be overridden using Drupal 7htmlmail contributed module. We may also need i>mailsystem module for implementing htmlmail, which is a dependency. The module also give html mail template, which could be customized as well in same way of theme templates.
Firstly, we can look on the configuration details and then theming of html template mail. If there is a need to make the html template for a particular mail with module name and with a particular mail key then we have to create a new settings in 'admin/config/system/mailsystem' namely module_name and key_name respectively. After saving the new settings we have to set 'HTMLMAILSystem' for the new 'Mail System Settings'. Next in htmlmail configuration 'admin/config/system/htmlmail'
  • step-1: List of available templates and descriptions.
  • step-2: Email theme, where the above custom template will hold.
  • step-3: Post-filtering, text format used for filtering the mail.
Find out the template file which meets your need and copy paste them to your theme template and specify it in above config page.
Now theme the copy of mail template as you wish. That's all and you have completed with your config and theming of html mail template.
Using the help of Drupal functions like drupal_mail we can send mail using the 'htmlmail' module template.
We need to specify the module name and key according to template file we use.
Let us assume, if we use 'htmlmail--module_name--key_name.tpl.php' template then drupal_mail call be as below to use this template for the same.

  $module = 'module_name';
  $key = 'key_name';
  $to = $user->mail;
  $language = language_default();
  $name = $user->name;
  $params = array();
  $from = '';
  drupal_mail($module, $key, $to, $language, $params, $from);
Also it will be a good practice to add hook_mail(), if we call drupal_mail with in our module.
Hope this article about html template mail for Drupal 7 helped you.

No comments:

Post a Comment