I'm attempting to send an email from a Drupal 8 module I'm developing, and it works except that special characters (' < >) seem to be getting escaped.
My module has a configuration to select whether the emails are plain text or HTML (using Swiftmailer). In plain text messages, apostrophes get replaced by '
while in the HTML messages, all HTML tags receive similar treatment, so the message arrives, but the HTML tags are all shown in the message, rather than formatting it.
Heres an extract from my hook_mail handler:
$message['subject'] = $params['subject'];
$body = $params['body'];
if ($config->get('confirmation.format_html')) {
// Set up HTML email.
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8';
$message['body'][] = $body; // HTML version of message body.
$message['plain'] = \Drupal\Component\Utility\SafeMarkup::checkPlain($params['body']); // Plain text version of body.
} else {
$message['body'][] = $params['body']; // Plain text version of body.
}
It seems there's some escapement of special HTML characters going on, but I can't see where?
check_plain()
the body field.