Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTF-8 html email #1010

Closed
jimblue opened this issue Mar 22, 2017 · 21 comments
Closed

UTF-8 html email #1010

jimblue opened this issue Mar 22, 2017 · 21 comments

Comments

@jimblue
Copy link

jimblue commented Mar 22, 2017

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

@r0073rr0r
Copy link
Contributor

r0073rr0r commented Mar 22, 2017

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");

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

Hello @r0073rr0r ,

Should I put header('Content-Type: text/html; charset=utf-8'); on the email template or on index.php ?

@r0073rr0r
Copy link
Contributor

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.

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

I don't use MySQL, it's flatfile system. Let me try your solution on index.php then :)

@Synchro
Copy link
Member

Synchro commented Mar 22, 2017

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.

@Synchro
Copy link
Member

Synchro commented Mar 22, 2017

If you're not using MySQL it suggests that your template file's encoding is incorrect.

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

Ok @Synchro, make sens, so the problem come probably from twig ?

@Synchro
Copy link
Member

Synchro commented Mar 22, 2017

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.

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

Hum okay 😄

Thanks for the hint, I'll make some tests

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

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:https://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 &#233; in the email I receive.

Really don't understand what's happening

@r0073rr0r
Copy link
Contributor

Try:

<html xmlns="http:https://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.

@Synchro
Copy link
Member

Synchro commented Mar 22, 2017

Text like &#233; 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.

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

A little improvement

with _$POST =

c'est chiânt les accents ça fou le bordel évidement

email look like this =

c&#39;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 😕 )

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

About html email formating I'm using the very popular MJML framework witch is apparently the best.

MJML GITHUB

@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

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... 🚶

😃

@Synchro
Copy link
Member

Synchro commented Mar 22, 2017

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 Synchro closed this as completed Mar 22, 2017
@jimblue
Copy link
Author

jimblue commented Mar 22, 2017

@Synchro yep, everything is validated and sanitized before template is parsed.
Thanks for the tips again ;)

@bahmanian
Copy link

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?

@Synchro
Copy link
Member

Synchro commented Oct 17, 2018

When you send HTML email, the message body is an HTML file; it is rendered by the client.

@bahmanian
Copy link

Thanks for the answer. I found a solution to fix my problem.

@MarcusXavierr
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants