Setting the priority with wp_mail

Hi

I'm trying to set a high priority for an email that is triggered when a custom post is published. I have added the header "X-Priority: 1" but when the email is arrives it has 2 priority headers - "X-Priority: 3" &
"X-Priority: 1". The email is interpreted as a lower priority because there are no exclamation marks (!!)

Here is my code :

add_action('publish_referral', 'send_email');
function send_email($post_id){
	global $post;
	$ref_no = $post->ID;
	$author_id = $post->post_author;
	$sender_name = get_the_author_meta('display_name', $author_id);
	$sender_email = get_the_author_meta('user_email', $author_id);
	$recipient_terms = wp_get_post_terms( $post->ID, 'partners' );
	$recipient_slug = $recipient_terms [0]->slug;
	$recipient_name = $recipient_terms [0]->name;
	$recipient = get_user_by( 'slug', $recipient_slug );
	$recipient_email = $recipient->user_email;
	$edit_post_url = get_edit_post_link($ref_no, '');
	$from_header = 'From:  <info@domain.org.uk>' . "\r\n";
	$recipient_replyto_header = "Reply-To: \"$sender_name\" <$sender_email>\r\n";
	$sender_replyto_header = "Reply-To: \"$recipient_name\" <$recipient_email>\r\n";

 $priority_header = "X-Priority: 1 (Highest)\r\n";

    if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {

    	$sender_message = "Your referral has been sent to ";
    	$sender_message .= $recipient_name.".\r\n";
    	$sender_message .= "You should receive a response within 5 working days.\r\n\r\n";
    	$sender_message .= $edit_post_url;

    	$recipient_message = "You have a referral from ";
    	$recipient_message .= $sender_name.".\r\n";
    	$recipient_message .= "Please action within 5 working days.\r\n\r\n";
    	$recipient_message .= $edit_post_url;

		wp_mail( $sender_email, 'New Referral: #'.$ref_no, $sender_message, $from_header.$sender_replyto_header.$priority_header);
		wp_mail( $recipient_email, 'New Referral: #'.$ref_no, $recipient_message, $from_header.$recipient_replyto_header.$priority_header);

	} else if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] == 'publish' ) ) {

		$sender_message = "Referral #".$ref_no." to ".$recipient_name." has been updated.\r\n\r\n";
		$sender_message .= $edit_post_url;
		wp_mail( $sender_email, 'Updated Referral: #'.$ref_no, $sender_message, $from_header.$sender_replyto_header.$priority_header);

		$recipient_message = "Referral #".$ref_no." from ".$sender_name." has been updated.\r\n\r\n";
		$recipient_message .= $edit_post_url;
		wp_mail( $recipient_email, 'Updated Referral: #'.$ref_no, $recipient_message, $from_header.$recipient_replyto_header.$priority_header);
	}
}
  • aristath

    Hello again @Wonderdust, I hope you're well today!

    I'm afraid this is bit above my level of expertise...
    I have however notified our Second Level Support on this thread (they're our code gurus), hopefully they'll be able to assist.
    Please keep in mind though that SLS have a lot of responsibilities and they usually deal with the most complex issues in these forums so this could take a bit longer than a normal ticket.

    Cheers,
    Ari.

  • Hoang Ngo

    Hi @Wonderdust,

    I hope you are well today and thanks for your question.

    Firstly, please remove the $priority_header = "X-Priority: 1 (Highest)\r\n"; from your code. And then use this filter

    add_filter('phpmailer_init','update_priority_mailer');
    function update_priority_mailer($mailer){
        $mailer->Priority = 1;
        return $mailer;
    }

    You can add the filter code inside your function, so that will restrict the email headers only get filtered when you submit your custom post.

    Please give it a try and let us update.

    Best regards,
    Hoang Ngo

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.