Skip to content

Instantly share code, notes, and snippets.

@quicksketch
Last active February 28, 2016 08:22
Show Gist options
  • Save quicksketch/11124800 to your computer and use it in GitHub Desktop.
Save quicksketch/11124800 to your computer and use it in GitHub Desktop.
Alter Webform outgoing e-mail
<?php
/**
* Implements hook_mail_alter().
*/
function mymodule_mail_alter(&$message) {
if ($message['id'] == 'webform_submission') {
// Use the From header as a Reply-to and set the From header to the site
// mail address. Trying to say e-mail is "From" other domains will get the
// mail marked as spam, so instead the e-mail is always from webform.com,
// but the reply address is the user-specified address.
$message['headers']['Reply-to'] = $message['from'];
// This first header is apparently not used, update for consistency.
$message['headers']['From'] = $message['headers']['Sender'];
// The From address actually used in the sent mail.
$message['from'] = $message['headers']['Sender'];
// Users prefer to have their own (as set in the "Reply-to" header) show up
// in the "From" address, even if the address is going to be @webform.com.
// Pull out the name from the Reply-to header and use it on From.
$replyto_parts = array();
$from_parts = array();
$email_regex = '/^"(.+?)"[ ]+<(.*?)>$/';
// We can only update the "From" name if a name is actually given.
if (preg_match($email_regex, $message['headers']['Reply-to'], $replyto_parts)) {
// Update "From" addresses that are "Name" <[email protected]>
if (preg_match($email_regex, $message['from'], $from_parts)) {
$message['from'] = $replyto_parts[1] . ' <' . $from_parts[2] . '>';
}
// Update "From" addresses that are just the e-mail (more common).
elseif (valid_email_address($message['from'])) {
$message['from'] = $replyto_parts[1] . ' <' . $message['from'] . '>';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment