Home > Net >  Sending emojis via SwiftMailer
Sending emojis via SwiftMailer

Time:11-24

I would like to send newsletters via SwiftMailer, but there should be an emoji in the subject of the email. I think the right charset is utf8mb4 but how can I change the charset for the subject?

It worked for the body of the Swift_Message Class because I defined the Content-Type like following:

$message->setBody($html, 'text/html');

CodePudding user response:

PHP 7.0 introduced the Unicode codepoint escape syntax.

This takes a Unicode codepoint in hexadecimal form, and outputs that codepoint in UTF-8 to a double-quoted string or a heredoc. Any valid codepoint is accepted, with leading 0's being optional.

Example to send a subject line with a checkmark ✅ which is Unicode U 2705:

$subject =  "\u{2705}Swift Mailer Dingbat Test Email";

$message = (new Swift_Message($subject))
        ->setFrom([$mail_to_address])
        ->setTo([$mail_from_address])
        ->setBody($html_body, 'text/html');
  • Related