Skip to content

Instantly share code, notes, and snippets.

@stingray82
Last active September 15, 2025 21:48
Show Gist options
  • Select an option

  • Save stingray82/265a958931903d78d1dbb7d5a4b57b67 to your computer and use it in GitHub Desktop.

Select an option

Save stingray82/265a958931903d78d1dbb7d5a4b57b67 to your computer and use it in GitHub Desktop.
Intercept and Modify outgoing mail to hotmail
<?php
//Snippet
add_filter('wp_mail', function ($args) {
$domains_to_override = ['hotmail.com', 'outlook.com', 'live.com', 'msn.com']; // add/remove as needed
$new_from_email = 'no-reply@yourdomain.com';
$new_from_name = 'Your Site';
// Normalize "to" into an array
$recipients = is_array($args['to'])
? $args['to']
: preg_split('/[,;]+/', (string) $args['to']);
$should_override = false;
foreach ($recipients as $addr) {
$addr = trim($addr);
// Extract the domain (works for "Name <email@domain>" and raw emails)
if (preg_match('/<([^>]+)>|([^<\s]+)/', $addr, $m)) {
$email = strtolower($m[1] ?: $m[2]);
$domain = substr(strrchr($email, '@'), 1);
if ($domain && in_array($domain, $domains_to_override, true)) {
$should_override = true;
break;
}
}
}
if ($should_override) {
// Normalize headers to array
$headers = $args['headers'];
if (!is_array($headers)) {
$headers = strlen((string) $headers) ? preg_split("/\r\n|\n|\r/", (string) $headers) : [];
}
// Remove any existing From header so our value wins
$headers = array_values(array_filter($headers, function ($h) {
return stripos($h, 'From:') !== 0;
}));
// Set the new From (and optionally Reply-To)
$headers[] = sprintf('From: %s <%s>', $new_from_name, $new_from_email);
// $headers[] = 'Reply-To: Support <support@yourdomain.com>';
$args['headers'] = $headers;
}
return $args;
}, 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment