Setting the Character Set

The character set of the message (and it's MIME parts) is set with the setCharset() method. You can also change the global default of UTF-8 by working with the Swift_Preferences class.

Swift Mailer will default to the UTF-8 character set unless otherwise overridden. UTF-8 will work in most instances since it includes all of the standard US keyboard characters in addition to most international characters.

It is absolutely vital however that you know what character set your message (or it's MIME parts) are written in otherwise your message may be received completely garbled.

There are two places in Swift Mailer where you can change the character set:
  1. In the Swift_Preferences class
  2. On each individual message and/or MIME part

To set the character set of your Message:

  • Change the global UTF-8 setting by calling Swift_Preferences::setCharset(); or
  • Call the setCharset() method on the message or the MIME part.

Setting the Charset

//Approach 1: Change the global setting (suggested)
Swift_Preferences::getInstance()->setCharset('iso-8859-2');

//Approach 2: Call the setCharset() method of the message
$message = Swift_Message::newInstance()
  ->setCharset('iso-8859-2');
  
//Apprach 3: Specify the charset when setting the body
$message->setBody('My body', 'text/html', 'iso-8859-2');

//Approach 4: Specify the charset for each part added
$message->addPart('My part', 'text/plain', 'iso-8859-2');

NOTE: Documentation for version 3 is in the wiki.