2013-04-22 27 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。