Cancel email in hook_mail In hook_mail() I might discover that some part of the email couldn't be generated (maybe when trying to load a variable from an external system it fails).

How can I cancel the sending? I don't want to send an email with an error message in it.

Example code:

function mymodule_mail($key, &amp;$message, $params) {
  switch ($key) {
    case 'some_email':
      $foo = Something::fetchSomething($params['bar']);
      if (!$foo) {
        // I don't want to send this email any more
      }
      else {
        $message['body'][] = t('Important information about @foo.' ['@foo' =&gt; $foo]);
      }
      break;
  }
}


Does that make sense? Or should all parameters be loaded and processed before hook_mail() and in hook_mail() you assume to always have those params available?
You can do the access check from hook_mail_alter instead of hook_mail (let it prepare the default message based on parameters, and alter it later) by something like:

function HOOK_mail_alter(&amp;$message) {
  // can add additional check for particular mails here,
  // by checking the value in $message['id']
  if (TRUE) {
    $message['send'] = FALSE;
  }
}


Note: You should clear the cache in order to make the changes effective.