phpwordpressreplacewp-mail

Why would I need to str_replace \t tabs and \n new lines in wp_mail()?


Check out this simple snippet, which is a "solution" I think I should not have to do... but maybe I am wondering if that IS what ya have to do when using wp_mail() these days?

$headers[] = 'Content-Type: text/plain; charset=\"utf-8\"\r\n'; 
$message = str_replace("\\t", "    ", $message); 
$message = str_replace("\\n", "<br />", $message); 
$message = str_replace("\n", "<br />", $message); 
$message = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", $message); 
                
wp_mail( $send_to, $subject, $message, $headers, $attachments  );

Turns out, that's what I have to do to make the following work as expected, which seems really odd to me... Any ideas why? Plugins are shut off, this is just a little hook in a function to send an email to an admin when certain conditions are met.

If I do NOT do str_replace, then this is the content of the email [below], it literally shows the new line and tab operators. lol, yuk.

Content-Type: text/plain; charset="utf-8"

Admin Notice:\n
\t\t This thing happened, [link]

If I do NOT do str_replace, then this is the html content of the email, again showing new line and tab operators. lol, also yuk

Content-Type: text/html; charset=us-ascii

<p>Admin Notice:\n<br />
\t\t This thing happened, [link]</p>

The body content of that email text is stored in a theme-option textarea. The default of that textarea contains \t\t, but when I save the value it becomes \\t\\t, hence those 4x lazy str_replace functions.

Ultimately, str_replace "works" but I just can not help but think I am missing something here or there is another problem I am not seeing...

If this is indeed "how its done" in wp_mail() let me know...

Surprisingly, none of these do the trick either...

$message = str_replace("\\t", "\t", $message); // not work
$message = rawurldecode($message); // not work
$message = htmlentities($message); // not work
$message = nl2br($message); // uh uh
$message = stripcslashes($message); // not work
$message = htmlspec($message); // nope
$message = wordwrap($message); // not the droid im lookn4

Whats interesting here is this works, when I shove some \r\n\t\t up in that header...

$headers[] = 'Content-Type: text/plain; charset=\"utf-8\"\r\n'; 

Just... does not work in the wp_mail($message) - odd...


Solution

  • Just did a test with wp_mail using these header settings:

    'Content-Type: text/plain; charset=utf-8'
    'Content-Transfer-Encoding: quoted-printable'
    
    ('Content-Transfer-Encoding: bit-8' also worked)
    

    A single backslash within double quotes ("\t") was sufficient to render the tab character.

    Be careful that you are not using the wp_mail_content_type filter hook to set the content type to HTML.

    If you send text-only content while using the text/html mime type (either using a Header or wp_mail_content_type), the tab characters can appear to be stripped out by the email client.

    When using text/html, the tab characters will be sent to the email server and client. If you open the email to view the raw content (e.g. View Source option in Thunderbird), you will see that the tab characters are there. Some email clients style plain text emails and display them using HTML. Since HTML collapses contiguous space characters, the display appears to have stripped out the tabs.

    Headers

    Below, are all the headers I used:

    $headers   = array();
    $headers[] = "From: ${from}";
    $headers[] = "Sender: ${from}";
    $headers[] = 'Content-Type: text/plain; charset=utf-8';
    $headers[] = 'Content-Transfer-Encoding: bit-8';
    $headers[] = 'Content-Language: ' . $locale;