2011-10-13 52 views
3

我在自定義模塊如何應用模板(TPL)文件電子郵件正文

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標籤已經這封郵件功能。

下面的代碼調用郵件函數,它寫在我的塊中。

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

我想知道,有沒有應用模板(.tpl.php)文件爲我的郵件正文,這樣我可以在TPL文件中把我所有的CSS樣式沒有簡單的方法。

任何建議將不勝感激。

回答

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/文件夾模板 - file.tpl.php(你將不得不做出這樣的),你可以使用變量$message在您的模板做任何你想。變量名稱與您的主題參數名稱相匹配。

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

+1

+1不錯的簡單方法,節省另一個contrib模塊的開銷 – Clive

+0

非常感謝。我同意@clive –