Adding sending of batches and bounce processing.

This commit is contained in:
Martin Kamerbeek 2010-05-19 15:42:39 +02:00
parent d62f56a635
commit f8cb8cd05a
5 changed files with 134 additions and 10 deletions

View file

@ -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;

View file

@ -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),
};
};

View file

@ -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;

View file

@ -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;
}