【问题标题】:Textarea new line for sending an email using php使用 php 发送电子邮件的 Textarea 新行
【发布时间】:2013-04-22 04:34:09
【问题描述】:

我正在使用 PHPMailer 发送邮件,我有 textarea 作为邮件正文。所以当我写文本时我需要换行但是当我换行时它不起作用,当文本丰富到邮件时,它仍然只显示一行。 我的 PHP 代码如下:

<?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&eacute;!";
    }
}   
?>

谁能帮我解决这个问题,谢谢。

【问题讨论】:

  • $mail-&gt;AltBody = "To view the message, please use an HTML compatible email viewer!"; — 我强烈建议实际使用纯文本替代方案,而不是更改电子邮件软件的说明。您的电子邮件可能不够重要,以至于人们会更改电子邮件客户端来阅读它!

标签: php email textarea phpmailer


【解决方案1】:

查看 PHP 的 nl2br() 函数。我相信这就是您正在寻找的:

http://ca3.php.net/manual/en/function.nl2br.php

【讨论】:

    【解决方案2】:

    我觉得应该是的

    $body = str_replace ('\r\n' , '<br>' , $_POST['about']);
    

    \r\n 应替换为 &lt;br&gt;

    【讨论】:

    • 这种形式的答案不是很有用。考虑为后代添加更多解释。
    【解决方案3】:

    简单使用,

    $body = nl2br ($_POST['about']);
    

    当然你要把它保存到 mysql 数据库中,所以为了那个用途,

    $body = mysql_real_escape_string ( nl2br ($_POST['about']) );
    

    请注意,$body = nl2br ( mysql_real_escape_string ($_POST['about']) ); 将不起作用。

    【讨论】:

      【解决方案4】:

      为什么要让它变得比需要的更难?

      //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);
      

      您显然需要标题并且可能有更多字段,但这是您的文本区域。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-15
        • 2013-07-13
        • 1970-01-01
        • 2016-04-22
        • 1970-01-01
        • 2011-09-16
        • 1970-01-01
        相关资源
        最近更新 更多