开发者

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

I've this mail function in my custom module

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;     
  }
}

Here you can clearly see that for message body i'm using some html tags.

Below code invoke the mail function, which is 开发者_如何转开发written in my block.

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

I want to know, is there any easy way to apply a template (.tpl.php) file for my message body so that i can put my all css styling within that tpl file.

Any suggestion would be greatly appreciated.


You'll need to set up a theme call for it

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,
        ),
    );
}

Now that you have that, you can change the way you're assigning the body

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

The key message needs to match the argument you supplied in mymodule_theme(), which it does.

Now you can just create your-template-file.tpl.php in the module's templates/ folder (you'll have to make that) and you can use the variable $message in your template to do whatever you'd like. The variable name matches your theme argument name.

After your module is set up properly, make sure to flush the cache. I can't tell you how long it took me to realize that the first time I started working with Drupal, and how much time I wasted trying to fix non-existent bugs.


The HTML Mail module does just that :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜