I am able to block emails successfully, but whenever an email is blocked Drupal displays an error message that says "Unable to send e-mail. Please contact the site admin, if the problem persists."

I believe this is because you are setting $message['to'] to NULL on line 129 (hook_mail_alter). While this method does block the email, it seems to basically confuse Drupal into thinking something went wrong.

I have tried blocking emails with custom code in the past (using the NULL method that you use, as well as others), but I never found a way to do so without that error message showing up. One solution could be to manually remove the error message, described in this post.

Regardless of how you plan to prevent it, I do think that the error message should not show up.

Comments

john.karahalis’s picture

Typo correction: "I do not think that the error message should show up."

Anonymous’s picture

Thank you John.

I've added the following code to the 6.x-1.x module. But I'm unable to test it with my current setup. Can someone please test this code. Just insert it after $message['to'] = NULL;

// Remove warning about Drupal being unable to send mail.
$warnings = drupal_get_messages('warning');
foreach ($warnings['warning'] as $warning) {
    if ($warning != 'Unable to send e-mail') {
        drupal_set_message($warning, 'warning');
    }
}
truyenle’s picture

No, it is still there since the error doesn't generate immediately after assigning $message['to'] = NULL but when drupal system send email. So clear error message here doesn't really supress the message.

My other simple solution is to assign it to a fake non-exist email address like
$message['to'] = "drupal-fake-email@drupal.org";

It works for me.