2016-10-16 33 views
-1

在下面的代碼中,每當按鈕被點擊時,sendmail函數被調用,並向php頁面發出ajax post請求。php intern發送郵件。我通過ajax函數以文本格式發送內容而不是瀏覽器上顯示的原始html格式。如何獲取郵件的內容爲html格式。如何使郵件的內容爲html格式而不是文本字符串?

function sendmail(){ 
    $.ajax({ 
     dataType: 'application/html', 
     type: 'POST',url: 'process.php', 
     data: { 'number': $('#div_content').html() }//div_content has all the content displayed on the html page 
    }).done(function() { 
     $('#div_content').html("Mail Sent"); 
    }); 
} 

PHP發送郵件 process.php

<?php 
$var1=$_POST['div_content']; 
$var2=fopen("somefile.txt","w+"); 
mail("[email protected]","My subject",$var1);//How to make the content in the html format rather than a string of text 
fwrite($var2,$var1); 
?> 

回答

0

你只需要頭添加到您的郵件

<?php 

$headers = "From: ... \r\n"; 
$headers .= "Reply-To: ... \r\n"; 
$headers .= "CC: ... \r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \r\n"; 

$var1=$_POST['div_content']; 
$var2=fopen("somefile.txt","w+"); 
mail("[email protected]","My subject",$var1, $headers); 
fwrite($var2,$var1); 
?> 
+0

我做this.But問題是$ _ POST [「div_content」]將返回文本,而不是HTML content.How使AJAX功能將內容發送到服務器以HTML格式,因爲數據類型:'應用程序/ html'不工作? – Volcanus

0

如果使用的是笨的框架,你只寫一個爲此。 把這一行$ email_setting = array('mailtype'=>'html');

function functionname(){ 
$this->load->library('email'); 
      $email_setting = array('mailtype'=>'html'); 
     $this->email->initialize($email_setting); 
     $this->email->from($from_email); 
     $this->email->to($email); 
     $this->email->subject('Subject title '); 
     $this->email->message($msg_data); 

     //Send mail 
     if($this->email->send()){ 
      // done 
     } 
} 
相關問題