1

我正在使用 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;!";
    }
}   
?>

请哪位帮我解决这个问题,谢谢。

4

4 回答 4

2

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

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

于 2013-04-22T05:15:14.333 回答
1

我认为应该是

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

\r\n应该替换为<br>

于 2013-04-22T05:22:58.517 回答
0

只需使用,

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

当然,您要将其保存到 mysql 数据库中,因此对于该用途,

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

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

于 2013-04-22T05:25:13.243 回答
0

为什么要让这比需要的更难?

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

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

于 2013-07-11T20:19:57.567 回答