【问题标题】:Problems theming Drupal 8 + SwiftMailer contact email主题 Drupal 8 + SwiftMailer 联系电子邮件的问题
【发布时间】:2016-08-29 02:33:03
【问题描述】:

我正在尝试使用 swiftmailer 在 Drupal 8 站点中为我的联系电子邮件设置主题,但遇到了一些困难。我正在尝试使用 swiftmailer 的默认主题文件,但它会呈现所有正文。如何单独访问每个字段?

我想做的是这样的:

{#
/**
 * @file
 * The default template file for e-mails.
 *
 * Available variables:
 * - subject: The subject.
 * - body: The message content.
 * - message: The $message array created and used in the mail building
 *   procedure. Its content varies between cases, but typically contains at
 *   least the following elements:
 *   - id: The message identifier.
 *   - module: The module that handles the building of the message.
 *   - key: The key of the message.
 *   - to: The recipient email address.
 *   - from: The email address of the sender.
 *   - langcode: The langcode to use to compose the e-mail.
 *   - params: The message parameters.
 *
 * This template may be overriden by module and/or mail key, using any of the
 * following template names:
 * - swiftmailer.html.twig: global, used by default.
 * - swiftmailer--mymodule.html.twig: only emails sent by the module "mymodule".
 * - swiftmailer--mymodule--test.html.twig: only emails by the module
 *   "mymodule" with key "test".
 *
 * @see template_preprocess()
 * @see template_preprocess_swiftmailer()
 *
 * @ingroup themeable
 */
#}
<html>
<head>
  <style type="text/css">
    table tr td {
      font-family: Arial;
      font-size: 12px;
    }

    td .field {
      width: 50px;
    }
  </style>
</head>
<body>
<div>
  <table width="800px" cellpadding="0" cellspacing="0">
    <tr>
      <td class="field">Nome</td>
      <td>{{ body.name }}</td>
    </tr>
    <tr>
      <td class="field">Email</td>
      <td>{{ body.mail }}</td>
    </tr>
    <tr>
      <td class="field">Motivo</td>
      <td>{{ body.motivo }}</td>
    </tr>
    <tr>
      <td class="field">Titulo</td>
      <td>{{ body.subject }}</td>
    </tr>
    <tr>
      <td class="field">Mensagem</td>
      <td>{{ body.message }}</td>
    </tr>
  </table>
</div>
</body>
</html>

这似乎很简单,但它花费了太多时间。所有“body.something”都是空白的。

谢谢

【问题讨论】:

    标签: drupal themes swiftmailer drupal-8


    【解决方案1】:

    电子邮件的参数通常(当然取决于实现)在message.params 中。我建议你使用message.params 来访问你需要的变量。

    此外,为了提供帮助,您可以使用 devel 模块并使用 {{ dump() }} 打印出该主题文件中可用的所有变量,并从那里发现您的东西在哪里。

    【讨论】:

    • dump(body) 正在工作,但并不真正感兴趣。 dump(message.params) 什么都不显示。
    【解决方案2】:

    变量都列在模板的顶部...

     * Available variables:
     * - subject: The subject.
     * - body: The message content.
     * - message: The $message array created and used in the mail building
     *   procedure. Its content varies between cases, but typically contains at
     *   least the following elements:
     *   - id: The message identifier.
     *   - module: The module that handles the building of the message.
     *   - key: The key of the message.
     *   - to: The recipient email address.
     *   - from: The email address of the sender.
     *   - langcode: The langcode to use to compose the e-mail.
     *   - params: The message parameters.
    

    你会在模板中这样使用:-

    {{ subject }}
    {{ body }}
    {{ message.to }}
    {{ message.params.YOUR_PARAM }}
    

    等等……

    【讨论】:

    • message.params.field_name 对我不起作用。字段都在正文中显示一次,我不知道如何单独访问字段。
    • 您有定义 PARAM 的邮件发送功能吗?
    猜你喜欢
    • 2019-05-18
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2021-05-02
    • 2013-10-06
    相关资源
    最近更新 更多