HTML inside PHP is shown as plain text in email - php

I'm trying to put html into php for emailing.The first time I tried directly entering the html into the variable (php double quotes and html in single quotes) but that resulted in an internal error. Then I used the <<< function and pasted the html insided (proper use of indentation). Now when I receive the email, i get the html file returned in plain text. How do I go from here?
$mailer->Body = <<<DEMO
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
........
</tbody>
</table>
</body>
</html>
DEMO;

if you're using plain php:
mail($to, $subject, $message, $headers);
you need to add this to the headers:
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Related

how to fix The character encoding of the HTML document was not declared

My website runs in localhost without any errors; but when I added my site to CPpanel and run members.php page disapeared (it shows white background). Inspecting the page in console displays this error message :
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.
Inspecting the element shows only these lines:
<div style="clear:both;"></div>
</div>
</div>
</body>
</html>
From the error you get, please add this to your <head> ... </head>:
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-5">
If the charset above did not work, please try this instead:
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
And I hope you defined the before BODY part of your pages as this:
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title> Your Title</title>
</HEAD>
<BODY>
....
EDIT 2:
Before including other any other PHP files in your members.php, type this line first:
header('Content-type: text/html; charset=utf-8');
Have you something like this in header? You put instead of utf-8 encoding of your file.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

Problems with Umlaute in HTML email

I am sending an HTML email that contains an ü-Umlaut in it's body. I'm setting the encoding to UTF-8 in the HTML <head>, as well as in the mail header:
HTML
$body = '<html>
<head>
<title>My Title</title>
<meta charset="utf-8">
</head>
<body>
<h1>Here comes the ü-Umlaut</h1>
...
<p>Here comes an ö-Umlaut</p>
</body>
</html>';
Mail header
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
When viewing the email on my iPhone, I see a ü instead of the ü. But when viewing the mail on my gmail account, the ü is displayed correctly. The ö is displayed correctly on both devices.
A colleague of mine correctly views the ü in his email client. But he doesn't see the ö, but a square with a question mark inside (probably something like �).
Now what's gone wrong here and how can I fix it?
Make sure your php file also forces UTF-8:
<?php
header('Content-type: text/html; charset=UTF-8');
same counts for the calling html page:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
or if you use ajax:
$.ajax(
{
url: './sendemail.php',
type: 'POST',
contentType: "application/x-www-form-urlencoded; utf-8",
All participants must use the same charset. Especially the final email client:
<meta http-equiv=Content-Type content=text/html; charset=UTF-8>

Encoding arabic using UTF8

Currently I created simple website using include system
header.php - contains first part of HTML page (Head, meta tags, JS codes ... etc )
page.php - contains simple php code
page content
My main problem with arabic language
I have to put
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
in page.php, footer.php between <head> tags otherwise the arabic will not support correctly.
This prevents page validation because of these tags.
Is there any method to avoid this problem ?
Thanks
// Send a raw HTTP header
header ('Content-Type: text/html; charset=UTF-8');
// Declare encoding META tag, it causes browser to load the UTF-8 charset
// before displaying the page.
echo '<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />';
// Right to Left issue
echo '<body dir="rtl">';
Encode the arabic string into UTF-8 using a tool like this. (No need to change any settings - that link has the the correct settings you need).
Then use utf8_decode() to decode the string back.
Example:
<?php echo utf8_decode('your_encoded_string_goes_here'); ?>
Apart from all of the answers... Make sure your (HTML/PHP) files are saved with the right encoding utf-8
All you need is to put this
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
in a file that you include/include_once in your pages
EDIT. example:
header.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>my title in العربية</title>
</head>
<body>
mypage.php
<?php
include_once 'header.html';
?>
<p>
العربية
</p>
<?php
include 'foot.html';
?>
foot.html
<div>my footer</div>
</body>
</html>
1- Put this
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
2- You should also save the documents in UTF-8 not ANSI
Your headers should appear as the following:
<!DOCTYPE html>
<html lang="ar">
<head>
<meta charset="utf-8">
</head>
<body>
Save your document as utf-8

PHP Form - Email sent doesn't support characters

I have a contact page (contact.php) that sends a form (formsend.php) when people fill in the contact details.
The contact.php has:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="ISO-8859-1">
Contact.php page is fine, displays my latin Portuguese characters fine (ã, é, etc).
But when I receive the email (when it uses formsend.php), I get the fields people signed up with messed up characters: João is João, etc.
On formsend.php I've tried adding:
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
and
header('Content-Type: text/html; charset=iso-8859-1');
and
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="ISO-8859-1">
as well. Nothing works.
It seems it's the formsend.php file that is failing here.
What can I do to ensure I receive properly formatted characters in the email with the user's info?
Yes you may try to switch utf-8:
and also do not forget to change header info
header('Content-Type: text/html; charset=utf-8');
but also you need to convert user inputed text to utf-8 in this case you will have no problem... I had the same problem with Armenian...

How to set UTF-8 encoding for a PHP file

I have a PHP script called :
http://cyber-flick.com/apiMorpho.php?method=getMorphoData&word=kot
That displays some data in plain text:
Cz��� mowy: rzeczownik
Przypadek: dope�niacz
Rodzaj: şe�ski
Liczba: mnoga
As you can see in place of proper chars there are so "bushes". What i would like to do is display this in a way so that people see in browser proper UTF-8 characters.
You can encapsulate it in HMTL tags and set in meta UTF-8 encoding, but because the data received from this script will be processed further I don't want to use any HTML tags, it should be only plain text result set.
So is there a way to inform browser that this file is UTF-8 without using meta tags?
PS. File is encoded in UTF-8 and if I manually change charset encoding in my browser to UTF-8 it displays ok, but what I want to acomplish is people to not be required to do so.
header('Content-type: text/plain; charset=utf-8');
Also note that setting a header to "text/plain" will result in all html and php (in part) printing the characters on the screen as TEXT, not as HTML. So be aware of possible HTML not parsing when using text type plain.
Using:
header('Content-type: text/html; charset=utf-8');
Can return HTML and PHP as well. Not just text.
PHP, by default, always returns the following header: "Content-Type: text/html" (notice no charset), therefore you must use
<?php header('Content-type: text/plain; charset=utf-8'); ?>
You have to specify what encoding the data is. Either in meta or in headers
header('Content-Type: text/plain; charset=utf-8');
Try this way header('Content-Type: text/plain; charset=utf-8');
HTML file:
<head>
<meta charset="utf-8">
</head>
PHP file :
<?php header('Content-type: text/plain; charset=utf-8'); ?>
Html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="x" content="xx" />
vs Php:
<?php header('Content-type: text/html; charset=ISO-8859-1'); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta name="x" content="xx" />

Categories