$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.184

Twitter updates

  • ProcessWire 3.0.185 (dev) core updates, plus new Session Allow module— More
    17 September 2021
  • Three new ProcessWire Textformatter modules: Find/Replace, Markdown in Markup, and Emoji— More
    3 September 2021
  • This week we have a new master version released after a year in the making. With nearly 40 pull requests, hundreds of new additions and more than 100 issue reports resolved, this new version has a ton of great new stuff— More
    27 August 2021

Latest news

  • ProcessWire Weekly #384
    In the 384th issue of ProcessWire Weekly we'll cover the latest core updates, introduce a new module called Session Allow, and highlight a new site of the week. Read on!
    Weekly.pw / 18 September 2021
  • ProcessWire 3.0.184 new master/main version
    This week we have a new master/main version released after a full year in the making. As you might imagine, this new version has a ton of great new stuff and we’ll try to cover much of it here.
    Blog / 27 August 2021
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits