How to handle special chars in email subject?

I am using the Swiftmailer in my Symfony2 webapp.

// Subject and body dynamically come from database
$subject  = "This is my subject with an apostroph ' in it.";
$bodytext = "Test text, also with an ' apostrophe in it.";

$message = \Swift_Message::newInstance()
             ->setSubject($subject)
             ->setFrom('me@mail.com')
             ->setTo('you@mail.com');

$message->setBody($bodytext);

$this->get('mailer')->send($message);

Now when there is a special char, e.g. the apostrophe (') in my subject, the email has a strange subject line in my email client:

This is my subject with an apostroph ' in it

Funny thing: The body text is displayed correctly, it's only the subject that's wrongly formatted.

Now how can I handle special chars like this - and even better, is there a function I can call before passing the subject that handles special chars in general?

Answers


Try to escape the subject with the htmlentities PHP function:

$subject  = htmlentities("This is my subject with an apostroph ' in it.");

Need Your Help

PHP: Convert hex color from color input to PHP hex number

php colors hex color-picker

I want to convert my hex color from a color input (example: #FFFFFF) to a PHP hex number format (example: 0xFFFFFF).

sun.java2d.loops.ProcessPath$Point

java memory loops profiling point

I am profiling an application suddenly using a lot of memory, and i am getting this: