如何为电子邮件正文应用模板(tpl)文件

Vik*_*nje 3 php drupal drupal-theming

我在自定义模块中使用此邮件功能

function mymodule_mail($key, &$message, $params) {
  switch ($key) {
    case 'notification':
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
      $message['subject'] = $params['subject'];
      $message['body'] = t('<table style="border:2px solid black;"><tr><td>MESSAGE BODY </td><td><b>'.$params['msg'].'</b></td></tr></table>');
      break;     
  }
}
Run Code Online (Sandbox Code Playgroud)

在这里你可以清楚地看到,对于消息体我正在使用一些html标签.

下面的代码调用mail块函数,它写在我的块中.

$params = array(
      'subject' =>  'email subject',
      'msg' => 'message body',
);
drupal_mail('mymodule', 'notification', 'email address', language_default(), $params);
Run Code Online (Sandbox Code Playgroud)

我想知道,是否有任何简单的方法为我的邮件正文应用模板(.tpl.php)文件,以便我可以将我的所有CSS样式放在该tpl文件中.

任何建议将不胜感激.

jpr*_*itt 6

您需要为它设置主题调用

function mymodule_theme() {
    $path = drupal_get_path('module', 'mymodule') . '/templates';
    return array(
        'mymodule_mail_template' => array(
            'template' => 'your-template-file', //note that there isn't an extension on here, it assumes .tpl.php
            'arguments' => array('message' => ''), //the '' is a default value
            'path' => $path,
        ),
    );
}
Run Code Online (Sandbox Code Playgroud)

现在你已经拥有了它,你可以改变你分配身体的方式

$message['body'] = theme('mymodule_mail_template', array('message' => $params['msg']);
Run Code Online (Sandbox Code Playgroud)

密钥message需要匹配您提供的参数mymodule_theme(),它与之相关.

现在您可以在模块的templates/文件夹中创建your-template-file.tpl.php (您必须这样做)并且您可以使用$message模板中的变量来执行您喜欢的任何操作.变量名称与主题参数名称匹配.

模块设置正确后,请确保刷新缓存.我不能告诉你我多久才意识到我第一次开始使用Drupal,以及我浪费了多少时间来修复不存在的错误.