Alter Drupal Email Messages
Posted on Tue, Mar 24, 2009 by Kevin Hankens
I just had a requirement to add some text to the body of Drupal's outgoing emails. Turns out it's not too tough using hook_mail_alter(). I started out by using var_export() and drupal_set_message() to find out more information about the array that Drupal creates for the message:
<span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Implementation of hook_mail_alter().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">your_module_mail_alter</span><span style="color: #007700">(&</span><span style="color: #0000BB">$message</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// For example, submit the contact form and you<br /> // will be able to see the $message array<br /><br /> </span><span style="color: #0000BB">$v </span><span style="color: #007700">= </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">drupal_set_message</span><span style="color: #007700">(</span><span style="color: #DD0000">"message: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$v</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span></span>The email text is constructed from an array of elements, so adding a new element to the email body is just a matter of adding an element to the body array:
<span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Implementation of hook_mail_alter().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">your_module_mail_alter</span><span style="color: #007700">(&</span><span style="color: #0000BB">$message</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// Add a link to the user's profile in the message body<br /> // when you submit the site wide contact form:<br /><br /> </span><span style="color: #007700">switch (</span><span style="color: #0000BB">$message</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">]) {<br /> case </span><span style="color: #DD0000">'contact_page_mail'</span><span style="color: #007700">:<br /> global </span><span style="color: #0000BB">$user</span><span style="color: #007700">;<br /> if (</span><span style="color: #0000BB">$user</span><span style="color: #007700">-></span><span style="color: #0000BB">uid </span><span style="color: #007700">!= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$message</span><span style="color: #007700">[</span><span style="color: #DD0000">'body'</span><span style="color: #007700">][] = </span><span style="color: #DD0000">'Profile: [your-site-address]/user/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$user</span><span style="color: #007700">-></span><span style="color: #0000BB">uid</span><span style="color: #007700">;<br /> }<br /> break;<br /> }<br /><br />}<br /></span><span style="color: #0000BB">?></span></span>You could just as easily use this for a variety of other cases. For example, you could add conditional recipients, do custom logging, or even use the message content as a conditional trigger. Lots of good stuff there.


