I have a module that uses hook_menu to create several pages that are populated with SQL query results.

I have been trying to configure drupal_mail and hook_mail to send these results via mail when the pages are visited, but so far I haven't succeeded.

I used the info I found here and indeed, the module works: whenever I run cron the sample mail is sent, but I haven't found the way to customize it in my module so when I load my custom pages the results are sent via mail.

Also, if my data is the result of:

function mystats_all() {

$header = array('Nombre', 'Status', 'Rol 5 = Alumno', 'Email' );

$query = "
SELECT name, status, rid, mail
FROM users
JOIN users_roles ON users.uid = users_roles.uid
WHERE mail NOT LIKE '%@example.com' 
AND users_roles.rid =5
AND users.status =1

$result = db_query($query, $some_value);
    while ($row = db_fetch_object($result))
        $rows[] = array(
                    array('data' => $row->name, 'align' =>'left'),
                    array('data' => $row->status, 'align' => 'left'),
                    array('data' => $row->rid, 'align' => 'center'),    
                    array('data' => $row->mail, 'align' => 'left'),    


    $table_attributes = array('id' => 'my_id_for_table', 'align' => 'center');
    $output .= theme('table', $header, $rows, $table_attributes);

    return $output; 

how can I correctly send my table via mail? I don't know (and I would like to have the drupal_mail function working to make some tests) if I should use $query or $result.

Thanx in advance.

Any ideas or help, please?

1 Answer 1


You need to implement hook_mail() in your custom module and then call drupal_mail() in your page callback function to get the result you want. You should save your table html in the $params array that gets delivered to hook_mail() and then set it as the $message['body'] value there (with additional html/data if needed). The linked tutorial and API pages should give you enough information and examples to work out the rest of the details.

  • 1
    This is pretty much how I made it work, Dooshta. My problem now is that I have several queries in this module and I would love to be able to trigger the mails independently when you visit each url generated via hook_menu.
    – MauF
    Commented Feb 8, 2012 at 22:00
  • @MauF If the page callback functions are different for every defined menu item then you can run the necessary queries and send the specific e-mail(s) depending of the page visited (or am I not quite understanding what you want to achieve?).
    – Madis
    Commented Feb 8, 2012 at 22:38
  • Yes, I do have specific callbacks. This means I have to code a specific hook_mail for every callback, right?
    – MauF
    Commented Feb 9, 2012 at 15:40
  • @MauF You can only have one hook_mail() function, but you can define different e-mail implementations within it. drupal_mail() also allows you to deliver data to hook_mail() (via the $params array) so the mail content/title can differ depending of the data sent. You can check the tutorial you linked about how it's done on the code level.
    – Madis
    Commented Feb 9, 2012 at 18:18
  • Will do, Dooshta. Thanx.
    – MauF
    Commented Feb 10, 2012 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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