Hello,
I'm using PHPMailer to send html emails.
I use Twig template engine to parse _POST data.
I followed your documentation to enable UTF8 but I still received email with specials characters that haven't been decoded.
Here is my mailer class:
Mailer Class based on PHPMailer
I maybe do something wrong? Any idea...
Try putting:
header('Content-Type: text/html; charset=utf-8'); at first line of code (page).
Also if you content get from MySQL datadabase,
you need to define after connection:
$conn->query("set names 'utf8'");
$conn->set_charset("utf8");
Hello @r0073rr0r ,
Should I put header('Content-Type: text/html; charset=utf-8'); on the email template or on index.php ?
Headers allways goes at first lines of code in index.php
But i thinks if you grab from MySQL you need to define names and charset.
I don't use MySQL, it's flatfile system. Let me try your solution on index.php then :)
That header has no effect on email at all. You're already doing the only thing that PHPMailer requires for UTF-8 to work:
$mail->CharSet = 'UTF-8';
You will need to do the MySQL names setting too. If it's still not looking correct, it means your handling of the data is not preserving the UTF-8 encoding. If you find that static template text is correct but dynamic data from MySQL is not, then you know where the problem is.
If you're not using MySQL it suggests that your template file's encoding is incorrect.
Ok @Synchro, make sens, so the problem come probably from twig ?
Perhaps - you'll probably need to tell twig it should be using UTF-8. It's quite easy to spot UTF-8 data if you hex-dump it - having it look correct isn't reliable as it might be showing you the right chars in the wrong charset.
Hum okay 馃槃
Thanks for the hint, I'll make some tests
So... I've made a var_dump on $content witch is the email template rendered by Twig with _$POST data.
Here is the head of the response.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<title>New message from 脡lodie B茅rang猫re</title>
<!--[if !mso]><!-- -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
As you can see the title contain the name from _$POST data and accent are looking good.
Just after that I use this rendered template ($content ) as PHPMailer Body but I still see character like é in the email I receive.
Really don't understand what's happening
Try:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>New message from 脡lodie B茅rang猫re</title>
<!--[if !mso]><!-- -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--<![endif]-->
According to W3 tags should be:
Meta charset
Title
Meta name="descripition"
Meta name="keywords"
Stylesheet
JavaScript Files
Maybe HTML4 have problems, try changing to HTML5 instead.
I dont know what can else be a problem.
Text like é is HTML entity encoding, which allows you to embed non-ascii chars in ascii text. PHPMailer will have no effect on that. As I said, having it _look_ correct doesn't tell you much, you need to check the underlying data to be sure - for example 茅 is e9 in ISO-8859-1, but is c3a9 in UTF-8 - you can't tell which you're using without hex-dumping the text. Just because your source file _says_ it's UTF-8 doesn't mean it's true!
Seeing those Microsoft tags in your doctype is an extremely bad sign. Steer well clear of any HTML generated by Word, it's truly terrible. That said, the doctype you're using is already HTML5.
A little improvement
with _$POST =
c'est chi芒nt les accents 莽a fou le bordel 茅videment
email look like this =
c'est vraiment chi芒nt les accents 莽a fou le bordel 茅videment
So quote and maybe some others characters still don't work.
Does it help you to understand what could be the problem.
PS : Thanks for your help guys and sorry if I sound newby, coding is not my job.
I try my best to understand you. (you lost me in your last comment @Synchro 馃槙 )
About html email formating I'm using the very popular MJML framework witch is apparently the best.
Found the solution
I had to use Twig flag raw on each $_POST data in Twig template.
from
{{ postdata.firstname}}
to
{{ postdata.firstname|raw}}
Thanks @Synchro and @r0073rr0r to show me the path... 馃毝
馃槂
Yes, that would explain the HTML entities - Twig escapes output by default. Be careful with raw option though - make sure you have validated/sanitized inputs before passing them to your template to be used unfiltered!
@Synchro yep, everything is validated and sanitized before template is parsed.
Thanks for the tips again ;)
Hello everyone
How to send an email via PHPMailer but instead of send as rendered html, user get a email with content of $message as a HTML file?
When you send HTML email, the message body is an HTML file; it is rendered by the client.
Thanks for the answer. I found a solution to fix my problem.
That header has no effect on email at all. You're already doing the only thing that PHPMailer requires for UTF-8 to work:
$mail->CharSet = 'UTF-8';You will need to do the MySQL names setting too. If it's still not looking correct, it means your handling of the data is not preserving the UTF-8 encoding. If you find that static template text is correct but dynamic data from MySQL is to, then you know where the problem is.
You saved my day
Most helpful comment
That header has no effect on email at all. You're already doing the only thing that PHPMailer requires for UTF-8 to work:
You will need to do the MySQL names setting too. If it's still not looking correct, it means your handling of the data is not preserving the UTF-8 encoding. If you find that static template text is correct but dynamic data from MySQL is not, then you know where the problem is.