簡體   English   中英

如何為電子郵件正文應用模板(tpl)文件

[英]How to apply template (tpl) file for email message body

我在自定義模塊中使用此郵件功能

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文件中。

任何建議將不勝感激。

您需要為它設置主題調用

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/文件夾中創建你的-sink-file.tpl.php(你必須這樣做),你可以在你的模板中使用變量$message來做你想做的事情。 變量名稱與主題參數名稱匹配。

模塊設置正確后,請確保刷新緩存。 我不能告訴你我多久才意識到我第一次開始使用Drupal,以及我浪費了多少時間來修復不存在的錯誤。

HTML Mail模塊就是這樣:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM