Add basic infrastructure to actually send out mailings.

This commit is contained in:
Martin Kamerbeek 2010-05-06 18:04:25 +02:00
parent 3f3ee717b7
commit 80c1ce0b44
4 changed files with 99 additions and 5 deletions

View file

@ -93,10 +93,10 @@ sub getRecipients {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
sub generateEmailContent { sub generateEmailContent {
my $self = shift; my $self = shift;
my $issueId = shift;
my $session = $self->session; my $session = $self->session;
my $form = $session->form; my $form = $session->form;
my $issueId = $form->get('issueId');
my $issue = WebGUI::Asset->newByDynamicClass( $session, $issueId ); my $issue = WebGUI::Asset->newByDynamicClass( $session, $issueId );
return "Invalid issueId [$issueId]" unless $issue; return "Invalid issueId [$issueId]" unless $issue;

View file

@ -71,6 +71,7 @@ sub getRecipients {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
sub processContentAsUser { sub processContentAsUser {
my $self = shift; my $self = shift;
my $issueId = shift;
my $userId = shift; my $userId = shift;
my $session = $self->session; my $session = $self->session;
my $currentUser = $session->user; my $currentUser = $session->user;
@ -85,7 +86,7 @@ sub processContentAsUser {
# Generate email body for this user # Generate email body for this user
my $content = $session->style->process( my $content = $session->style->process(
$self->generateEmailContent, $self->generateEmailContent( $issueId ),
$styleTemplateId, $styleTemplateId,
); );
@ -114,9 +115,10 @@ sub www_previewContent {
my $self = shift; my $self = shift;
my $form = $self->session->form; my $form = $self->session->form;
my $issueId = $form->get('issueId');
my $userId = $form->get('userId'); my $userId = $form->get('userId');
return $self->processContentAsUser( $userId ); return $self->processContentAsUser( $issueId, $userId );
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------

View file

@ -46,6 +46,15 @@ sub crud_definition {
return $definition; return $definition;
} }
sub getAsset {
my $self = shift;
my $session = $self->session;
my $asset = WebGUI::Asset->newByDynamicClass( $session, $self->get('assetId') );
#### TODO: error checking
return $asset;
}
sub getStatusLine { sub getStatusLine {
my $self = shift; my $self = shift;
my $db = $self->session->db; my $db = $self->session->db;
@ -66,8 +75,8 @@ sub getStatusLine {
foreach ( qw{ queued sent error } ) { foreach ( qw{ queued sent error } ) {
$output .= sprintf '%s ( %i + %i (test) ) | ', $output .= sprintf '%s ( %i + %i (test) ) | ',
$_, $_,
$status->{$_}->{regular}, $status->{$_}->{regular} || 0,
$status->{$_}->{test}, $status->{$_}->{test} || 0,
; ;
} }

View file

@ -48,5 +48,88 @@ sub crud_definition {
return $definition; return $definition;
} }
sub getMailing {
my $self = shift;
my $session = $self->session;
my $mailing = WebGUI::Mailing->new( $session, $self->get('mailingId') );
#### TODO: error checking;
return $mailing;
}
sub getQueuedTestEmails {
my $class = shift;
my $session = shift;
my $it = $class->getAllIterator( $session, {
constraints => [
{ 'isTest=?' => [ 1 ] },
{ 'status=?' => [ 'queued' ] },
],
} );
return $it;
}
sub error {
my $self = shift;
my $message = shift;
$self->update( {
status => 'error',
errorMessage => $message || 'No error message available',
} );
return;
}
sub send {
my $self = shift;
my $session = $self->session;
#### TODO: Error checking
my $mailing = $self->getMailing;
my $body = $mailing->getAsset->processContentAsUser( $mailing->get('issueId'), $self->get('userId') );
# Mail is a test mail but there is noone to send the result to
if ( $self->get( 'isTest' ) && !$self->get('recipientEmail') ) {
$self->error( 'Cannot send test mails without an override reciepint address' );
return;
}
# Determine recipients email address and bail out if we can't find one.
my $to = $self->get('recipientEmail') || $self->user->get('email');
if ( !$to ) {
$self->error( 'Cannot find an email address for this user' );
return;
}
# Fetch subject
my $subject = '[TEST] Subject selection still needs to be implemented.';
# Setup email
my $mail = WebGUI::Mail::Send->create( $session, {
#from => TODO
to => $to,
subject => $subject,
} );
$mail->addHtml( $body );
# And send it.
my $success = $mail->send;
if ( $success ne '1' ) {
$self->error( "Mail couldn't be sent by WebGUI::Mail::Send" );
}
else {
$self->update( { status => 'sent' } );
}
return;
}
1; 1;