Php 如何将邮件内容设置为html格式而不是文本字符串?
在下面的代码中,每当单击按钮时,都会调用sendmail函数,并向php页面发出ajax post请求。php实习生发送邮件。我得到的是ajax函数以文本格式发送的内容,而不是浏览器上显示的原始html格式。如何将邮件内容以html格式发送格式Php 如何将邮件内容设置为html格式而不是文本字符串?,php,Php,在下面的代码中,每当单击按钮时,都会调用sendmail函数,并向php页面发出ajax post请求。php实习生发送邮件。我得到的是ajax函数以文本格式发送的内容,而不是浏览器上显示的原始html格式。如何将邮件内容以html格式发送格式 function sendmail(){ $.ajax({ dataType: 'application/html', type: 'POST',url: 'process.php', data:
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("example@gmail.com","My subject",$var1);//How to make the content in the html format rather than a string of text
fwrite($var2,$var1);
?>
您只需在邮件中添加标题即可
<?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("example@gmail.com","My subject",$var1, $headers);
fwrite($var2,$var1);
?>
如果您使用的是codeigniter框架,则只需为此编写一行代码。 把这一行放入$email\u 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
}
}
我这样做了。但问题是$\u POST['div\u content']返回的是文本,而不是html内容。如何使ajax功能以html格式将内容发送到服务器,因为数据类型:“application/html”不起作用??