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

时间:2011-10-13 16:28:47

标签: 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;     
  }
}

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

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

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

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

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分: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,
        ),
    );
}

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

$message['body'] = theme('mymodule_mail_template', array('message' => $params['msg']);

message需要匹配您在mymodule_theme()中提供的参数。

现在你可以在模块的templates/文件夹中创建你的-codule-file.tpl.php(你必须这样做),你可以在你的模板中使用变量$message来做你想做的事。变量名称与主题参数名称匹配。

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

答案 1 :(得分:0)

HTML Mail module就是这样: - )

相关问题