5

I have installed HTML Mail and Mail System modules and set them up with a template.. this is working fine when I send the test email it uses the template.

However when I send mail from my function, it doesn't use the template.. it was my understanding this should happen automatically, but how can I force it?

I have a mymodule_mail function that just does a switch on $key and fills $message based on various types of messages the system sends.

I call it using drupal_mail().. and that's working, but not loading the template file.

So I added this line above where the mail is being dispatched:

mailsystem_set(array(mailsystem_default_id() => 'HTMLMailSystem'));
drupal_mail('mymodule', 'notification', $email, user_preferred_language($user), $params);

Then I tried moving the mailsystem_set to inside the mymodule_mail function.

and it still isn't working. What am I missing??

drupal_mail has a module parameter that I'm using to call my own module that processes the different templates based on database calls etc. I'm wondering if I have to use that to call HTMLMailSystem, but I need to call my own first. (and still thought using the HTMLMailSystem would be the default)

  • Have you tried to use the hook_mail_alter() function to modify the mail? – Lance Jun 4 '12 at 20:12
  • No..this looks tempting. I know if I rewrite my drupal_mail to call htmlmail instead of mymodule, it uses the template.. the trick is getting it to implement htmlmail via hook_mail_alter – Damon Jun 4 '12 at 20:21
4

The configuration can be done through the Mail System UI. Here is how I send HTML messages with a custom module without a template.

First, install HTML Mail and Mail System as you already have.

Next, create your module and implement hook_mail() and add an identifier (key) to use when sending.

function hook_mail($key, &$message, $params) {
  switch ($key) {
    case 'MODULE_unique_sending_key':
      $message['Reply-To'] = $params['Reply-To'];
      $message['subject'] = $params['subject'];
      $message['body'] = $params['body'];
      $message['headers']['Reply-To'] = $params['Reply-To'];
      break;
    }
}

Next, implement drupal_mail().

$key = 'MODULE_unique_sending_key';
$message = drupal_mail('MODULE', $key, $to, LANGUAGE_NONE, $params, $from);

Lastly, you will need to tell Drupal to use the HTMLMailSystem to send when using the 'MODULE_unique_sending_key' key.

To do this, go to admin/config/system/mailsystem and go to 'New Setting.'

Under module, choose your custom module.

Under Key, enter your unique message identifier you used in drupal_mail().

Mail Settings

Now, while still on the admin/config/system/mailsystem page look under 'MAIL SYSTEM SETTINGS' look for the Module+Key combination you just created.

From here, you will need to select the HTMLMailSystem as your sending class. Save and test your module. HTML mail should now be working without using a template.

Choose HTMLMailSystem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.