Character Sets

Zend_Mail does not check for the correct character set of the mail parts. When instantiating Zend_Mail, a charset for the e-mail itself may be given. It defaults to iso-8859-1. The application has to make sure that all parts added to that mail object have their content encoded in the correct character set. When creating a new mail part, a different charset can be given for each part.

Note: Only in text format
Character sets are only applicable for message parts in text format.

Example #1 Usage in CJK languages

The following example is how to use Zend_Mail in Japanese. This is one of CJK (aka CJKV ) languages. If you use Chinese, you may use HZ-GB-2312 instead of ISO-2022-JP.

  1. //We suppose that character encoding of strings is UTF-8 on PHP script.
  2. function myConvert($string) {
  3.     return mb_convert_encoding($string, 'ISO-2022-JP', 'UTF-8');
  4. }
  5.  
  6. $mail = new Zend_Mail('ISO-2022-JP');
  7. // In this case, you can use ENCODING_7BIT
  8. // because the ISO-2022-JP does not use MSB.
  9. $mail->setBodyText(
  10.     myConvert('This is the text of the mail.'),
  11.     null,
  12.     Zend_Mime::ENCODING_7BIT
  13. );
  14. $mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
  15. $mail->setFrom('somebody@example.com', myConvert('Some Sender'));
  16. $mail->addTo('somebody_else@example.com', myConvert('Some Recipient'));
  17. $mail->setSubject(myConvert('TestSubject'));
  18. $mail->send();
blog comments powered by Disqus