2013-04-22 28 views
1

我使用PHPMailer发送邮件,并且我为邮件正文创建了textarea。所以当我写文本时,我需要换行,但是当我换行时它不工作文本丰富的邮件,它仍然只显示一行。 我有PHP代码如下: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;!"; 
    } 
} 
?> 

任何人都帮我解决这个问题,请,谢谢。

+0

'$ mail-> AltBody =“要查看邮件,请使用HTML兼容的电子邮件查看器!”;' - **我强烈推荐使用纯文本替代方法,而不是更改电子邮件软件的指令。您的电子邮件不太可能足以让人们更改电子邮件客户端来阅读它! – Quentin 2013-04-22 05:49:49

回答

1

我觉得应该是

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

\r\n<br>

+1

这种形式的答案不是很有用。考虑为后代增加更多的解释。 – 2013-04-22 05:39:21

0

简单地使用可更换,

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

当然,你正在保存这个t ØMySQL数据库,所以在使用,

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

这里注意的是,$body = nl2br (mysql_real_escape_string ($_POST['about']));将无法​​正常工作。

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("[email protected]", "Website Form Submission", $message, $headers); 

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