Drupal hook: hook_mail_alter
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';
}
}
?>- 1348 reads
-
- Send feedback



Search
Sitestats
Recent comments
- Create a separate .js file
6 days 18 hours ago - Thanks for your reply
6 days 18 hours ago - Jquery and nice menus
6 days 20 hours ago - Image Menu
1 week 3 days ago - Image menu
1 week 4 days ago - Its cool to have a status!
33 weeks 6 days ago


