Drupal: Adding a footer to all of your system e-mail

Drupal's hook system is making me almost as happy as Emacs' hook system does. =)

There's a hook_mail_alter function that allows you to modify any message your system sends. Example:

function mymodule_mail_alter($mailkey, &$to, &$subject, &$body, &$from, &$headers) {
   $body .= "\n\n" . t('This is a system-generated email, please do not reply to this message');
}

You gotta love developers who plan for extensibility and put all sorts of hooks into the code…

Save to - del.icio.us - Digg it - reddit - StumbleUpon

RSS feed | Trackback URI

6 Comments »

Yeah, the hook system is pretty amazing, and it's wild to see how much functionality has been added through hooks alone.

By the way, did you ever meet or talk to my friend Rob Loach? I swear, he lives to write Drupal modules to integrate into every single other website out there.

 

Seems that you are really impressed with the drupal's hook system :-)

BTW,
is your example just for Drupal 5, or maybe Drupal 6?

hook_mail_alter has existed since Drupal 5, but has different arguments in Drupal 6 and later. The example that Sacha posted is for Drupal 5.

With Drupal 6, rather than passing in a bunch of parameters, a single structured array is passed in. Here's the example from above, modified for Drupal 6:


function mymodule_mail_alter(&$message) {
$message['body'] .= "\n\n" . t('This is a system-generated email, please do not reply to this message');
}

 
 

This is for Drupal 6 and beyond.

I just want to warn users that implementing this will alter all emails sent from the server.

Not necessarily a problem, unless you're running Listhandler or another module that does email out posts/comments/forum posts to mailing lists (etc).

Cheers.

Phil L.

 

;) You can scrub my "Drupal 6 and beyond" comment then ;).

 

Could I cancel an outgoing email with this function??? How? Thanks :)

 
Name (required)
E-mail (required - never shown publicly)
Website / Blog address:
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line=""> in your comment.

Subscribe without commenting

`

On This Day...