Index: pm_email_notify/pm_email_notify.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/pm_email_notify/pm_email_notify.module,v
retrieving revision 1.17
diff -u -r1.17 pm_email_notify.module
--- pm_email_notify/pm_email_notify.module	29 Nov 2010 07:57:05 -0000	1.17
+++ pm_email_notify/pm_email_notify.module	13 Jan 2011 23:39:33 -0000
@@ -50,9 +50,45 @@
  * Implements hook_privatemsg_message_insert().
  */
 function pm_email_notify_privatemsg_message_insert($message) {
+  $email_recipients = array();
+
+  // Separate user recipients from other types.
   foreach ($message['recipients'] as $recipient) {
-    // check if recipient enabled email notifications
-    if (isset($recipient->uid) && _pm_email_notify_is_enabled($recipient->uid)) {
+    if ($recipient->type == 'user' || $recipient->type == 'hidden') {
+      $email_recipients[$recipient->uid] = $recipient->uid;
+    }
+    else {
+      $to_load[$recipient->type][] = $recipient;
+    }
+  }
+
+  // Fetch the email recipients for the ones that are not users already.
+  foreach ($to_load as $type => $recipients) {
+    $type_info = privatemsg_recipient_get_type($type);
+    if (isset($type_info['generate recipients']) && is_callable($type_info['generate recipients'])) {
+      foreach ($recipients as $recipient) {
+        // @todo what to do about the 'limit'.
+        $generated_recipients = $type_info['generate recipients']($recipient, 999, 0);
+        if (is_array($generated_recipients)) {
+          foreach ($generated_recipients as $generated_recipient) {
+            $email_recipients[$generated_recipient] = $generated_recipient;
+          }
+        }
+      }
+    }
+  }
+
+  // Load user data for email recipients.
+  // Note: not doing user_load for performance reasons.
+  $result = db_query("SELECT * FROM {users} WHERE uid IN (" . db_placeholders($email_recipients) . ")", $email_recipients);
+  while ($recipient = db_fetch_object($result)) {
+    $email_recipients[$recipient->uid] = $recipient;
+  }
+
+  // Send emails to recipients.
+  foreach ($email_recipients as $recipient) {
+    // Check if recipient enabled email notifications.
+    if (isset($recipient->uid) && isset($recipient->mail) && _pm_email_notify_is_enabled($recipient->uid)) {
       // send them a new pm notification email if they did
       $params['recipient'] = $recipient;
       $params['message'] = $message;
