$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How do I override the Message-ID header of wp_mail function?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How do I override the Message-ID header of wp_mail function?

matteradmin7PV0评论

I have a custom notification function for our comments editor, who prefers to have all comments from one article threaded together in her email client. To achieve this, I'm creating a custom message-ID for the first comment on an article, then setting that as the In-Reply-To for future comment notifications.

This is working to some extent - I can see the additional headers in the mail client - however, the first message is being created with TWO Message-IDs. In other words, the one I passed into the headers is NOT overriding the one Wordpress is autogenerating. Therefore, the threading doesn't work.

Is this a bug with Wordpress? I don't want to resort to hunting down the actual WP_mail function and editing core code; and I'm not sure that would even work. Is this something more fundamental with PHP Mail function that I can't change perhaps?

$messageIDtoCreate = $post->ID.".".time(); // post ID, and current timestamp
add_post_meta( $post->ID, 'messageID', $messageIDtoCreate);
// add to the email headers
$message_headers .= "Message-ID: <".$messageIDtoCreate."@test>\n";

Thanks in advance.

I have a custom notification function for our comments editor, who prefers to have all comments from one article threaded together in her email client. To achieve this, I'm creating a custom message-ID for the first comment on an article, then setting that as the In-Reply-To for future comment notifications.

This is working to some extent - I can see the additional headers in the mail client - however, the first message is being created with TWO Message-IDs. In other words, the one I passed into the headers is NOT overriding the one Wordpress is autogenerating. Therefore, the threading doesn't work.

Is this a bug with Wordpress? I don't want to resort to hunting down the actual WP_mail function and editing core code; and I'm not sure that would even work. Is this something more fundamental with PHP Mail function that I can't change perhaps?

$messageIDtoCreate = $post->ID.".".time(); // post ID, and current timestamp
add_post_meta( $post->ID, 'messageID', $messageIDtoCreate);
// add to the email headers
$message_headers .= "Message-ID: <".$messageIDtoCreate."@test>\n";

Thanks in advance.

Share Improve this question asked May 18, 2012 at 9:37 James BruceJames Bruce 2593 silver badges7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

You can filter the $phpmailer object. Something like this should do the trick (not tested):

add_action( 'phpmailer_init', 'wpse_52555_msg_id' );

function wpse_52555_msg_id( &$phpmailer )
{
    $msg_id = get_post_meta( get_the_ID(), 'messageID', TRUE );
    '' !== $msg_id and $phpmailer->MessageID = $msg_id . '@test';
}

Anyone looking to just update the hostname inside the messageID, which may be needed for Nginx setup. Wordpress documentation.

add_action('phpmailer_init', 'sender_message_id');

function sender_message_id(&$phpmailer) {
  $phpmailer->Hostname = 'domain';
}

I think I found the solution to the initial problem.

You must, of course, use the phpmailer_init action to change the message-ID property of object phpmailer (used by wp_mail function). Thanks to Fuxia for the answer.

It is also necessary to respect message-ID format in accordance with the rfc5322 section-3.6.4 otherwise your new message-ID will be rejected (consult here the source file of phpmailer where the message-ID is setted).

Here is the correct format of a message-ID : <leftpart@rightpart> including brackets

Here is the code tested that works:

add_action('phpmailer_init', 'set_message_id');

function set_message_id(&$phpmailer) {
  $leftpart = 'mycustominfo-'.uniqid();
  $domain = 'mydomain';
  $messageID = '<'.$leftpart.'@'.$domain.'>';
  $phpmailer->MessageID = $messageID;
}

I hope it will help

Post a comment

comment list (0)

  1. No comments so far