2

if this is my function:

<?php
//dsm($query);


function customstatsB_menu() {
  $items['customstatsB'] = array(
    'title' => 'Maestros Inactivos + email',
    'page callback' => 'customstatsB_all',
    'access arguments' => array('access customstatsB content'),
    'type' => MENU_CALLBACK
  );
  return $items;
}
/**
* Valid permissions for this module
* @return array An array of valid permissions for the customstats module
 * No repetir permisos, este es únicamente un modulo, y cada query NO necesita permisos. PERMISOS A NIVEL MODULE x ROL.
 */

function customstatsB_perm() {
  return array('access customstatsB content');
} // function customstats_perm()

//return array('access customstats content', 'administer customstats');

function customstatsB_all() {

/**
 * MAESTROS INACTIVOS 
 */

$query = "
SELECT name, status, rid, mail
FROM users
JOIN users_roles ON users.uid = users_roles.uid
WHERE mail NOT LIKE '%@hotmail.com' 
AND users_roles.rid =6
AND users.status =0
";

  // get the links (no range limit here)
  $queryResult =  db_query($query);
  while ($links = db_fetch_object($queryResult)) {
    //$page_content .= l($links->name, 'node/'.$links->mail) . '<br />';
    $page_content .= 
    l($links->name, 'mailto:'.$links->mail) . '<br />';
  }

  // check to see if there was any content before
  // returning the page
  if ($page_content == '') {
    // let the user know there is no content to show
    $page_content = "No existe información para mostrar.";
  }

  return $page_content;
}

return array('access customstatsB content', 'administer customstatsB');

/**
 * Email configs 
 */




function customstatsB_mail($key, &$message, $params)
  {
     $params = array(
        'subject' => t('SQL Results Tester'),
        'body' => t("Body of the email goes here"),
      );

      drupal_mail("customstatsB", "customstatsB_normal_mail", "my@mail.net", language_default(), $params, "admin@mysite.com");
      drupal_mail("customstatsB", "customstatsB_html_mail", "my@mail.net", language_default(), $params, "admin@mysite.com");
  }
      $language = $message['language'];
      switch ($key)
      {
           case 'customstats_normal_mail':
              $message['subject'] = t($params['subject'], $var, $language->language);
              $message['body'][] = $params['body'];
              break;
          case 'customstats_html_mail':

              $message['subject'] = t($params['subject'], $var, $language->language);

               $body = "<html><body>
                  <h2>HTML Email Sample with Drupal</h2>
                  <hr /><br /><br />
                  {$params['body']}
                  </body></html>";
              $message['body'][] = $body;
              $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
              break;
      }
  }

and I am using hook_mail and drupal_mail to send an email, how should I insert the query results into the body of the mail?

Also, I have created pagecallbacks customstats1 to customstats15 via hook_menu and I need to send indivual mails when users visit each page independently. I have read a few tutorials but I haven't been able to do this.

Thanks in advance for your help.

1

You need to invoke hook_mail() to specify a 'body' parameter. It is a little confusing since it is two steps but well documented: http://api.drupal.org/api/drupal/includes%21mail.inc/function/drupal_mail/6

See 'example_notify()' and 'example_mail()' from the above link.

In your case yo need to call drupal_mail outside of your invocation of hook_mail. See below for an example. You can pass the body as in the $param when you call drupal_mail or you can edit it when you invoke hook_mail.

Be advised that this is not the best example since it would fire off an email any time somone visited that page, but I'm not sure where you are trying to call drupal_mail from:

$query = "
SELECT uid
FROM users
JOIN users_roles ON users.uid = users_roles.uid
WHERE mail NOT LIKE '%@mail.com' 
AND users_roles.rid =6
AND users.status =0
";


 // get the links (no range limit here)
  $queryResult =  db_query($query);
  while ($links = db_fetch_object($queryResult)) {
    $account = user_load($links->uid);
    $body = t('Message for %name', array('%name' => $links->name));
    $subject = t('New email!');
    $params = array('body' => $body, 'subject' => $subject);
    drupal_mail('customstats', 'html_mail', $account->mail, user_preferred_language($account), $params);  
    $page_content .= l($account->name, 'mailto:'.$account->mail) . '<br />';
   }


  if ($page_content == '') {
    $page_content = "No existe información para mostrar.";
  }

  return $page_content;
}
9
  • acouch, I´ve read that documentation a ton of times, i swear, but not being a programmer, I have absolutely no idea what the syntaxis for 'body' => t("Body of the email goes here"), needs to change in order to invoke the $query results of each function I have. I evidently bit much more that I could chew here. But thank you very much for your reply. I will read again. There has to be something I'm missing.
    – MauF
    Feb 16 '12 at 18:21
  • Don't get too frustrated! These things get easier with time. :) Can you post the entire code snippet you are using including your implementation of hook_mail()? I edited my answer, let me know if it helps.
    – acouch
    Feb 16 '12 at 18:28
  • acouch, I edited my original question for you to see the entire code. i left hook_mail almost untouched cause I just configured it to send empty emails, which is a start. Thanx again.
    – MauF
    Feb 16 '12 at 18:37
  • I updated the example. This should work for you. I changed 'customstatsB' to 'customstats'. It is not a good idea to use upper case for the module name so 'customstatsb' or 'customstats_b' might be better.
    – acouch
    Feb 16 '12 at 19:11
  • Thanx, acouch! I am about to make it! I just need to determine what this erros message (Sending of at least one e-mail failed. The error returned was: Invalid address: Object.") is all about. :)
    – MauF
    Feb 16 '12 at 19:21

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.