Home » Php » php – Joomla UTF-8 encoding fails on opening the mail

php – Joomla UTF-8 encoding fails on opening the mail

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have a strange issue with encoding, described as follows:

the ù is now shown as ù in the email subject. The email is sent through php mail function.

When viewing the e-mail in the mailbox, it is shown correctly. However, when anybody opens the e-mail, the ù is suddenly changed to ù.

Uw contact met Meeùs

should be

Uw contact met Meeùs

i have already used the encoding.

$emailsubject contains the above mentioned email subject.

$subject=$emailsubject;
$subject=$emailsubject;
$email_message=new email_message_class;
$email_message->SetEncodedEmailHeader("To",$to_address,$to_name);
$email_message->SetEncodedEmailHeader("From",$from_address,$from_name);
$email_message->SetEncodedEmailHeader("Reply-To",$reply_address,$reply_name);
$email_message->SetHeader("Sender",$from_address);
$email_message->SetEncodedHeader("Subject",$subject,"UTF-8");

In localhost it is working properly, but in the webserver it is not working properly. In webserver also encoding is set to utf-8 by default.

What i am doing wrong?
Thanks in advance.

How to&Answers:

Your code is correct absolutely there is no error in it but its other things failing encoding. As I need message source headers and message to tell you exactly what is happening? I have further no information about are you sending the email as plain text or HTML. But there are generally two issue which are:

Missing Mime-Version

Reason for showing the character wrongly is developers forget to describe the message as MIME Version. if the message is missing the “Mime-Version” header that Internet mail standards require, Webmail will ignore the “charset” header completely, garbling the message unless it’s already in the UTF-8 character set.

Showing Subject with Special Characters

As you want to show the subject with utf-8 encoding then you must encode the subject as:

//Setting the Language as Japan
mb_language("ja");

//Converting the string into Japan Encoding
$subject = mb_convert_encoding($subject, "ISO-2022-JP","AUTO");

//Now convert the string to MIME Header type
$subject = mb_encode_mimeheader($subject);

If the above mentioned things doesn’t resolve the problem then request you post the RAW Headers of the Email as it will help in better way to resolve issue.

Answer:

Are you test to change the charset with .htaccess ?

AddDefaultCharset   UTF-8

Answer:

Since you indicate in the comments you are using Joomla 1.5, it seems there is an issue with the phpmailer() library in that version that forces the character set of the mailer—on the message—to send things out using the character set setting of iso-8559-1. To fix this open up the core phpmailer() libary here:

[path to your Joomla install]/libraries/phpmailer/phpmailer.php

Around line 50 there is a setting called $CharSet. Change that to utf-8 if it’s not set to that already:

  /**
   * Sets the CharSet of the message.
   * @var string
   */
  var $CharSet           = 'utf-8';

You might also want to do search of your Joomla 1.5 codebase for iso-8559-1 to see if a component or library is forcing iso-8559-1 encoding somewhere in the chain of code.

And another setting I would recommend checking is $Encoding around line 63. The default setting seems to be 8bit, but I have had to adjust that in the past to either quoted-printable or base64 to solve some mailing issues on specific setups I was working on.

  /**
   * Sets the Encoding of the message. Options for this are "8bit",
   * "7bit", "binary", "base64", and "quoted-printable".
   * @var string
   */
  var $Encoding          = '8bit';

Answer:

I suggest you to use joomla mailer class, the could would look like this:

$mailer = JFactory::getMailer();
$mailer->setSender(array($from_address,$from_name));
$mailer->addRecipient($to_address, $to_name);
$mailer->setSubject($subject);
$mailer->setBody("BODY MESSAGE STRING");
$mailer->Send();

It’s utf8 by default, and i don’t see any reasons for not to use it, if you’re using Joomla.