in Software Development #PHP

How to disable the X-Mailer header with PHPMailer

Sometimes things aren't quite as you'd expect them to be. One such case is when you try to disable the X-Mailer header with the PHPMailer library.

The library itself is pretty straight forward, you instantiate the class, and then you can interact with headers as object properties as such:

$mailer = new PHPMailer();
$mailer->XMailer = 'My Awesome Script';

Great, but what if you wanted to disable it completely? My first thoughts would be to set it to an empty string or false:

$mailer->XMailer = false;
$mailer->XMailer = '';

Turns out, both of those retain the original X-Mailer value. The proper way to disable the X-Mailer, historically has been to set it to a string with a single space:

$mailer->XMailer = ' ';

I say "historically" because it seems that more recent versions worked through some issues with PHP and fixed things up to support null as well:

$mailer->XMailer = null;