I'm using PHPMailer for sending mail and I have textarea for the body of mail.So when I write text I need to new line but when I new line it dose not work when the text rich to the mail, it still show only one line. I have the PHP code as below:
<?php
if(isset($_POST['submit'])){
require_once('class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = str_replace ('<br>' , '\r\n', $_POST['about']); // $_POST['about'] is the value text take from the body text area of mail
//$body = $_POST['about'];
$from = $_POST['from'];
$mail->AddReplyTo($from,$from);
$mail->SetFrom($from, $from);
$mail->AddReplyTo($from,$from);
$address = $_POST['email'];
$mail->AddAddress($address, $address);
$mail->Subject = $_POST['subject'];
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message envoyé!";
}
}
?>
Anyone help me to fix this please, thanks.
Check out PHP's nl2br() function. I believe this is what you are looking for:
I think it should be
$body = str_replace ('\r\n' , '<br>' , $_POST['about']);
\\r\\n
should be replace with <br>
Simply Use,
$body = nl2br ($_POST['about']);
And ofcourse you are saving this to mysql database, so for that use,
$body = mysql_real_escape_string ( nl2br ($_POST['about']) );
Here note that, $body = nl2br ( mysql_real_escape_string ($_POST['about']) );
will not work.
Why make this harder than it needs to be?
//here is the pull from the form
$your_form_text = $_POST['your_form_text'];
//line 1 fixes the line breaks - line 2 the slashes
$your_form_text = nl2br($your_form_text);
$your_form_text = stripslashes($your_form_text);
//email away
$message = "Comments: $your_form_text";
mail("destination_email@whatever.com", "Website Form Submission", $message, $headers);
You will obviously need headers and likely have more fields, but this is your textarea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.