4

I'm using Drupal 7.12 / Webform 3

How do I stop email from being sent upon a webform submission? I want to do this conditionally so I need to use a hook. I still want everything else to go through correctly (i.e. confirmation page, db entry for submission).

I see that there are these lines in function webform_client_form_submit($form, &$form_state)

if (!$is_draft && !$form_state['values']['details']['finished']) {
    $submission = webform_get_submission($node->webform['nid'], $sid, TRUE);
    webform_submission_send_mail($node, $submission);
  }

How do I hook into that and stop it?

6 Answers 6

8

If you go the end of the function webform_submission_send_mail() inside the file webform.submissions.inc (inside weform module's include directory), you'll notice that the webform module using the function drupal_mail() to send the mail.

So a better place to change the behavior of the mail from being sent (alter in Drupal terms) is hook_mail_alter(), which is used to alter an email message created by using drupal_mail() function.

Implementation of the hook to stop the e-mails:

function MODULE_mail_alter(&$message) {
  //You can do your conditional check here and depending on that
  //you can set the value of $message['send'] to false to stop the mail from being sent. 
  //place the line below inside conditional loop, or else it'll stop every mail from being sent ;-)
  $message['send'] = FALSE; 
}

Note: This hook will only work if the mail is generated by drupal_mail(). If you're sending mail by some other menthod, it'll not be invoked. But, by default Webform module uses drupal_mail() as mentioned earlier, so there's nothing to worry :-)
You'll also need to clear the caches to make the change effective.

1

You can also use webform with Rules and Webform Rules. Are your conditions something complicated that Rules cannot implement?

This module adds rules integration on webforms.

0

You should just remove all recipients on here:

yoursite.com/node/XXX/webform/emails

when there are no recipients no emails will be sent.

3
  • Yeah. I only want to stop the emails under certain conditions which I will calculate in my custom module. So I still need email addresses in there. Mar 20, 2012 at 21:03
  • 4
    Ok, then you should write your own submit function, in fact you can copy and paste the code of webform_client_form_submit and just modify it to your needs. And then in your iimplementation of hook_form_alter for the webform you should replace default submit function with yours. Mar 20, 2012 at 21:12
  • @Alex Petrov I upvoted your comment, parhaps you should update your answer with this solution
    – Mike
    Sep 30, 2012 at 22:52
0

you even do not need override webform_client_form_submit function, because 'webform_client_form_submit' function works with $form['#node']->webform['emails'] array, which you can modify in form_alter hook. (unset your email for some specific condition). I've tested it on webform 4v, maybe for 3v object can have other structure, but i think it also have to work

0

I had to do this in hook_webform_submission_insert. I just looped through the emails and changed their status. In my case I wanted to enable them so set them to 1.

   foreach ($node->webform['emails'] as $key => $email) {
    $node->webform['emails'][$key]['status'] = 1 // or 0 to disable;
  }
0

I used the same approach as Cameron, by using the insert hook. Setting the status for each email did not work for me however.

I managed to get it to work by unsetting the emails I did not want to go out.

foreach ($node->webform['emails'] as $key => $email) {
  unset($node->webform['emails'][$key]);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.