How to disable account related e-mails in Drupal 7
Blog

How to disable account related e-mails in Drupal 7

Drupal 7 provides 8 basic email templates for registering, cancellation & password recovery. These settings we can find at ‘admin/config/people/accounts’ page.

account-seting-mail

In these templates, some of following templates has option to disable( checkbox above subject field ).

  • Account activation
  • Account blocked
  • Account canceled

But other templates by default enabled & those cannot be disabled through UI. So incase we don’t want these template then we can do it through programmatically by using hook_mail_alter(). 

In one of case we didn’t wanted to send default “Welcome (no approval required)” email template as we were using Rules to send custom email with some attachment. There is no option to send attachment in default email.

Now let’s look into the simple code how we can disable that. And later I will explain how we can disable for other default templates too. We have created module called no_emails.

/**
 * Implements hook_mail_alter().
 */
function no_emails_mail_alter(&$message) {
  if ($message['key'] == 'register_no_approval_required') {
    $message['send'] = FALSE;
  }
}

Here register_no_approval_required is the message key for ‘Welcome (no approval required)’ template, Following are other message keys in case if you want to disable them.

  • register_admin_created: Welcome message for user created by the admin.
  • register_no_approval_required: Welcome message when user self-registers.
  • register_pending_approval: Welcome message, user pending admin approval.
  • password_reset: Password recovery request.
  • status_activated: Account activated.
  • status_blocked: Account blocked.
  • cancel_confirm: Account cancellation request.
  • status_canceled: Account canceled.

Similarly we can disable Welcome (new user created by administrator) email template using below snippet.

/**
 * Implements hook_mail_alter().
 */
function no_emails_mail_alter(&$message) {
  if ($message['key'] == 'register_admin_created') {
    $message['send'] = FALSE;
  }
}

We have learned how can we disable Welcome (no approval required) template & Welcome (new user created by administrator) templates. You can use above mentioned message keys to disable other default email templates also.