• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

About bhavinhjoshi

Drupal hook: hook_mail_alter

Printer-friendly versionSend to a DeveloperPDF version
3
0

When sending e-mails or newsletters from your website, you might want to inject some information like website name, disclaimer, copy right information, unsubscribe link and such. Just like hook_form_alter, you can use hook_mail_alter to virtually alter anything in the mail just before Drupal sends the e-mail. You can even change the mime type of the email, for example, from plain text to HTML and vice versa.
Here is the syntax/signature of the hook

  • Drupal 5: hook_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers)
  • Drupal 6, 7: hook_mail_alter(&$message)

$message has the following key-value pairs:

  • 'id': Unique id of the mail being sent.
  • 'to': The destination e-mail address(es)
  • 'subject': The subject of the e-mail. This should not contain any new line characters
  • 'body': This is an array which contains the actual message to be sent
  • 'from': The e-mail address of the sender. This can be either a custom address or site's email address
  • 'headers': This is an array containing e-mail's MIME type, Content-type etc.
  • 'params': Params are the parameters which is supplied by the function which calls the drupal_mail(), which builds the message prior to hook_mail_alter is invoked.
  • 'language': The language used to build the message prior to hook_mail_alter is invoked

<?php
/**
* Implementation of hook_mail_alter
*
* @param $message
* @author Drupal Developer
*/
function mymodule_mail_alter(&$message) {
    if (
$message['id'] == 'contact_page_mail') {
       
       
$sn__nid = $_POST['link'];
       
$message['body'][1] = "<a href='". url("node/". $sn__nid, array('absolute' => TRUE))
          .
"'>". drupal_get_path_alias("node/". $sn__nid) ."</a>\r\n". $message['body'][1];
       
$message['headers']['Content-Type'] = 'text/html;charset=UTF-8; format=flowed; delsp=yes';
    }
}
?>

Syndicate content

Sitestats

Recent comments

rss-top-right.png
toolbar powered by www.iconcy.com