如何使邮件的内容为html格式而不是文本字符串?

时间:2016-10-16 11:10:14

标签: php

在下面的代码中,每当单击按钮时,调用sendmail函数并向php页面发出ajax post请求.php实习生发送邮件。我正在获取ajax函数以文本格式发送的内容而不是浏览器上显示的原始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("example@gmail.com","My subject",$var1);//How to make the content in the html format rather than a string of text
fwrite($var2,$var1); 
?>

2 个答案:

答案 0 :(得分: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("example@gmail.com","My subject",$var1, $headers);
fwrite($var2,$var1); 
?>

答案 1 :(得分:0)

如果您使用的是codeigniter框架,则必须只为其编写一行。 把这行 $ email_setting = array(&#39; mailtype&#39; =&gt;&#39; html&#39;);

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
         }
}