0

I'm using htmlmail module. I'm trying to send an email after node save. So I put drupal_mail() into hook_node_insert() or hook_node_presave(), but when I'm doing this, the body of my emailes are empty.

The source looks literally like this:

<div class="htmlmail-body">
</div>

The templates are not processed. The hook_preprocess_htmlmail() function in template.php is not beeing invoked. I'm using different templates for different mail keys.

Now the funny thing is that when I'm using the same drupal_mail() in hook_cron() it just works as expected. I have no idea how I can debug this. Any ideas (for example where to put a debuggers break for further diagnosis)?

Added #1: It appears that only htmlmail.tpl.php is being used instead of my module and key specific templates that works when I'm sending mails from cron.

Added #2: Code in hook_cron() and in hook_node_presave() is the same:

$receipents = array( variable_get('site_mail', 'example@example.com') );
$params = array();
drupal_mail('bonds_cron', "create_payments__client", implode(', ', $receipents), language_default(), $params, variable_get('site_mail', 'example@example.com'), true);

The difference is that in hook_cron() it works, and in hook_node_presave() it doesn't. I was trying to change double "__" into one, thinking that maybe it's something with picking up templates, but without success. I'm searching now for place in core code, when I could put a break to see what is going on when picking up those templates but I cannot find it. :/

Added #3: template_preprocess_htmlmail() run just fine. The proper template suggestion is added there.

Added #4: I added var_dump($items) before the end of htmlmail_theme() function. When clearing cache I got this output:

array(11) {
  'htmlmail__bonds-cron__create_payments__admin' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(44) "htmlmail--bonds-cron--create_payments__admin"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__bonds-cron__create_payments__agent' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(44) "htmlmail--bonds-cron--create_payments__agent"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__bonds-cron__create_payments__client' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(45) "htmlmail--bonds-cron--create_payments__client"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__bonds-cron__payments_alert__admin' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(43) "htmlmail--bonds-cron--payments_alert__admin"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__bonds-cron__payments_alert__agent' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(43) "htmlmail--bonds-cron--payments_alert__agent"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__bonds-cron__payments_alert__client' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(44) "htmlmail--bonds-cron--payments_alert__client"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__bonds-cron__tax_alert__admin' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(38) "htmlmail--bonds-cron--tax_alert__admin"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__htmlmail' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(18) "htmlmail--htmlmail"
    'path' =>
    string(26) "sites/all/modules/htmlmail"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__simplenews' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(20) "htmlmail--simplenews"
    'path' =>
    string(26) "sites/all/modules/htmlmail"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail__user__password_reset' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(30) "htmlmail--user--password_reset"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
  'htmlmail' =>
  array(4) {
    'variables' =>
    array(1) {
      'message' =>
      array(0) {
        ...
      }
    }
    'template' =>
    string(8) "htmlmail"
    'path' =>
    string(35) "sites/default/themes/bond/templates"
    'theme path' =>
    string(25) "sites/default/themes/bond"
  }
}

Seems ok for me.

  • Can you add the code you're using, that's not working, to send the email in the node api hooks. Otherwise impossible to debug. – David Thomas May 19 '14 at 22:31
  • Yes please post the actual code as well. It sounds like you are not implementing hook_mail which is essential to set the body, subject and other parameters if the email. – AyeshK May 20 '14 at 2:33
  • I added more code, but there is not much to look at. – Łukasz Zaroda May 21 '14 at 10:50
0

Ok, after digging VERY deep in theme.inc code I found the problem. I named my templates "htmlmail--bonds-cron--create_payments__client" and I should "htmlmail--bonds_cron--create_payments__client" (notice underscore instead of dash). Replacing underscore with dash made anomalies in ThemeRegistry storage, probably caused by string translations (of template names) in theme.inc . The consequence was this very strange different drupal_mail behaviour in different hooks. I hope it will help someone.

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.