Khriz
Khriz

Reputation: 5998

Is there a way to change the encoding of the headers in SwiftMailer?

I'm using SwiftMailer to send emails but I have some codification problems with UTF-8 subjects. Swiftmailer uses QPHeaderEncoder as default to encode email headers and the safeMap looks like it has some problems with some UTF-8 French characters. One subject I use contains the word trouvé (found in French) and when the subject gets to the user it shows trouv.

I'd like to use something similar to the NativeQPContentEncoder that's available as content encoders but for headers there's only Base64 and Quoted Printable encoders.

Is there a way to fix this, maybe I'm doing something wrong so I paste the code I'm using here

$message = Swift_Message::newInstance()

// set encoding in 8 bit
->setEncoder(Swift_Encoding::get8BitEncoding())

// Give the message a subject
->setSubject($subject)

// Set the From address with an associative array
->setFrom(array($from => $niceFrom))

// Set the To addresses with an associative array
->setTo(array($to)) ;

Upvotes: 5

Views: 4518

Answers (2)

cronfy
cronfy

Reputation: 1066

Check if in your PHP configuration mbstring.func_overload option has any value other than 0. If yes, change it to 0, reload your webserver and try to send message again.

mbstring.func_overload overrides some string PHP functions and may lead to tricky bugs with UTF-8.

Personally I solved exactly this problem by disabling mbstring.func_overload.

Upvotes: 2

Ares
Ares

Reputation: 5903

First, make sure you know how is your subject string encoded. If it is not UTF-8 then utf8_encode() it.

Also, make sure you setCharset('utf-8') your message.

Upvotes: 0

Related Questions