Laura
Laura

Reputation: 81

Formatting Email Umlaut and Linebreaks

I am just working on a mail function and have problem with formatting it. I want to send the mail as HTML. The newlines shall be converted to linebreaks. Currently I am using these functions

$mail_filtered    = nl2br($mail_filtered);
$mail_filtered    = htmlspecialchars($mail_filtered);

The umlauts are shown correctly, if I use this function, but the linebreaks are shown as <br/> tags in the mail. The mail header looks like this:

"Content-Type: text/html", "Charset=utf-8"

What did I do wrong?

Upvotes: 1

Views: 204

Answers (1)

Gumbo
Gumbo

Reputation: 655219

If you use nl2br to add HTML line breaks to the “physical” line break sequences and apply htmlspecialchars afterwards, you’re also converting the added <br /> into &lt;br /&gt; that is then displayed as <br />:

$str = "foo\nbar>baz";
var_dump(nl2br($str) === "foo<br />\nbar>baz");  // bool(true)
var_dump(htmlspecialchars(nl2br($str)) === "foo&lt;br /&gt;\nbar&gt;baz");  // bool(true)

Do it the other way round, first use htmlspecialchars and than nl2br:

var_dump(nl2br(htmlspecialchars($str)) === "foo<br />\nbar&gt;baz");  // bool(true)

Upvotes: 1

Related Questions