Yahoo! Mail SMTP server blocking WordPress emails

I’ve been using the excellent Post SMTP plugin on several websites now, and recently configured it using Yahoo! Mail’s SMTP, only to find out it blocks some messages but not others, giving a Request failed; Mailbox unavailable status.

On closer investigation it appeared it only blocked messages with a Reply-to header, such as New Order emails from WooCommerce or Jetpack Contact From messages.

Here’s how to remove the headers in each – note you’ll have to copy and paste the users’ emails when responding then:

/**
 * Remove New Order reply-to address to avoid being blocked by Yahoo!
 */
function new_order_reply_to_admin_header($header, $email_id, $order)
{
  if ($email_id === 'new_order') {
    $email = new WC_Email($email_id);

    $header = "Content-Type: " . $email->get_content_type() . "\r\n";
  }

  return $header;
}
add_filter('woocommerce_email_headers', 'new_order_reply_to_admin_header', 20, 3);
/**
 * Remove reply-to header in Jetpack Contact Form emails to avoid being blocked by Yahoo!
 */
function jetpack_contact_form_remove_reply_to($headers, $comment_author, $reply_to_addr, $to)
{
  return preg_replace('/Reply-To: (.*) <(.*)>\r\n/', '', $headers);
}
add_filter('jetpack_contact_form_email_headers', 'jetpack_contact_form_remove_reply_to', 20, 4);