Send email with Acentos BD Uft8

Asked

Viewed 10,370 times

5

I’m having a problem emailing names from the database. Some names come with accents and when I receive the emails the names are disfigured.

The database is as Utf8 - Default collation

Php code:

$PHPMailer->Charset = 'UTF-8';
$PHPMailer->Body = "<body>
<p>
<strong>
Faltam 10 dias para terminar um ou mais documentos do $Nome
</strong> 

Already tried: (At the beginning of php code)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
  • I solved my problem by putting in the email header: Content-Type: text/plain; charset=utf-8\r\n

  • 1

    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.

  • In the email header? And where can I put this function ?

1 answer

10


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

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

    Captura de tela do Geany

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

  3. 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;
    
  4. 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>
  • 3

    Congratulations, your suggestion worked perfectly!!!

  • 1

    I used solution 4. It ran straight... Thank you!

Browser other questions tagged

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