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
Name | Type(s) | Description |
---|---|---|
email | string | Email to check |
options (optional) | array |
|
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 viathrow
option.
Hooking $mail->isBlacklistEmail(…)
You can add your own hook events that are executed either before or after the $mail
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
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
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;
});
API reference based on ProcessWire core version 3.0.236