wp_mail() transforms "-" to "–" in email subject

1.4k views Asked by At

I am using the wp_mail() to send emails from a custom WordPress plugin.

I am trying to figure out why the emails sent result in some non-alphanumeric characters in the email subject being changed? For example, a subject such as "Word1 - Word2" will be received as "Word1 – Word 2", which doesn't look good at all.

The code looks like this:

$subject = 'word1 - word2';
$msg = 'message';
$headers = 'Content-Type: text/html; charset=utf-8';
wp_mail('[email protected]', $subject, $msg, $headers);

The email subject shows "Word1 – Word 2" in Gmail. I know it has to do with encoding, but does anyone know how to fix this?

Thanks!

1

There are 1 answers

1
Rick James On
E28093     8211=x2013  [–]   ON  EN DASH

– is an "html entity". There is a whole set of these allowing you to encode any fancy character for web pages, using only plain Ascii characters.

It is also the Unicode "codepoint" 8211 (decimal) or 2013 (hex). And it can be encoded in most places using the 3 utf-8 bytes hex E28093

The sender had a way of encoding an EN dash instead of a plain dash -.

Quite possibly wp_mail deliberately encoded any non-ascii characters in order to avoid strange things happening if it were to be rendered on a web page.

On any web page – will render as –

"Edit" my answer to see that that is exactly what I did. (Note also that ` on this forum inhibits the rendering.)