I need to stop sending my webform submission email programmatically and conditionally. By conditionally I mean I have two contact forms and two locales on my Drupal application. I have a custom configuration in the admin end based upon which I will be allowing/disallowing the webform submission emails only for the contact form and locale I choose at the backend configuration.

I have seen an answer at How to stop webform from sending an email that point me to do this in hook_webform_submission_insert but my preference is do do it via a handler. I'm not able to comment on that answer as i don't have enough stackoverflow reputation.

I know in Drupal 7 we were able to do it by simply setting the flag $message['send'] = false; Is there a way to in a similar way in Drupal 10? This might be simple but I could not find a way around this. Please help.

1 Answer 1


You can use hook_webform_handler_invoke_alter() to Conditionally enable or disable a handler:

function Mymodulename_webform_handler_invoke_alter(\Drupal\webform\Plugin\WebformHandlerInterface $handler, $method_name, array &$args) {
  $webform = $handler->getWebform();
  $webform_id = $handler->getWebform()->id();
  $handler_id = $handler->getHandlerId();
  if($webform_id == 'webform_machine_name' && $handler_id == 'handler_machine_name'){
     // Add logic here to disable webform handler programatically
      $operation = 'disable';
      // call disable() function
      // Save the webform.

Note: Make sure clear cache & check email handler list of webform whose email handler you want to disable , you 'll see email handler 'll disble for eg: /admin/structure/webform/manage/contact/handlers

For more documentation you can check here

  • Thanks a lot for this, this is something I did not know. But what I actually wanted to know is - How to prevent webform from sending an email programmatically based on a conditional logic.
    – irohit786
    Nov 8, 2023 at 6:08
  • Did you try this above mentioned hook_webform_handler_invoke_alter by adding your custom logic? Nov 8, 2023 at 6:16
  • I do not want to disable a handler, but rather I want to disable the email sent by two variants of contact us webforms based on a logic in code.
    – irohit786
    Nov 9, 2023 at 5:27

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.