You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP邮件系统中为textarea换行添加<br>标签?

Fixing Line Breaks for HTML Emails in Your Admin Form

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., &lt;, &gt;). 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:53:43