Index: mail-out.pl
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og2list/mail-out.pl,v
retrieving revision 1.36
diff -u -p -r1.36 mail-out.pl
--- mail-out.pl	6 Sep 2006 00:17:10 -0000	1.36
+++ mail-out.pl	24 Oct 2006 14:11:25 -0000
@@ -71,9 +71,13 @@ use vars qw(
    $get_content_stmt $get_rcpt_stmt $set_sentflag_stmt $get_sender_stmt
    $get_arg_stmt $get_create_mail_stmt $get_del_arg_stmt
 
+   $get_digest_rcpt_stmt $clear_digest_rcpt_stmt
+
    $get_content_handle $get_rcpt_handle $set_sentflag_handle $get_sender_handle
    $get_arg_handle $get_create_mail_handle $get_del_arg_handle
 
+   $get_digest_rcpt_handle $clear_digest_rcpt_handle
+
    $dbtype $dbuser $dbauth $dbhost $dbase $mail_domain
 
    $set_reply_to
@@ -165,6 +169,12 @@ FROM og2list_outgoing_recipients
 WHERE (mid = ?) AND (status = ?)
 EOF
 
+$get_digest_rcpt_stmt=<<EOF;
+SELECT DISTINCT recipient_address
+FROM og2list_outgoing_recipients
+WHERE (nid = ?) AND (status = ?)
+EOF
+
 $get_sender_stmt=<<EOF;
 SELECT DISTINCT g.recipient
 FROM og2list_outgoing_recipients AS r, og2list_groups AS g
@@ -177,6 +187,11 @@ DELETE FROM og2list_outgoing_recipients
 WHERE (mid = ?) AND (recipient_address = ?)
 EOF
 
+$clear_digest_rcpt_stmt=<<EOF;
+DELETE FROM og2list_outgoing_recipients
+WHERE (nid = ?) AND (status = ?)
+EOF
+
 # ----------------------------------------------------------------------
 
 sub send_bsmtp($$$$) {
@@ -261,7 +276,9 @@ if ($dbtype eq 'mysql') { $dbh->do('SET 
 # Set up queries
 $get_content_handle = $dbh->prepare($get_content_stmt) or die;
 $get_rcpt_handle = $dbh->prepare($get_rcpt_stmt) or die;
+$get_digest_rcpt_handle = $dbh->prepare($get_digest_rcpt_stmt) or die;
 $set_sentflag_handle = $dbh->prepare($set_sentflag_stmt) or die;
+$clear_digest_rcpt_handle = $dbh->prepare($clear_digest_rcpt_stmt) or die;
 $get_sender_handle = $dbh->prepare($get_sender_stmt) or die;
 $get_arg_handle = $dbh->prepare($get_arg_stmt) or die;
 $get_del_arg_handle = $dbh->prepare($get_del_arg_stmt) or die;
@@ -290,14 +307,19 @@ if ($do_digest) {
          "Charset"  => "utf-8",
 	 "X-Mailer" => undef);
 
-      # FIXME List of recipients is determined by the recipients that
-      # are associated with the first message found. It should be
-      # determined by the list of addresses that are currently
-      # subscribed to the group.
-      $get_rcpt_handle->execute($mid,OG2LIST_OUTGOING_DIGEST);
-      @{$digest_msgs{$nid}}{recipients} =
-	map {$$_[0] } $get_rcpt_handle->fetchall_arrayref();
+      # Get the list of addresses that are currently waiting a message
+      # for this digest
+      #
+      $get_digest_rcpt_handle->execute($nid, OG2LIST_OUTGOING_DIGEST);
+
+       my $recipient_address;
+       while ( ($recipient_address = 
+ 		      $get_digest_rcpt_handle->fetchrow_array()) ) {
+           # print "- $recipient_address \n";
+           push @{${$digest_msgs{$nid}}{recipients}}, $recipient_address;
+       }
     }
+
     # Add each message to the multipart/digest container.
     my $msg = MIME::Entity->build
       ("From"       => "$from_name <$from_address>",
@@ -330,7 +352,8 @@ if ($do_digest) {
     my $sender = $msg{sender};
     my @mids = @{$msg{mids}};
     # Get a bunch of recipients
-    while (my @recipients = splice @{$digest_msgs{$_}{recipients}},0,$batch_size) {
+    while (my @recipients = 
+	    splice @{$digest_msgs{$_}{recipients}},0,$batch_size) {
       # print "- ".join(', ', @recipients)."\n";
       foreach ($send_method) {
 	/^sendmail$/i && do { send_sendmail([$mid], $content, $sender,
@@ -344,6 +367,11 @@ if ($do_digest) {
 	die("Value of \$send_method not recognized: $send_method");
       }
     }
+
+    # mids for this nid (key) have been sent, clear it from 
+    # the outgoing_recipients table
+    ##
+    $clear_digest_rcpt_handle->execute($_, OG2LIST_OUTGOING_DIGEST);
   }
 } else {
   $get_content_handle->execute(1);
