Search code examples
phpemailmime

PHP mail() can't find a solution for accents in message content


I have tried many solutions proposed about it, but still can't find the one that works. The email is sent succesfully but accents are in html code.

This is my code for the email content :

        $to_APPLICATION_RECEIVER = "w@z.ca";
        $from_APPLICATION_RECEIVER = "x@y.com";
        $subject_APPLICATION_RECEIVER = "Subject";
        $headers_APPLICATION_RECEIVER = 'From: "people" <x@y.com> \r\n';
        $headers_APPLICATION_RECEIVER .= 'Reply-to: x@y.com \r\n';

        //define boundary
        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

        //tell header about $mime_boundary
        $headers_APPLICATION_RECEIVER .= "\nMIME-Version: 1.0\n";
        $headers_APPLICATION_RECEIVER .= "Content-Type: multipart/mixed;\n";
        $headers_APPLICATION_RECEIVER .= " boundary=\"{$mime_boundary}\"";

        //Message section
        //$message = utf8_decode($message);
        $message_hed_APPLICATION_RECEIVER ="\n\n--{$mime_boundary}\n";
        $message_hed_APPLICATION_RECEIVER .="Content-Type: text/plain; charset=\"UTF-8\"\n";
        $message_hed_APPLICATION_RECEIVER .="Content-Transfer-Encoding: 8bit\n\n" . $message . "\n\n";
        $message_hed_APPLICATION_RECEIVER .= "--{$mime_boundary}\n";

The content of $message displayed by var_dump is "sfdféèî" but the email message content I receive is : sfdf&eacute;&egrave;&icirc;. Can someone help me with that? I have tried utf8_decode() and many other propositions on forums but still it ain't working. Thanks for your help guys.


Solution

  • If you view the source of the page you will see that your string actually does contain the html entities. var_dump() will have hinted at this being the case like so:

    string(27) "sfdféèî"
    

    You can convert the entities back to UTF8 with:

    html_entity_decode('sfdf&eacute;&egrave;&icirc;')
    

    Which yields:

    string(10) "sfdféèî"