$mail->isBlacklistEmail() method

Is given email address in the blacklist?

  • Returns boolean false if not blacklisted, true if it is.
  • Uses $config->wireMail['blacklist'] array unless given another blacklist array in $options.
  • Always independently verify that your blacklist rules are working before assuming they do.
  • Specify true for the why option if you want to return the matching rule when email is in blacklist.
  • Specify true for the throw option if you want a WireException thrown when email is blacklisted.

Available since version 3.0.129.

Example

// Define blacklist in /site/config.php
$config->wireMail('blacklist', [
  'email@domain.com', // blacklist this email address
  '@host.domain.com', // blacklist all emails ending with @host.domain.com
  '@domain.com', // blacklist all emails ending with @domain.com
  'domain.com', // blacklist any email address ending with domain.com (would include mydomain.com too).
  '.domain.com', // blacklist any email address at any host off domain.com (domain.com, my.domain.com, but NOT mydomain.com).
  '/something/', // blacklist any email containing "something". PCRE regex assumed when "/" is used as opening/closing delimiter.
  '/.+@really\.bad\.com$/', // another example of using a PCRE regular expression (blocks all "@really.bad.com").
]);

// Test if email in blacklist
$email = 'somebody@domain.com';
$result = $mail->isBlacklistEmail($email, [ 'why' => true ]);
if($result === false) {
  echo "<p>Email address is not blacklisted</p>";
} else {
  echo "<p>Email is blacklisted by rule: $result</p>";
}

Usage

// basic usage
$bool = $mail->isBlacklistEmail(string $email);

// usage with all arguments
$bool = $mail->isBlacklistEmail(string $email, array $options = []);

Arguments

NameType(s)Description
emailstring

Email to check

options (optional)array
  • blacklist (array): Use this blacklist rather than $config->emailBlacklist (default=[])
  • throw (bool): Throw WireException if email is blacklisted? (default=false)
  • why (bool): Return string containing matching rule when email is blacklisted? (default=false)

Return value

bool string

Returns true if email is blacklisted, false if not. Returns string if why option specified + email blacklisted.

Exceptions

Method can throw exceptions on error:

  • WireException - if given a blacklist that is not an array, or if requested to via throw option.


Hooking $mail->isBlacklistEmail(…)

You can add your own hook events that are executed either before or after the $mail->isBlacklistEmail(…) method is executed. Examples of both are included below. A good place for hook code such as this is in your /site/ready.php file.

Hooking before

The 'before' hooks are called immediately before each $mail->isBlacklistEmail(…) method call is executed. This type of hook is especially useful for modifying arguments before they are sent to the method.

$this->addHookBefore('WireMailTools::isBlacklistEmail', function(HookEvent $event) {
  // Get the object the event occurred on, if needed
  $mail = $event->object;

  // Get values of arguments sent to hook (and optionally modify them)
  $email = $event->arguments(0);
  $options = $event->arguments(1);

  /* Your code here, perhaps modifying arguments */

  // Populate back arguments (if you have modified them)
  $event->arguments(0, $email);
  $event->arguments(1, $options);
});

Hooking after

The 'after' hooks are called immediately after each $mail->isBlacklistEmail(…) method call is executed. This type of hook is especially useful for modifying the value that was returned by the method call.

$this->addHookAfter('WireMailTools::isBlacklistEmail', function(HookEvent $event) {
  // Get the object the event occurred on, if needed
  $mail = $event->object;

  // An 'after' hook can retrieve and/or modify the return value
  $return = $event->return;

  // Get values of arguments sent to hook (if needed)
  $email = $event->arguments(0);
  $options = $event->arguments(1);

  /* Your code here, perhaps modifying the return value */

  // Populate back return value, if you have modified it
  $event->return = $return;
});

$mail methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #549
    In the 549th issue of ProcessWire Weekly we’re going to check out the latest core updates, highlight one older yet still very relevant third party module, and more. Read on!
    Weekly.pw / 17 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“…building with ProcessWire was a breeze, I really love all the flexibility the system provides. I can’t imagine using any other CMS in the future.” —Thomas Aull