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.
My Answer:
To keep line breaks in the email content, you can use the nl2br() function in PHP to convert newlines to HTML line breaks. Here's how you can modify your code to include this:
php
$tajuk_email = $_POST['tajuk_email'];
$content = nl2br($_POST['content']); // Convert newlines to HTML line breaks
$content_1 = "".$content."
";
$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();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPDebug = 0;
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'your_email@gmail.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'ssl';
$mail->From = "Tester";
$mail->FromName = "Tester 1";
foreach ($recipients as $email => $name) {
$mail->AddAddress($email, $name);
}
$mail->AddCC("cc_email@example.com", "cc_name");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $tajuk_email;
$mail->Body = $content_1;
// Send the email
if($mail->Send()) {
echo "Email sent successfully";
} else {
echo "Email sending failed";
}
By using nl2br() function, the newlines in the textarea content will be converted to HTML line breaks and the email will display the content with line breaks intact.
Rate this post
4 of 5 based on 9273 votesComments