Using WordPress ‘fix_phpmailer_messageid()’ PHP function

The fix_phpmailer_messageid() WordPress PHP function corrects the ‘From’ host on outgoing mail to match the site domain.

Usage

$phpmailer = new PHPMailer();
fix_phpmailer_messageid($phpmailer);
echo $phpmailer->Hostname;

In the example above, the fix_phpmailer_messageid() function is called with the $phpmailer instance as an argument. The output would be the corrected hostname to match the site domain.

Parameters

  • $phpmailer (PHPMailer object, required): This is the PHPMailer instance passed by reference.

More information

See WordPress Developer Resources: fix_phpmailer_messageid()

Examples

Correcting the hostname

$phpmailer = new PHPMailer();
fix_phpmailer_messageid($phpmailer);
echo $phpmailer->Hostname;  // Outputs corrected hostname

This code initializes a new PHPMailer instance, corrects the hostname, and then outputs the corrected hostname.

Verifying the change

$phpmailer = new PHPMailer();
echo $phpmailer->Hostname;  // Outputs original hostname
fix_phpmailer_messageid($phpmailer);
echo $phpmailer->Hostname;  // Outputs corrected hostname

This code first outputs the original hostname, corrects it, and then outputs the corrected hostname, allowing you to verify the change.

Using with a mail sending function

$phpmailer = new PHPMailer();
fix_phpmailer_messageid($phpmailer);
$phpmailer->send();

Here, after correcting the hostname, the mail is sent using PHPMailer’s send function.

Checking for errors

$phpmailer = new PHPMailer();
fix_phpmailer_messageid($phpmailer);
if(!$phpmailer->send()) {
    echo $phpmailer->ErrorInfo;
} else {
    echo "Message sent!";
}

This code checks for errors after trying to send the email. If an error occurs, it outputs the error information.

Sending a formatted HTML email

$phpmailer = new PHPMailer();
fix_phpmailer_messageid($phpmailer);

$phpmailer->isHTML(true);
$phpmailer->Body = '<h1>Hello, World!</h1>';
$phpmailer->send();

In this example, after correcting the hostname, the PHPMailer instance is set to handle HTML content, and a simple HTML message is sent.