If the mimemail module is enabled, webform uses the mimemail() function to send messages. However, this bypasses any possibilities of hook_mail_alter firing, which is pretty important if you're relying on it to redirect emails on a test domain instead of them going to clients.
The recommended usage (at least from what I understood via this mimemail issue - http://drupal.org/node/448996#comment-2002518) is to use drupal_mail as that should call mimemail anyway.
A workaround I'm currently using at the moment is to have a separate module with a hook_form_alter() function and the following piece of code to redirect emails to a test address:
if (module_exists('mimemail') && strpos($form_id, 'webform_client_form') !== FALSE) {
$webform_redirect = variable_get('webform_mail_redirect', NULL);
if (is_array($webform_redirect) && $webform_redirect['enabled']) {
if (!valid_email_address($webform_redirect['email'])) {
drupal_set_message('Invalid webform email redirect set, please check your settings.php file.', 'error');
return;
}
foreach ($form['#node']->webform['emails'] as $eid => $email) {
$old_email = $email['email'];
$form['#node']->webform['emails'][$eid]['email'] = $webform_redirect['email'];
$form['#node']->webform['emails'][$eid]['subject'] .= ' (REDIRECTED)';
$form['#node']->webform['emails'][$eid]['template'] = "<p>This email was REDIRECTED. Originally to " . $old_email . ".</p>\n" . $email['template'];
}
}
}
This using the following two config options set in settings.php:
$conf['webform_mail_redirect']['enabled'] = TRUE;
$conf['webform_mail_redirect']['email'] = 'testemail@mylocaldomain.local';
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | webform.submissions.inc_987782_9.patch | 880 bytes | bès |
| #7 | webform_mimemail_alter.patch | 1.09 KB | quicksketch |
Comments
Comment #1
quicksketchAs far as I know, drupal_mail() doesn't support attachments or HTML-ready e-mails. Using drupal_mail() would only be benificial if we wanted MIME Mail to use the system-wide setting for converting plain e-mails into HTML-ones, but since Webform needs manual control over whether the e-mail is plain text, HTML, and the attachments, I don't think using drupal_mail() is an option for us.
As noted in that same issue, perhaps a hook_mime_mail_alter() is necessary here? If we can use drupal_mail() that'd be great, but I'm not sure how to do that.
Comment #2
quicksketchI'm moving this to the MIME Mail queue as it's not an issue caused by Webform but a deficiency in the MIME Mail API.
Comment #3
sgabe commentedI can confirm this issue. I got an email from a fellow Drupalist who encountered this problem too. The referenced issue is the reason exactly why mimemail() doesn't invoke hook_mail_alter().
@quicksketch: Please consider to use drupal_mail() to prepare the $message before calling mimemail(), like Simplenews does. Is there any reason why this could not work for Webform?
Comment #4
quicksketch@sgabe: I'm all-for properly sending the e-mail like other modules, but your example doesn't make sense to me. How can you call drupal_mail() and not actually have it send the e-mail? Could you provide a patch?
Comment #5
sgabe commented@quicksketch: drupal_mail() has a $send parameter which indicates whether to or not to send the message.
Comment #6
quicksketchAh, okay I wasn't aware of that parameter, back to Webform we go...
A patch would still be great here, though I see where you're coming from now. :)
Comment #7
quicksketchI've committed this patch based on sgabe's suggestions. It should remedy this problem and run things through drupal_mail() (and the alter hooks) before calling mimemail().
Comment #9
bès commentedHello,
the #7 patch still have problem for me.
The modification made during hook_mail_alter (called by drupal_mail) are not sent to mimemail.
We need to use the returned $message to provide mimemail() arguments.
Comment #10
quicksketchThanks Bes, I'll take a look next time I'm working on this. IMO this probably should have been a separate issue rather than re-opening this one since we've already made a release containing this change.
Comment #11
quicksketchCommitted @Bés's #9 which will be included in 3.10.