Score:1

How can I add HTML links in e-mails?

za flag
          $content_title = $translated_entity->getTitle();
          $url_alias = \Drupal::service('pathauto.alias_cleaner')
            ->cleanString($content_title);
          $content_link = <<<link
    <a href='/publications/$url_alias'>$url_alias</a>
    link;
          $params['node_title'] = 'Chaire ANMT – Nouvelle Publication : ' . $entity->label() . '';
          $params['message'] = <<<msg
    Nouvelle publication : $content_title $content_link. \n
    msg;
//some code
  $result = $mailManager->mail($module, $key, $subscriber, $langcode, $params, null, $send);

The email generated do not place the link next to the content title. How to fix that?

enter image description here

Score:4
us flag

That is expected, when the Drupal mail back-end that comes with Drupal, implemented by the PhpMail class, is used. In fact, its format() method contains the following code.

  // Join the body array into one string.
  $message['body'] = implode("\n\n", $message['body']);

  // Convert any HTML to plain-text.
  $message['body'] = MailFormatHelper::htmlToText($message['body']);

  // Wrap the mail body for sending.
  $message['body'] = MailFormatHelper::wrapMail($message['body']);

MailFormatHelper::htmlToText() is the method that changes the HTML markup as you see.

The only way to avoid that is to use a different mail back-end or implement one in a custom module.

Matoeil avatar
za flag
what is the simplest alternative plugin/service that allows HTML in emails ?
apaderno avatar
us flag
For Drupal 9.4/10, I would say that is the [Drupal Symfony Mailer](https://www.drupal.org/project/symfony_mailer) module. I have never used it, but its [documentation page](https://www.drupal.org/docs/contributed-modules/drupal-symfony-mailer) says it fully supports HTML mails.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.