簡體   English   中英

Textarea使用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);

您顯然需要標題,可能還有更多字段,但這是您的文本區域。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM