php - How to keep line breaks when email gets text?

I am using phpmailer to do send email function, now my problem is when I pass the textarea value post to the phpmailer function, it cannot keep line breaks to send the email to the selected recipient.
For example, if I want to send content from textarea which is:
A lion was once sleeping in the jungle when a mouse started running up and down his body just for fun.
This disturbed the lion’s sleep, and he woke up quite angry.
Below is my sample code:
$tajuk_email = $_POST['tajuk_email'];
$content= $_POST['content'];
$content_1 = "<p style='white-space: pre-wrap;'>".$content."</p>";
$checkbox_value = $_POST['checkbox_value'];
$checkbox_value = str_replace("=>", ":", $checkbox_value);
$recipients = json_decode( "{" . $checkbox_value . "}", true);
require 'plugins/mail/class.phpmailer.php';
$mail = new PHPMailer(true);
$mail->IsSMTP(); //Sets Mailer to send message using SMTP
$mail->Host = 'smtp.gmail.com'; //Sets the SMTP hosts of your Email hosting, this for Godaddy
$mail->SMTPDebug = 0;
$mail->Port = 465; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication. Utilizes the Username and Password variables
$mail->Username = '[email protected]'; //Sets SMTP username
$mail->Password = '88853sdhdfh'; //Sets SMTP password
$mail->SMTPSecure = 'ssl'; //Sets connection prefix. Options are "", "ssl" or "tls"
$mail->From = "Tester"; //Sets the From email address for the message
$mail->FromName = "Tester 1"; //Sets the From name of the message
foreach ($recipients as $email => $name) {
$mail->AddAddress($email, $name);
}
$mail->AddCC("[email protected]", "sagsa"); //Adds a "Cc" address
$mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = $tajuk_email; //Sets the Subject of the message
$mail->Body = $content_1; //An HTML or plain text message body
The result for content is, it cannot keep line break to send an email:
A lion was once sleeping in the jungle when a mouse started running up and down his body just for fun. This disturbed the lion’s sleep, and he woke up quite angry.
What I have tried?
- I have used
white-space:pre-wrap:
to keep the line break, but cannot work. - I have used
$content= nl2br($_POST['content']);
, but cannot work. - I have used
$content = trim($_POST['content']); $content = nl2br($content );
, but cannot work. - I don't have tried the
CKEditor function
, because I want to keep using textarea function to send the content.
Hope someone can guide me on how to solve this problem. Thanks.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: an exception occurred in the driver: could not find driver
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.