0

I am trying to use this code to pass via POST a variable containing HTML

var data = {
    message: $('#mydiv').html()
};
jQuery.ajax({
    type: 'POST',
    data: data,
    url: '/myurl?action=send_email',
    success: function( response ) { }
});

In PHP, I retrieve the data and I send an Email using the data content

$message = "Hello<br>" . $_POST['message'] . "<br>Bye Bye";
$mail = mail($email, $subject, nl2br($message), $headers);

The HTML within the email that I receive is badly formatted:

<img width="\&quot;70\&quot;" height="\&quot;87\&quot;" alt="\&quot;D_6928_antiqueoak_vapor\&quot;">

Can someone tell me why and if there is a solution? Thank you a lot

Ruben Rizzi
  • 342
  • 1
  • 3
  • 20
  • Why on earth are you sending HTML? – max Apr 01 '15 at 07:44
  • 3
    Uh... Why not? Do you receive a lot of plain-text e-mails? With no formatting, no line breaks and all? – Jeremy Thille Apr 01 '15 at 07:46
  • @Ruben what are your mail headers? Did you activate UTF-8 encoding? – Jeremy Thille Apr 01 '15 at 07:46
  • No I mean why are you sending HTML from the browser to your server. Its going to be really difficult to validate or sanitize, just use `$.data()` or `$.attr('whatever')` and send plain data which you then construct HTML with. – max Apr 01 '15 at 07:50
  • @papirtiger you can send html esay enough, why would you bother reconstructing the html on the server side if you can just grab it from the client side already laid out and ready to go. Not to mention it would make it easier to abstract your php code – Wesley Smith Apr 01 '15 at 07:55
  • Because of [XSS](http://en.wikipedia.org/wiki/Cross-site_scripting) – max Apr 01 '15 at 07:56
  • Find the old question link http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript – Shelim Apr 01 '15 at 09:09

3 Answers3

0

Try this method using encodeURIComponent()

var data = 'message='+$('#mydiv').html();
jQuery.ajax({
    type: 'POST',
    data: encodeVars(data),
    url: '/myurl?action=send_email',
    success: function( response ) { }
});


function encodeVars(vars){
    return vars.map(function (cell) {
        var res = cell.split('=');
        return res[0] + '=' + encodeURIComponent(res[1]);
    }) ;    
}
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
0

Always set content-type when sending HTML email

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Asit
  • 458
  • 4
  • 14
0

I solved in that way. Javascript

var data = {
    message: $('#mydiv').html()
};
jQuery.ajax({
    type: 'POST',
    data: data.replace(/&/g, "&amp;")
            .replace(/"/g, "&quot;")
            .replace(/'/g, "&#039;"),
    url: '/myurl?action=send_email',
    success: function( response ) { }
});

PHP

$message = preg_replace('/&amp;/', '&', $_POST['message']);
$message = preg_replace('/&quot;/', '"', $message);
$message = preg_replace('/&#039;/', "'", $message);
$message = "Hello<br>" . $message . "<br>Bye Bye";
$mail = mail($email, $subject, nl2br($message), $headers);
Ruben Rizzi
  • 342
  • 1
  • 3
  • 20