Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Created December 4, 2024 08:28
Show Gist options
  • Save finalwebsites/4006f73e86b61a7aae14e9a7dc0bbbc6 to your computer and use it in GitHub Desktop.
Save finalwebsites/4006f73e86b61a7aae14e9a7dc0bbbc6 to your computer and use it in GitHub Desktop.
Example: Overide the PHPMailer object in WordPress
<?php
add_action( 'phpmailer_init', 'fw_prepare_email' );
function fw_prepare_email( $phpmailer ) {
$host = get_option('fw_tools_smtp_host');
$port = get_option('fw_tools_smtp_port');
$login = get_option('fw_tools_smtp_login');
$password = get_option('fw_tools_smtp_password');
$from = get_option('fw_tools_from_address');
$force_from = get_option('fw_tools_force_address');
$from_name = get_option('fw_tools_from_name');
$force_name = get_option('fw_tools_force_name');
if (empty($host) or empty($login)) {
return;
}
if ( ! is_object( $phpmailer ) ) {
$phpmailer = (object) $phpmailer;
}
if ($force_from) {
if ($from == '') {
return;
} else {
$phpmailer->From = $from;
}
} else {
$dom_org = substr($phpmailer->From, strpos($phpmailer->From, '@') + 1);
$dom_set = substr($from, strpos($from, '@') + 1);
if ($dom_org != $dom_set) {
$phpmailer->From = $from;
}
}
if ($force_name) {
if ($from_name == '') {
return;
} else {
$phpmailer->FromName = $from_name;
}
} else {
if ($phpmailer->FromName == '') {
if ($from_name != '') {
$phpmailer->FromName = $from_name;
} else {
$phpmailer->FromName = get_option('blogname');
}
}
}
$phpmailer->Mailer = 'smtp';
$phpmailer->Host = $host;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = $port;
//$phpmailer->SMTPDebug = 1;
$phpmailer->Username = $login;
$phpmailer->Password = $password;
$phpmailer->SMTPSecure = 'tls';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment