简体   繁体   English

如何在Drupal 8中发送HTML格式的邮件而不是默认的电子邮件格式

[英]How to send Html formatted mail instead of default email format in Drupal 8

How can we send a html formatted mail in Drupal 8 instead of default format that given by Drupal core. 我们如何以Drupal 8发送html格式的邮件,而不是Drupal核心提供的默认格式。 Such as default email format for user account creation, how can we send this in Html format? 例如用于创建用户帐户的默认电子邮件格式,我们如何以HTML格式发送此格式?

Drupal 8 account creation email format Drupal 8帐户创建电子邮件格式

You can have a look at the HTML Mail module but the drupal 8 version is still in alpha :/ . 您可以看一下HTML Mail模块,但drupal 8版本仍在alpha:/中。

Another simple alternative is to implement hook_mail_alter in order to : 另一个简单的选择是实现hook_mail_alter以便:

  • Set the proper content-type in the message headers. 在邮件标题中设置适当的内容类型。
  • Use the proper formatter to preserve HTML tags (which, by default, would be escaped). 使用适当的格式化程序来保留HTML标记(默认情况下会被转义)。
  • Put HTML tags in email messages without relying on additional module. 将HTML标签放入电子邮件中,而无需依赖其他模块。

The only thing to do now is to write some HTML in the messages, and this should do the job : 现在唯一要做的就是在消息中编写一些HTML,这应该可以完成工作:

/**
 * Implements hook_mail_alter().
 */
function modulename_mail_alter(&$message) {
  switch ($message['key']) {
    case 'register_admin_created':
    #case '<other HTML message id>': 
    #...
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
      foreach ($message['body'] as $key => &$body) {
        $body = new Drupal\Component\Render\FormattableMarkup($body, []);
      }
      break;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM