E-mail coming out with strange characters in PHP [closed]

0

I use a PHP + MySQL system, but every email that searches the data of the email in the database as a subject, etc ..., in the words where it has accents everything goes wrong, an example: no it's like this N $$ O $, I've already changed the default for UTF-8 but it did not work.

    
asked by anonymous 13.10.2016 / 13:35

3 answers

2

Have 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);
    
13.10.2016 / 13:46
2

You can use the PHPMailer class to send e-mails, the page in Github is a good example of use:

link

By default PHPMailer is configured with ISO-8859-1 charset, I advise you to leave as is and test, if the accented characters continue to present a problem, then you can change to UTF-8:

$mail->Charset = 'UTF-8';

The full documentation is at link

    
13.10.2016 / 14:06
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 do not have to mess with the rest of the application. Remember that for the email to be correct, it is recommended that you do not use ENCODING any, but rather just convert to HTML using htmlentities :

htmlentities ($campoDoBanco, ENT_QUOTES, ENCODING_DO_BANCO);

Normally, the ENCODING_DO_BANCO default 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";
    
13.10.2016 / 13:48