如何在PHP邮件系统中为textarea换行添加<br>标签?
I see the issue—when your admin hits enter in the textarea, those line breaks are just plain newline characters (\n/\r\n), which HTML emails don’t render as actual line breaks. You need to convert those newlines to <br> tags specifically for the HTML version of your email, while leaving the plain text version untouched.
The Simple Fix: Use nl2br() + Sanitization
PHP has a built-in function nl2br() that does exactly what you need—it replaces every newline with an HTML line break tag. I’d also recommend adding htmlspecialchars() to sanitize the input (critical to prevent XSS vulnerabilities, even for trusted admin users).
Here’s how to update your PHP code:
Modify the line where you set the $html variable:
// Original line: // $html = $_POST['message'];// HTML version of the email // Updated line with line break conversion + sanitization: $html = nl2br(htmlspecialchars($_POST['message'], ENT_QUOTES));// HTML version of the email
Full Modified PHP Code
Here’s your complete script with the fix applied:
$SQL = $odb -> prepare("SELECT `name` FROM `staff_names` WHERE `username` = :username"); $SQL -> execute(array(':username' => $_SESSION['username'])); $name = $SQL -> fetchColumn(0); if (isset($_POST['send-email'])){ if ($user -> isAdmin($odb)){ include('Mail.php'); include('Mail/mime.php'); // Constructing the email $sender = "$name <".strtolower($_SESSION['username']."@example.com>");// Your name and email address $recipient = strip_tags(trim($_POST['email'])); // The Recipients name and email address $subject = strip_tags(trim($_POST['subject']));// Subject for the email $text = $_POST['message'];// Text version of the email // Updated HTML body with line breaks and sanitization $html = nl2br(htmlspecialchars($_POST['message'], ENT_QUOTES));// HTML version of the email $crlf = "\r\n"; $headers = array('From' => $sender, 'Return-Path' => $sender, 'Subject' => $subject); // Creating the Mime message $mime = new Mail_mime($crlf); // Setting the body of the email $mime->setTXTBody($text); $mime->setHTMLBody($html); $body = $mime->get(); $headers = $mime->headers($headers); // Sending the email $mail =& Mail::factory('mail'); $mail->send($recipient, $headers, $body); $notify = success("Email sent to {$_POST['email']} successfully."); } }
Why This Works
nl2br(): Takes the raw newlines from the textarea and converts them to<br>tags, so HTML emails will display line breaks exactly how the admin typed them.htmlspecialchars(): Converts special characters (like<,>,",') into safe HTML entities (e.g.,<,>). This prevents your email HTML from breaking and blocks potential XSS attacks, even if an admin accidentally inputs unexpected characters.
Quick Note
Leave the $text variable as-is—plain text emails recognize newline characters natively, so no conversion is needed there. If you ever want to add richer formatting (like bold or bullet points), you’d need a rich text editor, but for basic line breaks, this solution is perfect.
内容的提问来源于stack exchange,提问作者Zac Ram

