12

I need to generate German e-mails which contain umlaut characters. In the e-mail itself this works perfectly, but not in the subject of the e-mail. I've tried many different umlaut letters and they all seem to work except for the ü. I also tried different mail libraries (HTMLMimeMail & PHPMailer) and they both fail at this:

$mail = new htmlMimeMail();
$mail->setTextEncoding("base64");
$mail->setHTMLEncoding("base64");
$mail->setTextCharset("UTF-8");
$mail->setHTMLCharset("UTF-8");
$mail->setHeadCharset("UTF-8");
$mail->setSMTPParams(mailinglist_smtp_host,mailinglist_smtp_port);
$mail->setHtml("test");
$mail->setFrom("test@test.nl");    

$mail->setSubject("The ï, ö, ë, ä, and é work, but when adding the ü it doesn't");

$recipients[] = "me@myhost.nl";    
$mail->send($recipients);

&

$mail = new PHPMailer();
$mail->IsMail();
$mail->FromName = 'test';
$mail->From = 'test@test.nl';
$mail->AddAddress("me@myhost.nl");
$mail->Subject = "The ï, ö, ë, ä, and é work, but when adding the ü it doesn't";
$mail->Body = "test";    
$mail->Send();

Can anyone help me find the source of and the solution to this problem?

Sander
  • 339
  • 1
  • 3
  • 11

1 Answers1

13

You should quoted-printable encode the subject header.

Like this:

$mail->Subject = "=?UTF-8?Q?" . quoted_printable_encode("The ï, ö, ë, ä, and é work, but when adding the ü it doesn't") . "?=";

Quoted printable encode in PHP: http://www.php.net/manual/en/function.quoted-printable-encode.php

Edit: $mail->CharSet = "UTF-8"; did the job.

  • Thanks, but this didn't solve it. The subject now turned into: =?UTF-8?Q?The =C3=AF, =C3=B6, =C3=AB, =C3=A4, and =C3=A9 work, but when adding the ==C3=BC it doesn't?= – Sander Nov 30 '12 at 13:43
  • 4
    Hmm maybe it's enough to add `$mail->CharSet = "UTF-8";`, and remove the quoted-printable stuff. Try it and let me know how it works! – Timothy E. Johansson Nov 30 '12 at 13:52
  • Super, that did the job for the PHPMailer :). Thanks. Now as you can see in my mimemail code, I tried an option similar to this one, but I apparently missed the right setting. Do you perhaps know what I need to set in mimemail to apply the same fix? – Sander Nov 30 '12 at 14:04
  • Sweet! I would recommend you to also use PHPMailer to build the MIME message, instead of htmlMimeMail. Else I cannot see what the problem is. – Timothy E. Johansson Dec 01 '12 at 23:46
  • Another solution is to check out [AlphaMail](http://comfirm.se), which takes care of all this for you :) – Timothy E. Johansson Dec 11 '12 at 00:20
  • the "Edit" was great! – Amir Koklan Jan 07 '16 at 13:48
  • In my case utf8_encode($subject) did the job which is basically the same as $mail->CharSet="UTF-8" I guess... – Yasen Jan 17 '19 at 09:10