Web Hosting Forum | Lunarpages

Author Topic: Ordinary character sets  (Read 6291 times)

Offline Bandi

  • Space Explorer
  • ***
  • Posts: 7
Ordinary character sets
« on: October 15, 2017, 05:02:53 PM »
My problem is related to PHPMailer script,

UTF-8, iso-8859-1, iso-8859-2 character sets all have accented letters such as ü and Ü (I use an Hungarian keyboard)
But when I specify e.g.

$mail = new PHPMailer();
....
$mail->CharSet = 'iso-8859-2';
....
echo "Message has been sent ~ Üzenet elküldve.";

PHPMailer writes: Message has been sent ~ Üzenet elküldve

Debugging client/server conversation indicates
CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-2

Result is the same when specifying any of the above 3 character sets or when no character set is specified.
Yet actual message content with accented letters arrives at destination correctly.
 
I just don't get it. What default character set is used by PHP? How do I reset it to any of the above mentioned character sets?

I use iso-8859-2 for html programs all the time on my webpages. No problems. So I thought, to go around this problem, I could write an html file that would display the correct accented characters.

But I do not know enough about calling an html file from PHP and passing a variable to it. I saw examples of embedding the php echo command in html file but I guess it will not work since it already does not work inside the PHPMailer script file...

Any suggestions? I know very little php, but can follow examples

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6236
Re: Ordinary character sets
« Reply #1 on: October 16, 2017, 04:56:34 AM »
I don't know PHPMailer, but the fact that single accented characters are expanded to pairs of characters suggests to me that your original source was entered in UTF-8 encoding. Look at your PHP program in some editor mode that will show a single-byte encoding (e.g., Latin-1 or CP-1252) and confirm that it's pairs of bytes. Are you using exactly the same editor that you use to create HTML files? Could it be switching encodings depending on whether you are editing an .html file or a .php file? Just for fun, try naming your PHP file as .html, and see if the appearance changes. Try writing the PHP code with an .html file extension (with ISO-8859-2 encoding), and then rename to .php when you're ready to run it.

We've got to see if your .php file is being entered in UTF-8 rather than, say, ISO-8859-2, and if so, how to control the editor's encoding used. Your PHP code needs to be entered in ISO-8859-2 encoding for it to work with the encoding declared to PHPMailer.
Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-

Offline Bandi

  • Space Explorer
  • ***
  • Posts: 7
Re: Ordinary character sets
« Reply #2 on: October 24, 2017, 12:34:18 AM »
What you said made sense.

Using  Charset Decoder at  http://www.webatic.com/run/chars/charset_decoder.php,   I tested encoding of accented characters by  UTF-8 and decoding same by using  iso-8859-2.

It proved that in my setup PHPMailer uses UTF-8 characters writing to my Windows 7 set to iso-8859-2 (default input language Hungarian).

Knowing unfortunately is not the same as making it work.

I originally  wrote the PHPMail script with NotePad in Windows 7  set to iso-8859-2.  I now tried writing the PHPMail code in HTML editor set to iso-8859-2. It did not change the outcome. Accented letters still come out as garbage in echo "Message has been sent ~ Üzenet elküldve.";

While searching through half of Internet, I found what may solve my need.
/* Set internal character encoding to iso-8859-2*/
mb_internal_encoding("iso-8859-2");

This command is not enabled by default, it must be installed. However, all my searches did not turn up an example for how I could install it.

Could you or someone help me out?

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6236
Re: Ordinary character sets
« Reply #3 on: October 24, 2017, 06:22:53 AM »
You should not have to install anything. It should be built into PHP, unless your server is unusual or severely backlevel. See http://php.net/manual/en/function.mb-internal-encoding.php which suggests it may have no effect on PHP 5.6 and higher.

Note that you need to know (and control) what your original source is entered in -- the actual byte stream making up your text message. I don't think it's going to do any good to actually enter it in UTF-8, and then use mb_internal_encoding() to declare that it's ISO-8859-2 -- it won't magically change the bytes in your code. You have to find a way to type in your code in ISO-8859-2 in the first place. You might first try mb_internal_encoding('UTF-8'), but you might have to declare the output encoding too (as ISO-8859-2). That might translate the text on the way out, but I can't promise anything.

I think I've seen command-line utilities to convert a file from one encoding to another. It might be easiest to take your UTF-8 source file and run it through such a utility to turn it into ISO-8859-2.
Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-

 

Share |