-1

In the below code whenever the button is clicked sendmail function is called and makes a ajax post request to the php page.The php intern sends the mail.I am getting the contents sent by the ajax function in the text format rather than the original html format displayed on the browser.How to get the contents of the mail to be in the html format.

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 for sending the mail 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); 
?>
RanjanaLK
  • 685
  • 2
  • 13
  • 26
Volcanus
  • 1
  • 2

2 Answers2

0

You just have to add headers to your mail

<?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); 
?>
  • I did this.But the problem is $_POST['div_content'] is returning text and not the html content.How to make ajax function to send the content to the server in html format because dataType: 'application/html' is not working?? – Volcanus Oct 16 '16 at 13:25
0

If you are using codeigniter framework ,you have to write only one line for that. put this line $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
         }
}