文本区新行发送电子邮件使用php


Textarea new line for sending an email using php

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

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

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

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

我觉得应该是

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

'r'n应替换为<br>

简单使用,

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

当然你要把它保存到mysql数据库中,因此,

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

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

为什么要让它比需要的更困难呢?

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

您显然需要标题和可能有更多的字段,但这是您的textarea