Posts Tagged ‘utf-8’

PHPMailer send mail in UTF-8 encoding

Monday, July 4th, 2011

PHPMailer does not seem to auto-detect encodings, so if you wish to send mail in (for instance UTF-8) you need to tell PHPMailer this. Thankully that is very easy

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

PHP Send a mail as UTF-8

Tuesday, March 1st, 2011

To send a mail from PHP encoded as UTF-8 is fairly easy to do by modifying the headers.

//Sender with special chars (UTF-8) encoded
mail('mail@example.com', '=?UTF-8?B?'.base64_encode($subjectString).'?=', $messageString, "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n");
//Sender not UTF-8 encoded
mail('mail@example.com', 'sender@mail.com', $messageString, "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n");

HTML: UTF-8 encoding

Wednesday, July 14th, 2010

To get a html page to display UTF-8 encoded text correctly (without setting a default charset) simply add the following to the head of the html page.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

 at the top of the page (UTF-8 BOM)

Wednesday, April 14th, 2010

These characters are the Byte Order Mark (BOM) of the Unicode Standard.

This can be solved in two ways:

  • Save the file without the BOM (some editors have this as an advanced option)
    • Using vi:
       :set nobomb

      and then save the file

  • Make sure the right encoding is used to present the file (meta charset) / .htaccess or similar

Magento: Send åäö (and other international characters) to DIBS

Tuesday, February 9th, 2010

If your store uses DIBS to manage payments, then the risk is that you have to worry about characters like åäö.

What we had to do is to make the form post the data as ISO-8859-1.
We did this by adding accept-charset=”iso-8859-1″ to the forms that we send to DIBS.

In “/app/design/frontend/default/blank/template/dibs/standard/redirect_paymentwindow.phtml”

56
<form action="https://payment.architrade.com/paymentweb/start.action" method="post" name="dibs" id="dibs" accept-charset="iso-8859-1">

Once we had done this then the order details were shown correctly on the DIBS page (yes, the Chinese chars are just a test, I have no idea what it might say)
Details on DIBS including åäö

Linux: Converting a file encoded in ISO-8859-1 to UTF-8

Tuesday, February 9th, 2010

If you have a file that is saves as ISO-8859-1 (or ISO-LATIN-1 if you like to call it that) and wish to convert it to UTF-8 you can use:

 iconv --from-code=ISO-8859-1 --to-code=UTF-8 ./oldfile.htm > ./newfile.html

This will create a new file with the converted encoding.

iconv can of of course convert to and from several other charsets. To see a list of all the encodings that iconv can work with use:

 iconv -l

If you wish to massconvert files find can be used with exec

 find . -name "*.txt" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o {}.utf8 \;

Apache2 changing charset using .htaccess

Thursday, January 14th, 2010

If you do not wish to change character encoding for the entire server, but only one site (or only one directory) then this is possible to do using .htaccess.

AddDefaultCharset UTF-8

If you only wish to do this to php or htm files (and not all files)

<FilesMatch "\.(htm|php)$">
AddDefaultCharset UTF-8
</FilesMatch>

If you wish to modify the mime-type as well as the encoding (on html files in this example)

AddType 'text/html; charset=UTF-8' html