Fixing the InvalidInternetMessageHeader Error in Outlook

Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy but don’t provide support for code customizations or 3rd party development.

Overview

Are you using the Outlook mailer and encountering an ‘InvalidInternetMessageHeader’ error in WP Mail SMTP? This error often occurs due to specific email headers causing conflicts. Email headers are crucial for defining the behavior of your emails, and a misconfiguration might lead to conflicts with certain plugins.

In this tutorial, we’ll guide you on how to use a PHP snippet to modify specific headers in your emails, which can help resolve this ‘InvalidInternetMessageHeader’ error exclusive to the Outlook mailer.

Note: Be sure to install and activate the WP Mail SMTP plugin on your site before adding the code snippet provided in this tutorial.

A general solution to this issue is to prefix the problematic header with X-. In the context of the Outlook mailer, all custom headers should ideally be prefixed with X-. This standard helps to avoid conflicts and errors like the ‘InvalidInternetMessageHeader’ error.

We’ll implement this solution in the following code snippet.

Setup

We recommend that you copy and paste the required snippet below into a new WPCode snippet. WPCode makes it easy and safe to run code snippets on your site.

For help with adding snippets to your site, please see our tutorial on adding code snippets using the WPCode plugin.

Copy and paste the snippet below to your site. On line 5, you’ll need to replace 'Auto-Submitted'with the name of the header that’s causing the ‘InvalidInternetMessageHeader’ error in your setup.

Next, replace 'X-Auto-Submitted'on line 6 with the problematic header’s name prefixed with 'X-'. This means you’re renaming the original problematic header to a new one that adheres to the X- prefix standard.

1   function wp_mail_smtp_modify_header( $body, $mailer ) {
2       if ( 'outlook' === $mailer ) {
3           $headers = $body['message']['internetMessageHeaders'];
4           foreach( $headers as $key => $header ) {
5               if ( 'Auto-Submitted' === $header['name'] ) {
6               $body['message']['internetMessageHeaders'][$key]['name'] = 'X-Auto-Submitted';
7               }
8           }
9       }
10      return $body;
11  }
12  add_filter( 'wp_mail_smtp_providers_mailer_get_body', 'wp_mail_smtp_modify_header', 10, 2 );

Note: The header causing the error might differ for each user, based on their specific setup. Therefore, it’s critical to identify the header causing the ‘InvalidInternetMessageHeader’ error in your situation, and then rename it by adding the prefix X- to avoid similar issues.

That’s it! You’ve successfully adjusted the problematic email header in WP Mail SMTP for the Outlook mailer. This should resolve the ‘InvalidInternetMessageHeader’ error.

Next, want to explore more ways to customize your WP Mail SMTP setup? Be sure to look at our other code snippets for more customization options.