Home / Alter Drupal Email Messages

Alter Drupal Email Messages

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">&lt;?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">(&amp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">) {<br />&nbsp; </span><span style="color: #FF8000">// For example, submit the contact form and you<br />&nbsp; // will be able to see the $message array<br /><br />&nbsp; </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 />&nbsp; </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">?&gt;</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">&lt;?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">(&amp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">) {<br />&nbsp; </span><span style="color: #FF8000">// Add a link to the user's profile in the message body<br />&nbsp; // when you submit the site wide contact form:<br /><br />&nbsp; </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 />&nbsp;&nbsp;&nbsp; case </span><span style="color: #DD0000">'contact_page_mail'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; global </span><span style="color: #0000BB">$user</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (</span><span style="color: #0000BB">$user</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">uid </span><span style="color: #007700">!= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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">-&gt;</span><span style="color: #0000BB">uid</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; break;<br />&nbsp; }<br /><br />}<br /></span><span style="color: #0000BB">?&gt;</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.