Sending HTML and Plain text EMails with Drupal 6

JoshuaKissoon's picture

This is a simple tutorial that shows you how to use hook_mail and drupal_mail to send both HTML and Plain text mails in drupal. We are gonna build a module to demonstrate sending these mails; how this module works is that it sends out mails everytime cron is ran, you can configure when you want to send out mails depending on your application.

hook_cron is implemented first to call drupal_mail everytime cron is ran. Within hook_cron we are calling drupal_mail twice to send 2 sets of mails, note that each mail has a different key, and would therefore be handled differently in hook_mail. One mail key is for HTML mails, the other for Plain text Mails.
Everytime drupal_mail is used, hook_mail is called to setup the mail template to be sent, and allow drupal to configure the mail. Based on the key passed to hook_mail, the necessary action is taken whether to send an HTML Mail or Plain text mail.

The first thing we need to do is to setup our .infofile: samplemail.info

name = Sample Email
description = Example module of how to send emails using hook_mail. Also includes how to send HTML emails
core = 6.x
package = CleverLogic
version = 1.0

The next thing is our .module file: samplemail.module

<?php
 
/*
   * Implementation of hook_cron()
   */
 
function samplemail_cron()
  {
     
/*
       * This function is called everytime cron is ran
       * In this function we will be calling drupal_mail to send a mail
       */
      /*
       * Drupal mail is the function used to send mail
       * @parameters
       * $module - A module name to invoke hook_mail() on. The {$module}_mail() hook will be called to complete the
       *            $message structure which will already contain common defaults, in our case, this is samplemail_mail.
       * $key - A key to identify the e-mail sent. This key will be used in hook_mail to identify which mail call is it.
       *        In samplemail_mail above, you may notice we have the keys in a switch statement.
       * drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE)
       */
     
$params = array(
       
'subject' => t('Client Requests Quote'),
       
'body' => t("Body of the email goes here"),
      );
     
/* If you notice, we use different keys for the admin and other users, so in hook_mail, we can adjust the admin emails a bit if needed */
     
drupal_mail("samplemail", "samplemail_normal_mail", "admin@mysite.com", language_default(), $params, "admin@mysite.com");
     
drupal_mail("samplemail", "samplemail_html_mail", "user1@mysite.com, user2@mysite.com", language_default(), $params, "admin@mysite.com");
  }
 
/*
   * Implementation of hook_mail()
   */

  function samplemail_mail($key, &$message, $params)
  {
     
/*
       * This function is used to hook into drupal_mail function to send the mail
       * Everytime drupal_mail is used, this function is called to setup the mail to be sent
       */
     
$language = $message['language'];
      switch (
$key)
      {
         
/*
           * Here we will be switching the key values, therefore different mails can be sent differently using different keys.
           * As demonstrated above, we have different keys for the admin and other users to send normal mail to admins, and html mails to users
           */
         
case 'samplemail_normal_mail':
             
/* Emails with this key will be normal emails, so we just set body and subject accordingly */
             
$message['subject'] = t($params['subject'], $var, $language->language);
             
//the email body is here, inside the $message array
             
$message['body'][] = $params['body'];
              break;
          case
'samplemail_html_mail':
             
/*
               * Emails with this key will be HTML emails,
               * we therefore cannot use drupal default headers, but set our own headers
               */
              /*
               * $vars required even if not used to get $language in there since t takes in: t($string, $args = array(), $langcode = NULL)
               */
             
$message['subject'] = t($params['subject'], $var, $language->language);
             
/* the email body is here, inside the $message array */
             
$body = "<html><body>
                  <h2>HTML Email Sample with Drupal</h2>
                  <hr /><br /><br />
                  {$params['body']}
                  </body></html>"
;
             
$message['body'][] = $body;
             
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
              break;
      }
  }
?>

Attached is a copy of the module. And that would send your either Plain text emails or HTML emails based on the key passed into the drupal_mail functions. Please tell me what you think about this tutorial and feel free to ask questions in the comments section below. I am also thinking about a tutorial with mail attachments and one for sending mail in drupal 7 if anyone is interested?

References: 
http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/t/7
http://api.drupal.org/api/drupal/includes--mail.inc/function/drupal_mail/7