Support » Plugins » Hacks » Wp_mail function: email subject with multibyte chars is not encoded properly

  • Hi all,

    I’ve been on this issue for a few days now and i’ve manage to fix it, so i guess it is more of the reminder to wordpress team than a cry for help 🙂

    The issue is as follows: when a new user comes and registers a new blog, he gets an email with his new blog name in a subject, like this:

    New site_name Site: New blog name

    So when he puts any multibyte character in his blog name (like é, å, etc. which are plenty of in German or Swedish for example), the subject becomes all messy, like this:

    =?UTF-8?Q?New_site_name_Site:_J=C3=A9t_inqsfzxb_p=C3=A5_?= =?UTF-8?Q?=C3=A9?=

    I’ve digged around this for quiet a while and found following things:

    • The issue only occurs when you have you mbstring.func_overload setting set to 7 (which is to overload all single-byte functions with their multi-byte analogs). Setting mbstring.func_overload = 0 solves the problem. But in my case it was not an option on the production server so i had to find something else
    • The issue comes from PHPMailer class which has some architectual errors in it. For instance, it uses both multibyte and singlebyte string functions in the same areas of code. But it is not the point.
      The point is it uses standard php mail() function to send an actual email. Before the sending, the class uses it’s EncodeHeader function to ecode the subject of an email as a header. When the single-byte variation of mail() function is used, it works perfectly well, but when it is overloaded with it’s multibyte analog, it spoiles the subject. This happens simply because the mb_send_mail() ecnodes the subject and the messages itself. So basically the subject is first encoded by EncodeHeader function and then – by mail() function itself. As a result we have double-encoded subject that looks like i’ve mentioned above.
    • If you remove the EncodeHeader call on subject (which is right before the call to actual mail() function), this fixes the problem, and fixes it not depending on the installation (both mbstring.func_overload = 7 and = 0 work well)

      $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);

      (this is around line 660 in /wp-includes/class-phpmailer.php in my installation)

    • This issue will occur not only when registering a new blog, but anywhere wordpress tries to send email with the multibyte character in the subject.

    In my situation i can’t hack the wordpress core, i can only add plugins and themes so i’ve come up with two possible fixes:

    • Remove all the possible multibyte characters from the subjects 🙂 This is not really a fix though
    • As wp_mail function is pluggable, we can overload it with our own one by creating a plugin with a new wp_mail function, which is forced to use another PHPMailer class. Then we just create a new PHPMailer class, remove the EncodeHeader from where it is not needed (described above) and it works.
      The problem is that in WP 3.0.4 you could just extend the PHPMailer class, overwrite one function and use it. Now, in WP 3.2.1 the PHPMailer::AddAddress function is made private, so i couldn’t extend it and had to copy the whole class. Moreover, each time i update wordpress, i have to check if this plugin still works and change it accordinly

    I don’t know if wordpress team has any public bugtracker, so i’ve decided to post it here. If anyone has any contact with them, please tell them about this issue, coz it it really annoying.
    Hope this helps anyone who faced the same problem.

  • The topic ‘Wp_mail function: email subject with multibyte chars is not encoded properly’ is closed to new replies.