Search code examples
character-encodingphpmailer

PHPMailer not setting charset in header correctly


Im using PHPMailer 6.1.8 for sending plain text mails. I want to send them with utf8 encoding but in Thunderbird the received eMails still have the wrong content type in header:

Content-Type: text/plain; charset=iso-8859-1

My PHP code:

function sendMail($subject, $body, $to, $from = 'mail@server1.intranet.lan', array $cc = [], array $bcc = []) {
    $mail = new PHPMailer();
    $mail->charSet = 'UTF-8';
    $mail->setLanguage = 'de';

    $mail->isSMTP();
    $mail->Host = '****';
    $mail->SMTPAuth = true;
    $mail->Username = '****';
    $mail->Password = '****';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    
    $mail->setFrom($from);
    $mail->addAddress($to);

    foreach ($cc as $address) {
        $mail->addCC($address);
    }

    foreach ($bcc as $address) {
        $mail->addBCC($address);
    }

    $mail->Subject = $subject;

    $mail->Body = $body;

    $mail->send();
}

Solution

  • PHP is case-sensitive for property names, so change this:

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

    to

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

    You might consider this as a hint to get a decent IDE, since any good one would have flagged this without you even running the code.