To send the email without problems, you must ensure that everything is using the same charset. If not, you will need to make conversions so that the email arrives properly.
Assuming UTF-8
PHP documents should be encoded in UTF-8.
How to handle this depends on the IDE to be used, but whether in an IDE or text editor, there is always the option to set the file encoding, as well as a location where it can be viewed:
The document sending the email should contain the following line:
<?php
header('Content-Type: text/html; charset=UTF-8');
// o teu código
?>
Place at the beginning of the document before any output or operation.
The database, the tables and their respective fields must be Collation in UTF-8.
To ensure that there will be no need for conversion between Charsets the data shall be stored in a database containing a charset equal to that used to process them.
Example in Mysql
ALTER DATABASE minhaBaseDados CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE minhaTabela CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
The default Phpmailer charset is iso-8859-1 as can be seen on documentation (English). To ensure a correct sending with data in UTF-8 you need to define the charset:
$mail->CharSet = 'UTF-8';
With these steps you can ensure that throughout your application the data is always with the charset UTF-8 thus avoiding problems with special characters, accented characters and/or possible conversions between Charsets.
Note:
When we speak in HTML and Ajax, pages must contain the headers also in UTF-8 to ensure a correct presentation to the visitor and a correct sending of data from the browser to the server (Ajax):
<!-- HTML 5 -->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
...
</head>
<body>
...
</body>
</html>
I solved my problem by putting in the email header:
Content-Type: text/plain; charset=utf-8\r\n
– Jorge B.
You can use these functions to help you detect and convert to correct encoding:
utf8_encode
,mb_check_encoding
,mb_detect_encoding
,mb_convert_encoding
,htmlentities
,htmlspecialchars
.– Humberto Castelo Branco
In the email header? And where can I put this function ?
– ChrisAdler