Email exiting with strange characters in PHP

Asked

Viewed 977 times

0

Guys, I use a PHP + Mysql system, but every email that searches the e-mail data in the database as a subject, etc..., in the words where there are accents, everything goes wrong, an example: it’s not like that N$O$, I’ve changed the default of the database to UTF-8 but did not solve.

3 answers

2

You set the email headers?

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: base64"."\r\n";
$headers .= "From: <".$from.">\r\n";
mail($to, $subj, chunk_split(base64_encode($msg)), $headers);

2

1

You can convert the values to UTF8 in real time using PHP functions, utf8_encode and utf8_decode:

utf8_encode($campoDoBanco)

Or convert from UTF8 to ISO using:

utf8_decode ($campoDoBanco)

This resolves in the case of email, so you don’t need to touch the rest of the application. Remembering that for the email to stay straight, it is recommended you do not use some ENCODING, but just convert to HTML using the htmlentities:

htmlentities ($campoDoBanco, ENT_QUOTES, ENCODING_DO_BANCO);

Normally the standard of ENCODING_DO_BANCO is 'ISO-8859-1' (latin1), but you can switch to 'UTF-8'. To check the database encoding, run:

$link    = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$charset = mysql_client_encoding($link);

echo "O conjunto de caracteres atual é: $charset\n";
  • Just the opposite, it is essential to use the correct encoding and header. This "it is recommended that you do not use any ENCODING" is a bad idea. I understand that if you use entities, encoding is irrelevant, but then you have to warn the user that the character set is more limited (not everything has a "full" entity). Another thing: "by default the bank encoding" is ISO, not quite so. Better check on the server, has recent versions coming with UTF-8, and has host that already configures UTF-8 as default.

  • Remember that Roberto send emails... and many email clients do not accept ENCODING right... especially those of M$! anyway, I’m editing to improve the response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.