I have swiftmailer installed on a Drupal 8 installation. I am using a hook_mail() function to edit the message params as follows:

function mymodule_mail($key, &$message, $params) {
    $options = array(
        'langcode' => $message['langcode'],
    switch ($key) {
        case 'support_form':
            $message['files'] = $params['files'];
        case 'submit_to_admin':
        case 'submit_to_user':
            $message['from'] = array(\Drupal::config('system.site')->get('mail') => 'site admin name');
            $message['subject'] = t('@title', array('@title' => $params['title']), $options);
            $message['body'][] = Html::escape($params['message']);

I am sending the e-mail like this:

$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'mymodule';
$key = 'support_form';
$params['message'] = $message; //defined elsewhere
$params['files'] = $attachments; //defined elsewhere
$params['title'] = $title; //defined elsewhere
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

The e-mail is sending fine, including attachments (so I know Swiftmailer must be working), but when I receive the e-mail the sender is listed as just the e-mail address and not as "site admin name".

Additionally, any & symbol in the body is showing up as &

My swiftmailer install is configured according to this guide.

Is there a setting or code piece I am missing that is making this happen?

1 Answer 1


I had the same issue and did the following to solve the issue with the entities.

$text = $message['body'];
$message['body'] = array_map(function ($text) {
  return Markup::create($text);
}, $text);

This will convert the body and will not display any entites anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.