phpcharacter-encodingphpmailercharset

PhpMailer accented characters not recognized in external IE application


Good morning, I have a problem with my php page that sends mails using PhpMailer because everything I enter with accented letters when it is sent to the external application, which uses Internet Explorer since it is old, is downloaded with unaccented characters; I guess it is a coding problem and for this I tried to do some tests that I put here below:

$today = date("F j, Y, g:i a"); 
require'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 0;
$mail->SMTPAuth = true; 


$mail->Host = "####";    
$mail->Username = "####";
$mail->Password = "####";
$mail->SMTPSecure = 'ssl';
$mail->Port = ####;
$mail->From = "####";

$mail->FromName = '';
$mail->charSet = "multipart/alternative";
$mail->AddAddress("####");
$mail->Subject = utf8_decode($oggetto);
$mail->Body = utf8_decode($messaggio);

below is the message to be sent to an external application that needs special tags:

    $messaggio = '<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-15">'
. '<!--MF@[WEB]--><strong>ORDINE</strong>&nbsp;&nbsp;&nbsp;num. <!--MF$[NUM]-->' . $numero_ordine . '<!--MF&[NUM]-->&nbsp;&nbsp;&nbsp;del <!--MF$[DAT]-->' . $data . '<!--MF&[DAT]--><br/>'
. '<hr/><br/>'
. 'Azienda: <!--MF$[SOC]-->' . $UserSoc . '<!--MF&[SOC]--> ' . $strSocCts . '<br />'
. $strSdi
. $strInd . '<br/>'
. $strPiv . '<br/>'
. $strCRS . '<br/>'
. $strTel . '<br />'
. $strEma . '<br />'
. '<br />'
. 'Dati recapito:<br />'
. '<hr/>'
. $strRecSoc . '<br/>'
. $strRecInd . $strRecTel . $strRecEma
. '<br /><br />'
. $prodott
. '<hr/>'
. 'Subtotale materiale: ' . fix($total) . ' euro  + Trasporto: ' . $trasporto . ' euro <br />'
. $ivatotale . $sconto . $pagamento 
. 'Vettore: ' . $vettore . '<br />'
. '<hr>'
. $totaletotale
. $annotazioni
. 'Note :&nbsp; <br/><hr>'
. $UserNec . '<br/><br/>';

As tests for encoding I tested these but I only got characters with no accent or "weird" characters.

/**
     * The character set of the message.
     * @type string
     */
    public $CharSet = 'UTF-8';


    /**
     * The MIME Content-type of the message.
     * @type string
     */
    public $ContentType = 'text/plain';

trying to put as charset = ISO-8859-1 or UTF8 but without results.

I just don't understand where I'm wrong. Thanks in advance.


Solution

  • Firstly you're using a very old and unsupported version of PHPMailer, so upgrade.

    Next:

    $mail->charSet = "multipart/alternative";
    

    PHP is case sensitive for property names, so this will fail to set the CharSet property. Even if it did work, multipart/alternative is a MIME type, not a character set name.

    The utf8_decode function converts UTF-8 text into ISO-8859-1, for the set of characters that are common to both character sets. Whether that is appropriate depends on your input string actually being in UTF=8, and the output being expected to be ISO-8859-1; if either of these are incorrect, it will make a mess.

    Generally speaking, you want to use UTF-8 everywhere, and if you do that, all you should need to do is:

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

    and if your content is already in UTF-8 (which it probably is), you can then simply do:

    $mail->Subject = $oggetto;
    $mail->Body = $messaggio;