Send email with Accents BD Uft8

4

I have a problem sending email with names that come from the database. Some names come with accents and when I get the emails the names are unconfigured.

The database is like Utf8 - Default collation

Code php:

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

I've tried: (At the beginning of the php code)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
    
asked by anonymous 25.06.2014 / 10:58

1 answer

8

To send the email running smoothly, you should ensure that everything is using the same charset . If not, you'll need to do the conversions to get the email in.

Assuming UTF-8

  • PHP documents should be encoded in UTF-8.

    How to handle this depends on the IDE to use, but whether it is in an IDE or a 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 should be in Collation in UTF-8.

    To ensure that there will be no need to convert between charsets the data should be stored in a database that contains a charset equal to the one 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 in documentation (English). In order to ensure correct sending with UTF-8 data, you must define charset :

    $mail->CharSet = 'UTF-8';
    
  • With these steps you can ensure that throughout your application the data is always with charset UTF-8 thus avoiding problems with special characters, accented characters and / or eventual conversions between charsets .

    Note:
    When we talk about HTML and Ajax, pages should also contain headers in UTF-8 to ensure a proper presentation to the visitor and correct submission of data from the browser to the server (Ajax):

    <!-- HTML 5 -->
    <!doctype html>
    <html>
      <head>
        <meta charset="UTF-8">
        ...
      </head>
      <body>
        ...
      </body>
    </html>
    
        
    25.06.2014 / 13:26