How to retain line breaks when textarea converted to php mail

240 Views Asked by At

I've seen this question posed numerous times here, with most answers the same: use nl2br() function. Which is great, as far as it goes... Anyway, first the form which sends contents as $message:

<textarea name=aMessage rows="46" cols="60" tabindex=5 value=<?php $message; ?> >
Honourable Jane Doe,
Minister of Environment

Sincerely,

</textarea>

Then the conversion:

$message = nl2br(htmlentities($message));
echo $message;

NOTE: with that use of nl2br(), the echo that follows shows the contents with line breaks properly inserted. Great! Then that is handed over to the send_mail() function, which produces a formatted email (tidy boxes etc.) - which looks like:

function send_email($name,$mail,$message) {
$to = "[email protected]";
$subject = "Testing - Public Input";
$body = "
<html><head>
</head><body>Testing - Public Input<br><br>
<table>
<tr><th>Name:</th><th>Email Address:</th></tr>
<tr><td>".$name."</td><td>".$mail."</td></tr>
<tr><th>FEEDBACK:</th></tr>
<tr>
<td colspan='2' style='padding:10px;display:block'>".$message."</td>
</tr>
</table>
</body>
<html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: [email protected]\n";
mail($to,$subject,$body,$headers);
}

At which point, an email is produced with NO line breaks. What is wrong?

1

There are 1 best solutions below

0
On

Try This:

function send_email($name,$mail,$message) {
$to = "[email protected]";
$subject = "Testing - Public Input";
$body = <<< EOF
<html><head>
</head><body>Testing - Public Input<br><br>
<table>
<tr><th>Name:</th><th>Email Address:</th></tr>
<tr><td>".$name."</td><td>".$mail."</td></tr>
<tr><th>FEEDBACK:</th></tr>
<tr>
<td colspan='2' style='padding:10px;display:block'>".$message."</td>
</tr>
</table>
</body>
<html>
EOF;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: [email protected]\n";
mail($to,$subject,$body,$headers);
}