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('firstname.lastname@example.org') ->setTo('email@example.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?
Try to escape the subject with the htmlentities PHP function:
$subject = htmlentities("This is my subject with an apostroph ' in it.");