--- MailerContrib.pm.orig	2009-07-24 15:00:15.000000000 +1000
+++ MailerContrib.pm	2009-07-24 14:58:24.000000000 +1000
@@ -70,30 +70,43 @@
     }
     $exwebstr =~ s/\*/\.\*/g;
 
-    if (!defined $twiki) {
-        $twiki = new Foswiki();
+    if (defined $twiki) {
+      print STDERR 'mailNotify(): Foswiki session object was already passed in.';
+    } else {
+      # SMELL: Is there a better of doing this so that 
+      # Foswiki::Func::getListOfWebs() works?
+      $twiki = new Foswiki();
     }
-
-    $Foswiki::Plugins::SESSION = $twiki;
-
-    my $context = Foswiki::Func::getContext();
-
-    $context->{command_line} = 1;
-
-    # absolute URL context for email generation
-    $context->{absolute_urls} = 1;
-
-    initContrib();
-
+    
     my $report = '';
+    initContrib();
+    my $query = new CGI();
+    
     foreach my $web ( Foswiki::Func::getListOfWebs( 'user ') ) {
-       if ( $web =~ /^($webstr)$/ && $web !~ /^($exwebstr)$/ ) {
-          $report .= _processWeb( $twiki, $web );
-       }
+      if ( $web =~ /^($webstr)$/ && $web !~ /^($exwebstr)$/ ) {
+        my (%initialContext) = ('webName' => '$web', 'command_line' => 1);
+        # Trailing slash helps Foswiki::new() resolve subwebs.
+        $query->path_info($web . '/');
+        
+        my $session = new Foswiki(undef, $query, \%initialContext);
+
+        $Foswiki::Plugins::SESSION = $session;
+        my $context = Foswiki::Func::getContext();
+        # SMELL: Redundant? 
+        $context->{command_line} = 1;
+
+        # absolute URL context for email generation
+        # SMELL: Do we really have to preserve old abs_urls state?
+        my $old_context_abs_urls = $context->{absolute_urls};
+        $context->{absolute_urls} = 0;
+
+        $report .= _processWeb( $session, $web );
+
+        $context->{absolute_urls} = $old_context_abs_urls;
+        $session = undef;
+      }
     }
 
-    $context->{absolute_urls} = 0;
-
     return $report;
 }
 
