From f8cb8cd05ac36a37a9f86a4da2011b9a5ccfff2f Mon Sep 17 00:00:00 2001 From: Martin Kamerbeek Date: Wed, 19 May 2010 15:42:39 +0200 Subject: [PATCH] Adding sending of batches and bounce processing. --- lib/WebGUI/Mailing.pm | 67 +++++++++++++++++++ lib/WebGUI/Mailing/Admin.pm | 1 + lib/WebGUI/Mailing/Email.pm | 43 +++++++++++- .../Workflow/Activity/SendQueuedMailings.pm | 9 ++- sbin/newsletter-transport.pl | 24 +++++-- 5 files changed, 134 insertions(+), 10 deletions(-) diff --git a/lib/WebGUI/Mailing.pm b/lib/WebGUI/Mailing.pm index d0a8259..443b145 100644 --- a/lib/WebGUI/Mailing.pm +++ b/lib/WebGUI/Mailing.pm @@ -104,6 +104,73 @@ sub queueTestEmails { return; } +sub queueEmails { + my $self = shift; + my $userIds = shift; + my $session = $self->session; + + croak "User ids must be an array ref" unless ( ref $userIds eq 'ARRAY' ); + + foreach my $userId ( @{ $userIds } ) { + WebGUI::Mailing::Email->create( $session, { + mailingId => $self->getId, + userId => $userId, + recipientEmail => undef, + isTest => 0, + } ); + } + + return; +} +sub www_sendBatch { + my $self = shift; + my $session = $self->session; + + my $f = WebGUI::HTMLForm->new( $session ); + $f->hidden( + name => 'newsletter', + value => 'mailing', + ); + $f->hidden( + name => 'func', + value => 'sendBatchConfirm', + ); + $f->hidden( + name => 'id', + value => $self->getId, + ); + $f->dateTime( + name => 'schedule', + label => 'Send batch at', + ); + $f->submit; + + return WebGUI::Mailing::Admin->new($session)->getAdminConsole->render( $f->print, 'Send batch' ); +} + +sub www_sendBatchConfirm { + my $self = shift; + my $session = $self->session; + my $form = $session->form; + + my $scheduled = $form->dateTime( 'scheduled' ); + + return $self->www_sendBatch unless $scheduled; + + my $asset = WebGUI::Asset->newByDynamicClass( $session, $self->get('assetId') ); + croak "Cannot instaciate asset " . $self->get('assetId') unless $asset; + + my $userIds = $asset->getRecipients; + + $self->queueEmails( $userIds ); + $self->update( { + sendDate => $scheduled, + active => 1, + } ); + + return WebGUI::Mailing::Admin->new( $session )->www_view; +} + sub www_sendTestEmails { my $self = shift; my $session = $self->session; diff --git a/lib/WebGUI/Mailing/Admin.pm b/lib/WebGUI/Mailing/Admin.pm index 1dbb065..e21b763 100644 --- a/lib/WebGUI/Mailing/Admin.pm +++ b/lib/WebGUI/Mailing/Admin.pm @@ -123,6 +123,7 @@ sub www_view { %{ $mailing->get }, status => $mailing->getStatusLine, sendTestUrl => $url->page('newsletter=mailing;func=sendTestEmails;id='.$mailing->getId), + sendUrl => $url->page('newsletter=mailing;func=sendBatch;id='.$mailing->getId), }; }; diff --git a/lib/WebGUI/Mailing/Email.pm b/lib/WebGUI/Mailing/Email.pm index 62195de..d6e86e4 100644 --- a/lib/WebGUI/Mailing/Email.pm +++ b/lib/WebGUI/Mailing/Email.pm @@ -73,6 +73,37 @@ sub getQueuedTestEmails { return $it; } +sub getSendableEmails { + my $class = shift; + my $session = shift; + + my $it = $class->getAllIterator( $session, { + joinUsing => [ + { 'WGMailing' => 'mailingId' }, + ], + constraints => [ + { 'isTest=?' => [ 0 ] }, + { 'status=?' => [ 'queued' ] }, + { 'active=?' => [ 1 ] }, + { 'sendDate <= ?' => [ time ] }, + ], + } ); + + my @sql = $class->getAllSql( $session, { + joinUsing => [ + { 'WGMailing' => 'mailingId' }, + ], + constraints => [ + { 'isTest=?' => [ 0 ] }, + { 'status=?' => [ 'queued' ] }, + { 'active=?' => [ 1 ] }, + { 'sendDate <= ?' => [ time ] }, + ], + } ); + $session->log->warn( "--->" . shift( @sql ) . join "\n\n", @{ shift @sql } ); + return $it; +} + sub error { my $self = shift; my $message = shift; @@ -145,7 +176,7 @@ sub send { # Setup email my $mail = WebGUI::Mail::Send->create( $session, { - #from => TODO + from => 'martin@geefmegeld.nl', to => $to, subject => $subject, messageId => "$messageId\@$domain", @@ -156,7 +187,7 @@ sub send { # And send it. my $success = $mail->send; - +$session->log->warn( "Sending email to [$to]" ); if ( $success ne '1' ) { $self->error( "Mail couldn't be sent by WebGUI::Mail::Send" ); } @@ -167,6 +198,14 @@ sub send { return; } +sub user { + my $self = shift; + my $session = $self->session; + + my $user = WebGUI::User->new( $session, $self->get('userId') ); + return $user; +} + sub registerBounced { my $self = shift; diff --git a/lib/WebGUI/Workflow/Activity/SendQueuedMailings.pm b/lib/WebGUI/Workflow/Activity/SendQueuedMailings.pm index 078cfd1..5a9f40b 100644 --- a/lib/WebGUI/Workflow/Activity/SendQueuedMailings.pm +++ b/lib/WebGUI/Workflow/Activity/SendQueuedMailings.pm @@ -49,12 +49,19 @@ sub execute { # For now only process test mails. my $it = WebGUI::Mailing::Email->getQueuedTestEmails( $session ); - while ( my $email = $it->() ) { return $self->WAITING(1) if time >= $maxTime; $email->send; } + + my $it = WebGUI::Mailing::Email->getSendableEmails( $session ); + while ( my $email = $it->() ) { + $session->log->warn( "processing" . $email->getId ); + return $self->WAITING(1) if time >= $maxTime; + + $email->send; + } return $self->COMPLETE; } diff --git a/sbin/newsletter-transport.pl b/sbin/newsletter-transport.pl index 3775cc5..9d35e53 100755 --- a/sbin/newsletter-transport.pl +++ b/sbin/newsletter-transport.pl @@ -12,24 +12,34 @@ BEGIN { use WebGUI::Mailing::Email; use List::MoreUtils qw{ any }; - my $NO_SUCH_USER = 67; + +my %configs = ( + 'martintwee.oqapi.nl' => 'martintwee.oqapi.nl.conf', +); + my $webguiRoot = '/data/WebGUI'; -my $configFile = 'martintwee.oqapi.nl.conf'; -my ( $domain, $user ) = @ARGV; +my ( $domain, $user ) = @ARGV; + +my $configFile = $configs{ $domain }; + my ( $listname, $command ) = $user =~ m{^(.+)-([^-]+)$}i; -#exit $NO_SUCH_USER unless any { $command eq $_ } qw{ subscribe unsubscribe bounces confirm }; - open my $log, '>>/data/test.log' || die "Cannot open log"; -unless ( any { $command eq $_ } qw{ subscribe unsubscribe bounce confirm } ) { + +my $validCommand = any { $command eq $_ } qw{ subscribe unsubscribe bounce confirm }; + +print $log "[$command][$validCommand][$configFile]\n"; +unless ( $configFile && $validCommand ) { print $log "Ignoring $domain $user\n"; close $log; - exit $NO_SUCH_USER; + system "/usr/sbin/sendmail -G -i $user\@$domain"; + + exit(0); } else { my $session = WebGUI::Session->open($webguiRoot,$configFile);