6078 comments
2357 subscribers
6202 on Twitter
Subscribe! Feed reader E-mail

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…

Short URL: http://sachachua.com/blog/p/4979
  • http://coldacid.net Chris Charabaruk

    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.

  • http://themegarden.org Drupal Themes Garden

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

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

    • http://coldacid.net Chris Charabaruk

      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');
      }

  • http://www.philipnet.com Phil L.

    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.

  • http://www.philipnet.com Phil L.

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

  • http://poemas.ec Beto

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

  • Anil

    Hi guys,

    I am trying to override the default hook_mail behavior in contact_mail since the message doesn’t send me the visitor’s email address.

    I’ve got the new contact_mail function written but I don’t really know how to override the default contact_mail function and have my function called instead.

    Maybe I need to override the form… not sure.

    Newbie here.

    Thanks,
    -Anil

  • http://sachachua.com Sacha Chua

    You could cheat and just edit contact.module directly. =)

  • http://sachachua.com Sacha Chua

    Actually, you could do a hook_mail_alter in your custom module. When you see a message with the mail ID contact-user-mail (or whatever it is; I’m looking at Drupal 5′s contact module), use the global $user variable and just tack the e-mail address from that $user onto the message. Hope that helps!

On This Day...

  • 2012: Learning from the basics — Last Saturday J- was running late, so W- offered to drive her to karate class. She asked if she could [...]
  • 2011: Rhetoric and the Manila Zoo; reflections on conversations and a request for insight — UPDATE: So I decided to face fire with love. =) Check out Friends of Manila Zoo and share your stories! One [...]
  • 2010: Giving myself permission to delegate again — SCHEDULED: 2010-07-23 Fri 08:00 I don’t like spending money. Sometimes it’s hard for me to give myself permission [...]
  • 2009: How I got my job at IBM — Reposting this ancient internal blog post because I keep forgetting where it is and I find that it’s still useful [...]
  • 2007: Am I really almost done? — 2 years. 73 pages. 16,394 words. Am I really almost done? I’ve spent the last two years of my life working [...]
  • 2005: Where should mailing list replies go? — John Billings writes: I’d also like to change it so that when you hit reply the messge goes to the list and [...]
  • 2005: Waaah! No Internet in the dorm yet! — Well, that sucks. Apparently, the reminder I got in my e-mail is a generic reminder sent to everyone at Graduate House, [...]
  • 2004: More stuff about just-in-time information retrieval — IBM Article - http://www.research.ibm.com/journal/sj/393/part2/rhodes.html (quick primer) Rhodes’ Thesis – www.bradleyrhodes.com/Papers/rhodes-phd-JITIR.pdf (has some nice theory and background) Emacs Plugin created from above – http://remem.org/ Autonomy’s [...]
  • 2003: Kernel 2.6.0 notes — Don’t forget: - make a /sys folder, enter this in fstab: sysfs /sys [...]
  • 2003: Mail fix — Okay, hmm. I’m having a hard time figuring out how to properly route my mail. I have two main mail accounts – [...]
  • 2002: Borging out — wearables — Richi and my mom started bugging me about my journal. Here it is! The CE guys came through with [...]

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging