Textarea使用php发送电子邮件的新行

时间:2013-04-22 04:34:09

标签: php email textarea phpmailer

我正在使用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 个答案:

答案 0 :(得分:2)

查看PHP的nl2br()函数。我相信这就是你要找的东西:

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

答案 1 :(得分:1)

我认为应该是

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

\r\n应替换为<br>

答案 2 :(得分:0)

简单使用,

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

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

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

请注意,$body = nl2br ( mysql_real_escape_string ($_POST['about']) );无效。

答案 3 :(得分: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);

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