Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/Mail/MailHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

namespace Yard\ConfigExpander\Mail;

use Illuminate\Support\Facades\Log;

class MailHandler
{
/**
* @param array<string, mixed> $args
*
* @return array<string, mixed>
*/
public static function filter(array $args): array
{
$allowedEnvironments = apply_filters('wp_mail_allowed_environments', ['dev', 'prod', 'development', 'production']);
if (! defined('WP_ENV') || in_array(WP_ENV, $allowedEnvironments)) {
return $args;
}

$config = resolve('config');
$whitelistedEmails = $config->get('email.whitelisted_emails', []);
if (! is_array($whitelistedEmails)) {
$whitelistedEmails = [];
}
$whitelistedDomains = $config->get('email.whitelisted_domains', ['yard.nl']);
if (! is_array($whitelistedDomains)) {
$whitelistedDomains = [];
}

if (
(! is_array($whitelistedEmails) || empty($whitelistedEmails)) &&
(! is_array($whitelistedDomains) || empty($whitelistedDomains))
) {
Log::warning('Email not sent due to missing whitelisted recipients.');
$args['to'] = [];
$args['cc'] = [];
$args['bcc'] = [];

return $args;
}

$fields = ['to', 'cc', 'bcc'];
$original = [];
$nonWhitelisted = [];
$notWhitelisted = false;

foreach ($fields as $field) {
$original[$field] = (array)($args[$field] ?? []);
if ([] === $original[$field]) {
continue;
}
$emails = $original[$field];
$filteredEmails = array_values(array_filter($emails, function ($email) use ($whitelistedEmails, $whitelistedDomains): bool {
if (in_array($email, $whitelistedEmails, true)) {
return true;
}

foreach ($whitelistedDomains as $domain) {
if (str_ends_with(strtolower((string)$email), '@' . strtolower((string)$domain))) {

Check failure on line 62 in src/Mail/MailHandler.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Cannot cast mixed to string.
return true;
}
}

return false;
}));

if (count($filteredEmails) !== count($emails)) {
$notWhitelisted = true;
}

$nonWhitelisted[$field] = array_diff($emails, $filteredEmails);
$args[$field] = $filteredEmails;
}

if ($notWhitelisted) {
add_filter('gettext', function (string $translated, string $original, string $domain): string {
if ('default' === $domain && '<strong>Error:</strong> The email could not be sent. Your site may not be correctly configured to send emails. <a href="%s">Get support for resetting your password</a>.' === $original) {
$translated = '<strong>Error:</strong> Email not added to whitelist.';
}

return $translated;
}, 10, 3);

Log::debug(
sprintf(
"Email not sent due to non-whitelisted recipients:\nTo: %s\nCc: %s\nBcc: %s",
implode(', ', $nonWhitelisted['to']),
implode(', ', $nonWhitelisted['cc']),
implode(', ', $nonWhitelisted['bcc'])
)
);

Log::debug(
sprintf(
"Email:\nHeaders: %s\nFrom: %s\nTo: %s\nCc: %s\nBcc: %s\nSubject: %s\nMessage: %s",
implode(', ', (array)($args['headers'] ?? [])),
$args['from'] ?? '',

Check failure on line 100 in src/Mail/MailHandler.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #3 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
implode(', ', $original['to']),
implode(', ', $original['cc']),
implode(', ', $original['bcc']),
$args['subject'] ?? '',

Check failure on line 104 in src/Mail/MailHandler.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #7 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
$args['message'] ?? '',

Check failure on line 105 in src/Mail/MailHandler.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #8 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
)
);
}

return $args;
}

/**
* @param array<string, mixed> $args
*
* @return array<string, mixed>
*/
public static function prefixSubject(array $args): array
{
$allowedEnvironments = apply_filters('wp_mail_allowed_environments', ['dev', 'prod', 'development', 'production']);
if (! defined('WP_ENV') || in_array(WP_ENV, $allowedEnvironments)) {
return $args;
}

$args['subject'] = '[Test mail]: ' . $args['subject'];

return $args;
}
}
10 changes: 10 additions & 0 deletions src/Mail/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ public function boot(): void
$phpmailer->Hostname = preg_replace('/^www\./', '', $hostName);
}
});

/**
* Filter wp_mail to restrict email sending in staging environment.
Comment thread
hnccox-yard marked this conversation as resolved.
*/
add_filter('wp_mail', [MailHandler::class, 'filter']);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worden de IDE's ook blij van: add_filter('wp_mail', MailHandler::filter(...));


/**
* Prefix email subject with [Test mail]: in non-allowed environments.
*/
add_filter('wp_mail', [MailHandler::class, 'prefixSubject']);
}
}
Loading