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

Comments

quicksketch’s picture

As 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.

quicksketch’s picture

Project: Webform » Mime Mail
Version: 6.x-3.4 » 6.x-1.0-alpha6
Priority: Major » Normal

I'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.

sgabe’s picture

I 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?

// Send mail
if (module_exists('mimemail')) {
  // If mimemail module is installed ALL emails are sent via this module.
  // drupal_mail() builds the content of the email but does NOT send. 
  $message = drupal_mail();
  $message['result'] = mimemail();
}
else {
  $message = drupal_mail();
} 
quicksketch’s picture

@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?

sgabe’s picture

@quicksketch: drupal_mail() has a $send parameter which indicates whether to or not to send the message.

$message = drupal_mail(..., FALSE); // Last parameter $send is FALSE.
quicksketch’s picture

Project: Mime Mail » Webform
Version: 6.x-1.0-alpha6 » 6.x-3.4

Ah, 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. :)

quicksketch’s picture

Status: Active » Fixed
StatusFileSize
new1.09 KB

I'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().

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bès’s picture

Version: 6.x-3.4 » 6.x-3.9
Status: Closed (fixed) » Active
StatusFileSize
new880 bytes

Hello,

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.

quicksketch’s picture

Status: Active » Needs review

Thanks 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.

quicksketch’s picture

Status: Needs review » Fixed

Committed @Bés's #9 which will be included in 3.10.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.