diff --git a/docs/mail_commands_setup.pod b/docs/mail_commands_setup.pod
new file mode 100644
index 0000000..10e0e1f
--- /dev/null
+++ b/docs/mail_commands_setup.pod
@@ -0,0 +1,101 @@
+=head1 Configuring WebGUI mail commands
+
+This document discusses the steps required to tie WebGUI mal command into
+postfix.
+
+=head2 Setting up WebGUI
+
+The mappings between To adress in the incoming emails and WebGUI instances is
+defined in /data/WebGUI/etc/mailing_dispatch.config. This is a JSON file
+containing a hash with domain to instance config file mappings:
+
+ {
+ "mailing.domain1.com" : "www_domain1_com.conf",
+ "newsletter.foo.com" : "www_foo_com.conf"
+ }
+
+Note that any domain can be tied to any config file: domains don't have to
+match. The only requirement is that the server can receive mail on the given
+domain. Eg. the following is a valid mapping
+
+ {
+ "newsletter.bar.com" : "www_xyzzy_com.conf"
+ }
+
+It is also possible to map multiple domains to a single config file, eg.
+
+ {
+ "mailing.example.com" : "example_com.conf",
+ "mailing.foobar.org" : "example_com.conf"
+ }
+
+However it is not allowed to map a single domain to multiple config files. If
+you do the instance that is used, is not defined. Eg. the following is NOT
+allowed.
+
+ {
+ "mailing.example.com" : "example_com.conf",
+ "mailing.example.com" : "foobar_org.conf"
+ }
+
+
+=head2 Setting up Postfix
+
+The newsletter-transport.pl script in the webgui_newsletter sbin directory
+acts as a bridge between postfix and WebGUI. It is intended to be invoked
+by postfix's pipe command.
+
+=head3 Configuring master.cf
+
+We need a queue that processes mail intended for WebGUI. To do so add the
+following to /etc/postfix/master.cf:
+
+ wgml unix - n n - - pipe
+ flags=FR user=webgui argv=/data/custom/webgui_newsletter/sbin/newsletter-transport.pl \
+ --domain=${domain} --user=${user} --sender=${sender} --senderIp=${client_address}
+
+This creates a queue called wgml that pipes all mail in it through the
+newsletter-transport.pl script. Make sure that the user parameter is set correctly and argv
+contains the full path to the newsletter-transport.pl script.
+
+
+=head3 Setup a transport map
+
+In order to determine which emails should go to the wgml queue, create a
+mapping in /etc/postfix/wg_mailer_transport. There are more ways to do this.
+
+If you use a dedicated maildomain used solely for receiving WebGUI mail
+commands add a line like the following for each mail domain in your
+/date/WebGUI/etc/mailing_dispatch.config:
+
+ /@mailing.foo.com$/ wgml:
+ /@newsletter.example.com$/ wgml:
+
+If you use a domain that is not solely dedicated to receiving WebGUI mail
+commands, you can match on individual commands:
+
+ /^[a-zA-Z0-9_-]+-bounce@/ wgml:
+ /^[a-zA-Z0-9_-]+-subscribe@/ wgml:
+
+
+Afterwards complile the mapping into a postfix lookup table
+
+ postmap /etc/postfix/wg_mailer_transport
+
+You have to do this everytime you change the mapping.
+
+=head3 Configuring main.cf
+
+Now we need to tell postfix to use the mapping, and how to use the wgml
+queue. To do this add the following to /etc/postfix/main.cf:
+
+ smtpd_reject_unlisted_recipient = no
+ transport_maps = regexp:/etc/postfix/wg_mailer_transport
+ wgml_destination_recipient_limit = 1
+
+Make sure you add the domains in your mailing_dispatch.config to the
+mydestination directlive in main.cf:
+
+ mydestination = mailing.foo.com, newsletter.example.org, some.other.domain
+
+Finally restart postfix.
diff --git a/docs/postfix_configuration b/docs/postfix_configuration
deleted file mode 100644
index b6ee775..0000000
--- a/docs/postfix_configuration
+++ /dev/null
@@ -1,17 +0,0 @@
-Add to /etc/postfix/main.cf:
- smtpd_reject_unlisted_recipient = no
- transport_maps = regexp:/etc/postfix/wg_mailer_transport
- wgml_destination_recipient_limit = 1
-
-Add to /etc/postfix/master.cf:
- wgml unix - n n - - pipe
- flags=FR user=martin argv=/data/custom/webgui_newsletter/sbin/newsletter-transport.pl ${domain} ${user}
-
-
-Create /etc/postfix/wg_mailer_transport:
- /^[a-zA-Z0-9]+-bounce@/ wgml:
-
-And run:
- postmap /etc/postfix/wg_mailer_transport
-
-Finally restart postfix.
diff --git a/docs/required_modules b/docs/required_modules
index f1d1dac..b8200dc 100644
--- a/docs/required_modules
+++ b/docs/required_modules
@@ -1,3 +1,3 @@
-Mail::DeliveryStatus::BounceParser
-Class::InsideOut
+Mail::DeliveryStatus::BounceParser (v 1.525)
+Class::InsideOut (v 1.10)
diff --git a/lib/WebGUI/Asset/Wobject/NewsletterCollection.pm b/lib/WebGUI/Asset/Wobject/NewsletterCollection.pm
index 85a42d1..d0a234a 100644
--- a/lib/WebGUI/Asset/Wobject/NewsletterCollection.pm
+++ b/lib/WebGUI/Asset/Wobject/NewsletterCollection.pm
@@ -8,7 +8,7 @@ use Class::C3;
use WebGUI::User::SpecialState;
use base qw{
- WebGUI::AssetAspect::Mailable
+ WebGUI::AssetAspect::Mailable
WebGUI::AssetAspect::Subscriber
WebGUI::Asset::Wobject
};
@@ -34,11 +34,18 @@ sub definition {
tab => 'display',
defaultValue => 1,
},
+ useHoneypot => {
+ fieldType => 'yesNo',
+ label => $i18n->get('useHoneypot label'),
+ hoverHelp => $i18n->get('useHoneypot description'),
+ tab => 'security',
+ defaultValue => 1,
+ },
);
-
+
push @{ $definition }, {
assetName => $i18n->get('assetName'),
- icon => 'newWobject.gif',
+ icon => 'newsletter_collection.gif',
autoGenerateForms => 1,
tableName => 'NewsletterCollection',
className => 'WebGUI::Asset::Wobject::NewsletterCollection',
@@ -52,12 +59,16 @@ sub definition {
sub getIssues {
my $self = shift;
- my $issues = $self->getLineage( [ 'children' ], {
- returnObjects => 1,
- orderByClause => 'lineage desc',
- } );
-
- return $issues;
+ # Caching of instanciated assets is not for speed, but is requied since prepareView is called on them, and we
+ # need them again in that state in getViewVars.
+ unless ( $self->{ _issues } ) {
+ $self->{ _issues } = $self->getLineage( [ 'children' ], {
+ returnObjects => 1,
+ orderByClause => 'lineage desc',
+ } );
+ }
+
+ return $self->{ _issues };
}
#----------------------------------------------------------------------------
@@ -88,7 +99,8 @@ sub getAssetContent {
my $self = shift;
my $asset = shift;
- $asset->prepareView;
+ # Do not call prepareView on $asset here but rather do this in our own prepareView to prevent head tags being
+ # written to body.
my $content = $asset->view;
return $content;
@@ -106,6 +118,11 @@ sub prepareView {
$self->{ _viewTemplate } = $template;
+ # Call prepareview on issues here, to prevent head tags ending up in the body.
+ foreach my $issue ( @{ $self->getIssues } ) {
+ $issue->prepareView;
+ }
+
return;
}
@@ -126,11 +143,11 @@ sub getViewVars {
foreach my $issue ( @{ $issues } ) {
my $issueVar = $issue->get;
$issueVar->{ url } = $issue->getUrl;
-
- my $isRecent =
- ( !$displayIssueId && $recentCount < $maxRecent )
- || ( $issue->getId eq $displayIssueId )
- ;
+
+ my $isRecent = defined $displayIssueId
+ ? $issue->getId eq $displayIssueId
+ : $recentCount < $maxRecent
+ ;
if ( $isRecent ) {
$issueVar->{ content } = $self->getAssetContent( $issue );
@@ -157,7 +174,7 @@ sub view {
my $self = shift;
my $form = $self->session->form;
- my $var = $self->getViewVars( {
+ my $var = $self->getViewVars( {
displayIssue => $form->guid('displayIssue'),
} );
@@ -165,4 +182,3 @@ sub view {
}
1;
-
diff --git a/lib/WebGUI/AssetAspect/Mailable.pm b/lib/WebGUI/AssetAspect/Mailable.pm
index 90bc717..5efa6c7 100644
--- a/lib/WebGUI/AssetAspect/Mailable.pm
+++ b/lib/WebGUI/AssetAspect/Mailable.pm
@@ -2,7 +2,7 @@ package WebGUI::AssetAspect::Mailable;
use strict;
use warnings;
-use Class::C3;
+use Class::C3;
use WebGUI::Macro;
use Tie::IxHash;
@@ -115,13 +115,15 @@ sub processContentAsUser {
$session->user( { userId => $userId } );
$session->log->preventDebugOutput;
- my $styleTemplateId =
- $configuration->{ styleTemplateId }
- || $self->get('mailStyleTemplateId')
+ my $styleTemplateId =
+ $configuration->{ styleTemplateId }
+ || $self->get('mailStyleTemplateId')
|| $self->get('styleTemplateId');
+ $session->stow->set( 'mailing_rendering' => 1 );
+
# Generate email body for this user
- my $content = $session->style->process(
+ my $content = $session->style->process(
$self->generateEmailContent( $issueId, $configuration ),
$styleTemplateId,
);
@@ -129,10 +131,13 @@ sub processContentAsUser {
# Process macros
WebGUI::Macro::process( $session, \$content );
+ $session->stow->delete( 'mailing_rendering' );
+
# Become ourselves again.
$session->user( { userId => $currentUser->getId } );
$var->switchAdminOn if $adminOn;
+
return $content;
}
diff --git a/lib/WebGUI/AssetAspect/Subscriber.pm b/lib/WebGUI/AssetAspect/Subscriber.pm
index 272e14a..fed9da6 100644
--- a/lib/WebGUI/AssetAspect/Subscriber.pm
+++ b/lib/WebGUI/AssetAspect/Subscriber.pm
@@ -2,11 +2,19 @@ package WebGUI::AssetAspect::Subscriber;
use strict;
use warnings;
-use Class::C3;
+use 5.010;
+use Class::C3;
use Carp;
use WebGUI::Asset::Template;
use WebGUI::Macro;
+use WebGUI::Mail::Send;
+use WebGUI::Group;
+use WebGUI::Asset;
+use WebGUI::Form;
+use WebGUI::Form::Honeypot;
+use WebGUI::User::SpecialState;
+use WebGUI::International;
use Tie::IxHash;
use URI;
@@ -18,6 +26,11 @@ sub definition {
my $i18n = WebGUI::International->new( $session,'AssetAspect_Subscriber' );
tie my %properties, 'Tie::IxHash', (
+ listName => {
+ fieldType => 'text',
+ label => $i18n->echo("List name"),
+ tab => 'subscription',
+ },
subscriptionGroupId => {
fieldType => 'subscriptionGroup',
label => $i18n->get( 'Subscription group' ),
@@ -33,7 +46,7 @@ sub definition {
alwaysConfirmSubscription => {
fieldType => 'yesNo',
defaultValue => 0,
- label => $i18n->get( 'require confirmation' ),
+ label => $i18n->get( 'require confirmation' ),
tab => 'subscription',
},
allowAnonymousSubscription => {
@@ -75,6 +88,13 @@ sub definition {
namespace => 'Subscriber/NoMutationEmail',
tab => 'subscription',
},
+ confirmMutationTemplateId => {
+ fieldType => 'template',
+ defaultValue => 'WUk-wEhGiF8dcEogrJfrfg',
+ label => $i18n->get( 'confirm mutation template' ),
+ namespace => 'Subscriber/MutationConfirmation',
+ tab => 'subscription',
+ }
);
push( @{ $definition }, {
@@ -116,7 +136,7 @@ sub getListHeaders {
my $site = $self->session->url->getSiteURL;
my $headers = {
- 'List-Unsubscribe' =>
+ 'List-Unsubscribe' =>
'<' . $site . $self->getUrl( "func=processSubscription&action=unsubscribe&email=$email" ) . '>',
'List-Subscribe' =>
'<' . $site . $self->getUrl( "func=processSubscription&action=subscribe&email=$email" ) . '>',
@@ -173,6 +193,44 @@ sub isSubscribed {
}
#----------------------------------------------------------------------------
+=head2 appendSubscriptionFormVars
+
+=head3 honeyPot
+Part of the form vars are the honeyPot variables. This is a form plugin that
+is used in NewsletterCollection.pm to activate the use of a honeypot or not,
+in this module, AssetAspect/Subscriber.pm, to check the honeypot and to
+display the form values and in i18n.
+
+There are the following form vars:
+
+=head4 subscriptionForm_emailBox
+This renders both the emailbox, subscribe/unsubscribe radio buttons and the
+honeypot form inputs:
+
+
+
+
+
+You can easily make the honeypot input field invisible with some css for
+class honeypot.
+
+=head4 form_honeypot
+Renders these fields:
+
+
+
+=head4 form_honeypot_id
+Gives you the id for the honeypot input. This makes it easy to create a label:
+
+
+=cut
sub appendSubscriptionFormVars {
my $self = shift;
my $var = shift || {};
@@ -180,16 +238,41 @@ sub appendSubscriptionFormVars {
my $i18n = WebGUI::International->new( $session, 'AssetAspect_Subscriber' );
# Setup form controls
- my $formHeader =
+ my $formHeader =
WebGUI::Form::formHeader( $session, { action => $self->getUrl } )
. WebGUI::Form::hidden( $session, { name => 'func', value => 'processSubscription' } )
;
- my $subscribeButton =
- sprintf '', $i18n->get('subscribe');
- my $unsubscribeButton =
- sprintf '', $i18n->get('unsubscribe');
- my $emailBox = WebGUI::Form::email( $session, { name => 'email', value => '' } );
my $formFooter = WebGUI::Form::formFooter( $session );
+ my $subscribeButton =
+ $formHeader
+ . WebGUI::Form::hidden( $session, { name => 'action', value => 'subscribe' } )
+ . WebGUI::Form::submit( $session, { value => $i18n->get('subscribe') } )
+ . $formFooter
+ ;
+ my $unsubscribeButton =
+ $formHeader
+ . WebGUI::Form::hidden( $session, { name => 'action', value => 'unsubscribe' } )
+ . WebGUI::Form::submit( $session, { value => $i18n->get('unsubscribe') } )
+ . $formFooter
+ ;
+ # honeypot is connected to the emailbox, that is displayed on anonymous subscription
+ # and only if set to useHoneyPot in definition/display
+ my $honeypot = WebGUI::Form::Honeypot->new( $self->session, { name => 'hp' } );
+ my $honeypot_form = $self->get('useHoneypot') ? $honeypot->toHtml : '';
+ my $emailBox =
+ $formHeader
+ . WebGUI::Form::email( $session, { name => 'email', value => '' } )
+ . WebGUI::Form::radioList( $session, {
+ name => 'action',
+ options => {
+ subscribe => $i18n->get('subscribe'),
+ unsubscribe => $i18n->get('unsubscribe'),
+ }
+ } )
+ . $honeypot_form
+ . WebGUI::Form::submit( $session )
+ . $formFooter
+ ;
# Compose default subscription form for current user
my $form = '';
@@ -198,19 +281,21 @@ sub appendSubscriptionFormVars {
$form .= $unsubscribeButton if $self->canUnsubscribe;
}
elsif ( $self->get('allowAnonymousSubscription') ) {
- $form = $emailBox . $subscribeButton . $unsubscribeButton;
+ $form = $emailBox;
}
# Setup tmpl_vars
- $var->{ subscriptionForm_form } = "$formHeader $form $formFooter" if $form;
+ $var->{ subscriptionForm_form } = $form if $form;
$var->{ subscriptionForm_header } = $formHeader;
$var->{ subscriptionForm_footer } = $formFooter;
$var->{ subscriptionForm_subscribeButton } = $subscribeButton;
$var->{ subscriptionForm_unsubscribeButton } = $unsubscribeButton;
- $var->{ subscrittionForm_emailBox } = $emailBox;
+ $var->{ subscriptionForm_emailBox } = $emailBox;
$var->{ user_canSubscribe } = $self->canSubscribe;
$var->{ user_canUnsubscribe } = $self->canUnsubscribe;
$var->{ user_isRegistered } = $session->user->isRegistered;
+ $var->{ form_honeypot } = $honeypot->toHtml;
+ $var->{ form_honeypot_id } = $honeypot->get('id');
return $var;
}
@@ -219,8 +304,8 @@ sub appendSubscriptionFormVars {
sub getSubscriptionGroup {
my $self = shift;
my $groupId = $self->get( "subscriptionGroupId" );
- my $group = $groupId
- ? WebGUI::Group->new( $self->session, $groupId )
+ my $group = $groupId
+ ? WebGUI::Group->new( $self->session, $groupId )
: $self->createSubscriptionGroup
;
@@ -231,7 +316,7 @@ sub getSubscriptionGroup {
sub getReturnUrl {
my $self = shift;
my $referer = $self->session->env->get('HTTP_REFERER');
-
+
return unless $referer;
# Get path and strip leading and trailing slash if it exists.
@@ -263,6 +348,45 @@ sub getEmailVars {
return $var;
}
+#----------------------------------------------------------------------------
+sub getAssetByListName {
+ my $class = shift;
+ my $session = shift;
+ my $listName = shift || return;
+ my $db = $session->db;
+
+ my $assetId = $db->quickScalar( 'select assetId from assetAspectSubscriber where listName=? limit 1', [
+ $listName
+ ] );
+
+ return unless $assetId;
+
+ my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId );
+
+ return $asset;
+}
+
+#----------------------------------------------------------------------------
+sub processPropertiesFromFormPost {
+ my $self = shift;
+ my $session = $self->session;
+ my $form = $session->form;
+
+ my $listName = $form->get('listName');
+ my $asset = WebGUI::AssetAspect::Subscriber->getAssetByListName( $session, $listName );
+
+ my $errors = $self->next::method;
+
+ if ( $asset && $asset->getId ne $self->getId ) {
+ return [
+ @{ $errors || [] },
+ "List name $listName is already taken."
+ ];
+ }
+
+ return $errors;
+}
+
#----------------------------------------------------------------------------
sub sendSubscriptionConfirmation {
my $self = shift;
@@ -271,7 +395,13 @@ sub sendSubscriptionConfirmation {
my $action = shift || 'subscribe';
my $session = $self->session;
my $i18n = WebGUI::International->new( $session, 'AssetAspect_Subscriber' );
-
+=pod
+ my $honeypot = $session->form->honeypot( 'hp' );
+ if ( $self->get('useHoneypot') && $honeypot ) {
+ $session->log->warn( "Honeypot triggered: $honeypot" );
+ return;
+ }
+=cut
my $var = $self->getEmailVars( $user );
my $url = $session->url->getSiteURL . $self->getUrl( "func=confirmMutation;code=$code" );
@@ -280,14 +410,18 @@ sub sendSubscriptionConfirmation {
$var->{ actionIsSubscribe } = $action eq 'subscribe';
my $mail = WebGUI::Mail::Send->create( $self->session, {
- to => $user->get('email'),
- subject => $self->get('confirmationEmailSubject'),
+ to => $user->get('email'),
+ subject => $self->get('confirmationEmailSubject'),
+ contentType => 'multipart/alternative',
} );
my $templateId = $self->get('confirmationEmailTemplateId');
my $template = WebGUI::Asset::Template->new( $session, $templateId );
if ( $template ) {
- $mail->addHtml( $template->process( $var ) );
+ my $content = $template->process( $var );
+
+ $mail->addHtml( $content );
+ $mail->addText( $self->transformToText( $content ) );
}
else {
$session->log->error( "Cannot instanciate confirmation email template with id [$templateId]" );
@@ -306,19 +440,29 @@ sub sendNoMutationEmail {
my $action = shift || 'subscribe';
my $session = $self->session;
my $i18n = WebGUI::International->new( $session, 'AssetAspect_Subscriber' );
-
+=pod
+ my $honeypot = $session->form->honeypot( 'hp' );
+ if ( $self->get('useHoneypot') && $honeypot ) {
+ $session->log->warn( "Honeypot triggered: $honeypot" );
+ return;
+ }
+=cut
my $var = $self->getEmailVars( $user );
$var->{ actionIsSubscribe } = $action eq 'subscribe';
my $mail = WebGUI::Mail::Send->create( $self->session, {
- to => $user->get('email'),
- subject => $self->get('noMutationEmailSubject'),
+ to => $user->get('email'),
+ subject => $self->get('noMutationEmailSubject'),
+ contentType => 'multipart/alternative',
} );
my $templateId = $self->get('noMutationEmailTemplateId');
my $template = WebGUI::Asset::Template->new( $session, $templateId );
if ( $template ) {
- $mail->addHtml( $template->process( $var ) );
+ my $content = $template->process( $var );
+
+ $mail->addHtml( $content );
+ $mail->addText( $self->transformToText( $content ) );
}
else {
$session->log->error( "Cannot instanciate no mutation email template with id [$templateId]" );
@@ -332,6 +476,18 @@ sub sendNoMutationEmail {
return;
}
+sub transformToText {
+ my $self = shift;
+ my $html = shift;
+
+ my $text = $html;
+ #HTML::Entities::decode($text);
+ $text =~ s/(.+?)<\/a>/$2 ($1)/g;
+ $text = WebGUI::HTML::html2text($text);
+
+ return $text;
+}
+
#----------------------------------------------------------------------------
sub logConfirmation {
my $self = shift;
@@ -339,7 +495,7 @@ sub logConfirmation {
my $session = $self->session;
my $db = $session->db;
- $db->write(
+ $db->write(
' update assetAspectSubscriber_log set '
.' confirmationIp=?, confirmationDate=?, confirmed=? where code=? and assetId=?',
[
@@ -363,7 +519,7 @@ sub logRequest {
my $session = $self->session;
my $db = $session->db;
- $db->write(
+ $db->write(
' insert into assetAspectSubscriber_log set '
.' assetId=?, requestIp=?, requestDate=?, code=?, confirmed=?, anonymous=?, type=?, userId=?, email=?',
[
@@ -389,6 +545,7 @@ sub subscribe {
my $requireConfirm = shift // $self->get('alwaysConfirmSubscription');
my $session = $self->session;
+
if ( $requireConfirm ) {
if ( $user->isInGroup( $self->getSubscriptionGroup->getId ) ) {
$self->sendNoMutationEmail( $user, 'subscribe' );
@@ -414,7 +571,7 @@ sub unsubscribe {
my $self = shift;
my $user = shift || $self->session->user;
my $requireConfirm = shift // $self->get('alwaysConfirmSubscription');
-
+
my $session = $self->session;
if ( $requireConfirm ) {
@@ -447,29 +604,72 @@ sub www_confirmMutation {
my $code = $form->get('code');
return unless $code;
- my ($userId, $type) = $db->quickArray(
- 'select userId, type from assetAspectSubscriber_log where confirmed=? and code=?',
+ my ($userId, $type) = $db->quickArray(
+ 'select userId, type from assetAspectSubscriber_log where confirmed=? and code=?',
[
0,
$code,
]
);
+ my $var = {
+ message => $i18n->get( 'wrong code' ),
+ returnUrl => $self->getUrl,
+ isSuccess => 0,
+ "is$type" => 1,
+ };
if ( $userId ) {
$self->logConfirmation( $code );
if ( $type eq 'subscribe' ) {
$self->getSubscriptionGroup->addUsers( [ $userId ] );
- return $self->processStyle( sprintf $i18n->get( 'subscription successful'), $self->getUrl );
+
+ $var->{ message } = sprintf $i18n->get( 'subscription successful'), $self->getUrl;
+ $var->{ isSuccess } = 1;
}
elsif ( $type eq 'unsubscribe' ) {
$self->getSubscriptionGroup->deleteUsers( [ $userId ] );
- return $self->processStyle( sprintf $i18n->get( 'unsubscription successful' ), $self->getUrl );
+
+ $var->{ message } = sprintf $i18n->get( 'unsubscription successful' ), $self->getUrl;
+ $var->{ isSuccess } = 1;
}
}
- return $self->processStyle( $i18n->get( 'wrong code' ) );
+ return $self->processStyle( $self->processTemplate( $var, $self->get('confirmMutationTemplateId') ) );
+}
+#----------------------------------------------------------------------------
+sub subscribeAnonymous {
+ my $self = shift;
+ my $email = shift; # TODO || return ?
+ my $session = $self->session;
+
+ my $emailUser = WebGUI::User::SpecialState->newByEmail( $session, $email );
+
+ # If email address doesn't have an account, create one.
+ if ( !defined $emailUser ) {
+ $emailUser = WebGUI::User::SpecialState->create( $session );
+ $emailUser->update( { email => $email } );
+ }
+ $emailUser->addSpecialState( 'Subscriber', $self->getId );
+
+ $self->subscribe( $emailUser, 1 );
+
+ return;
+}
+
+#----------------------------------------------------------------------------
+sub unsubscribeAnonymous {
+ my $self = shift;
+ my $email = shift;
+ my $session = $self->session;
+
+ my $emailUser = WebGUI::User::SpecialState->newByEmail( $session, $email );
+ if ( defined $emailUser ) {
+ $self->unsubscribe( $emailUser, 1 );
+ }
+
+ return;
}
#----------------------------------------------------------------------------
@@ -482,6 +682,12 @@ sub www_processSubscription {
my $action = $form->get( 'action' );
return unless $action eq 'subscribe' || $action eq 'unsubscribe';
+ my $honeypot = $session->form->honeypot( 'hp' );
+ if ( $self->get('useHoneypot') && $honeypot ) {
+ $session->log->warn( "Honeypot triggered: $honeypot" );
+ return;
+ }
+
if ( $session->user->isRegistered ) {
if ( $action eq 'subscribe' && $self->canSubscribe ) {
$self->subscribe;
@@ -496,18 +702,9 @@ sub www_processSubscription {
my $email = $form->email( 'email' );
return 'Error: no email address passed' unless $email;
- my $emailUser = WebGUI::User::SpecialState->newByEmail( $session, $email );
- if ( $action eq 'unsubscribe' && defined $emailUser ) {
- $self->unsubscribe( $emailUser, 1 );
- }
- if ( $action eq 'subscribe' ) {
- if ( !defined $emailUser ) {
- $emailUser = WebGUI::User::SpecialState->create( $session );
- $emailUser->update( { email => $email } );
- }
- $emailUser->addSpecialState( 'Subscriber', $self->getId );
-
- $self->subscribe( $emailUser, 1 );
+ given ( $action ) {
+ when ( 'unsubscribe' ) { $self->unsubscribeAnonymous( $email ); }
+ when ( 'subscribe' ) { $self->subscribeAnonymous( $email ); }
}
}
else {
@@ -545,4 +742,3 @@ sub www_unsubscribe {
}
1;
-
diff --git a/lib/WebGUI/MailCommand.pm b/lib/WebGUI/MailCommand.pm
new file mode 100644
index 0000000..a37aca1
--- /dev/null
+++ b/lib/WebGUI/MailCommand.pm
@@ -0,0 +1,56 @@
+package WebGUI::MailCommand;
+
+use strict;
+use warnings;
+
+sub isValidCommand {
+ my $command = shift;
+
+ return defined resolveCommandClass( $command );
+}
+
+sub new {
+ my $class = shift;
+ my $session = shift || die "Need a session";
+
+ bless { _session => $session }, $class;
+}
+
+sub process {
+ WebGUI::Error::OverrideMe->throw;
+}
+
+sub processCommand {
+ my $session = shift;
+ my $command = shift;
+ my $parameter = shift;
+ my $sender = shift;
+
+ my $commandClass = resolveCommandClass( $command )
+ || return;
+
+ my $commandObject = WebGUI::Pluggable::instanciate( $commandClass, 'new', [ $session ] );
+
+ return $commandObject->process( $parameter, $sender );
+}
+
+sub session {
+ return (shift)->{ _session };
+}
+
+sub resolveCommandClass {
+ my $command = shift;
+
+ # TODO: Do not hard code.
+ my %commands = (
+ unsubscribe => 'WebGUI::MailCommand::Unsubscribe',
+ subscribe => 'WebGUI::MailCommand::Subscribe',
+ bounce => 'WebGUI::MailCommand::Bounce',
+ );
+
+ return $commands{ $command } if exists $commands{ $command };
+ return;
+}
+
+1;
+
diff --git a/lib/WebGUI/MailCommand/Bounce.pm b/lib/WebGUI/MailCommand/Bounce.pm
new file mode 100644
index 0000000..752195b
--- /dev/null
+++ b/lib/WebGUI/MailCommand/Bounce.pm
@@ -0,0 +1,40 @@
+package WebGUI::MailCommand::Bounce;
+
+use strict;
+use warnings;
+
+use WebGUI::Mailing::Email;
+use Mail::DeliveryStatus::BounceParser;
+
+use base 'WebGUI::MailCommand';
+
+#-----------------------------------------------------------------------------
+sub process {
+ my $self = shift;
+ my $hexId = shift;
+ my $session = $self->session;
+ my $log = $session->log;
+ my $id = $session->id->fromHex( $hexId );
+
+ my $email = WebGUI::Mailing::Email->new( $session, $id );
+
+ if ($email) {
+ my $dsr = Mail::DeliveryStatus::BounceParser->new( \*STDIN );
+
+ my $report = ( $dsr->reports )[0];
+ my $reason = $report->get( 'std_reason' );
+ my $message = $report->get( 'reason' );
+ $message =~ s{\s+}{ }g;
+
+ $log->warn( "Registering email [$id] as bounced." );
+ $email->registerBounced( $reason, $message );
+ }
+ else {
+ $log->error( "Cannot process bounced email [$id] because it cannot be located in the db." );
+ }
+
+ return;
+}
+
+1;
+
diff --git a/lib/WebGUI/MailCommand/Subscribe.pm b/lib/WebGUI/MailCommand/Subscribe.pm
new file mode 100644
index 0000000..2244b1b
--- /dev/null
+++ b/lib/WebGUI/MailCommand/Subscribe.pm
@@ -0,0 +1,28 @@
+package WebGUI::MailCommand::Subscribe;
+
+use strict;
+use warnings;
+
+use WebGUI::AssetAspect::Subscriber;
+
+use base 'WebGUI::MailCommand';
+
+#-----------------------------------------------------------------------------
+sub process {
+ my $self = shift;
+ my $listName = shift;
+ my $fromAddress = shift;
+ my $session = $self->session;
+ my $log = $session->log;
+
+ my $asset = WebGUI::AssetAspect::Subscriber->getAssetByListName( $session, $listName );
+
+ die "Invalid list name [$listName]" unless $asset;
+
+ $asset->subscribeAnonymous( $fromAddress );
+
+ return;
+}
+
+1;
+
diff --git a/lib/WebGUI/MailCommand/Unsubscribe.pm b/lib/WebGUI/MailCommand/Unsubscribe.pm
new file mode 100644
index 0000000..e7ca1ca
--- /dev/null
+++ b/lib/WebGUI/MailCommand/Unsubscribe.pm
@@ -0,0 +1,27 @@
+package WebGUI::MailCommand::Unsubscribe;
+
+use strict;
+use warnings;
+
+use WebGUI::AssetAspect::Subscriber;
+
+use base 'WebGUI::MailCommand';
+
+#-----------------------------------------------------------------------------
+sub process {
+ my $self = shift;
+ my $listName = shift;
+ my $fromAddress = shift;
+ my $session = $self->session;
+
+ my $asset = WebGUI::AssetAspect::Subscriber->getAssetByListName( $session, $listName );
+
+ die "Invalid list name [$listName]" unless $asset;
+
+ $asset->unsubscribeAnonymous( $fromAddress );
+
+ return;
+}
+
+1;
+
diff --git a/lib/WebGUI/Mailing.pm b/lib/WebGUI/Mailing.pm
index 14a2f0a..93d8124 100644
--- a/lib/WebGUI/Mailing.pm
+++ b/lib/WebGUI/Mailing.pm
@@ -5,6 +5,8 @@ use warnings;
use Carp;
use WebGUI::Mailing::Admin;
+use WebGUI::Mailing::Email;
+
use JSON qw{ to_json };
use base 'WebGUI::Crud';
@@ -38,7 +40,7 @@ sub cancel {
);
};
- $self->update( {
+ $self->update( {
state => 'idle',
sendDate => undef,
} );
@@ -63,7 +65,7 @@ sub crud_definition {
assetId => {
fieldType => 'guid',
},
-
+
issueId => {
fieldType => 'guid',
},
@@ -81,7 +83,7 @@ sub crud_definition {
},
);
- $definition->{ properties } = {
+ $definition->{ properties } = {
%{ $definition->{ properties } || {} },
%properties,
};
@@ -92,7 +94,7 @@ sub crud_definition {
#----------------------------------------------------------------------------
sub delete {
my $self = shift;
-
+
$self->deleteQueuedEmails;
return $self->SUPER::delete;
@@ -101,12 +103,17 @@ sub delete {
#----------------------------------------------------------------------------
sub deleteQueuedEmails {
my $self = shift;
-
+
my $it = $self->getQueuedEmailIterator;
while ( my $email = $it->() ) {
$email->delete;
}
+ $it = $self->getQueuedTestEmailIterator;
+ while ( my $email = $it->() ) {
+ $email->delete;
+ }
+
return;
}
@@ -144,6 +151,14 @@ sub getQueuedEmailIterator {
return WebGUI::Mailing::Email->getQueued( $session, $self->getId );
}
+#----------------------------------------------------------------------------
+sub getQueuedTestEmailIterator {
+ my $self = shift;
+ my $session = $self->session;
+
+ return WebGUI::Mailing::Email->getQueuedTestEmails( $session, $self->getId );
+}
+
#----------------------------------------------------------------------------
sub getNextInSendQueue {
my $class = shift;
@@ -165,8 +180,8 @@ sub getStatusLine {
my $self = shift;
my $db = $self->session->db;
- my $sth = $db->read(
- 'select status, isTest, count( status ) as cnt from Mailing_email where mailingId=? group by status,isTest',
+ my $sth = $db->read(
+ 'select status, isTest, count( status ) as cnt from Mailing_email where mailingId=? group by status,isTest',
[
$self->getId,
],
@@ -245,7 +260,7 @@ sub queue {
$state,
);
};
-
+
$self->queueEmails( $self->getAsset->getRecipients );
$self->update( {
@@ -270,7 +285,7 @@ sub queueEmails {
recipientEmail => undef,
isTest => 0,
} );
-
+
}
return;
@@ -319,7 +334,7 @@ sub send {
$state,
);
};
-
+
$self->update( { state => 'sending' } );
my $complete = $self->sendQueuedEmails( $timeLimit );
@@ -346,7 +361,7 @@ sub sendQueuedEmails {
$email->send;
}
-
+
return 1;
}
@@ -365,7 +380,7 @@ sub www_cancel {
$i18n->get('cancel mailing success'),
$i18n->get('cancel mailing'),
);
- }
+ }
else {
return $self->renderInConsole(
$i18n->get('cancel mailing failure'),
@@ -400,13 +415,13 @@ sub www_delete {
#----------------------------------------------------------------------------
sub www_edit {
- my $self = shift;
- my $session = $self->session;
- my $i18n = WebGUI::International->new( $session, 'MailingManager' );
+ my $self = shift;
+ my $session = $self->session;
+ my $i18n = WebGUI::International->new( $session, 'MailingManager' );
return $session->privilege->insufficient unless $self->admin->canManage;
- my $f = WebGUI::HTMLForm->new( $session );
+ my $f = $self->getEditForm;
$f->hidden(
name => 'newsletter',
value => 'mailing',
@@ -420,16 +435,29 @@ sub www_edit {
value => $self->getId,
);
+ return $self->renderInConsole( $f->print, $i18n->get('configure mailing') );
+}
+
+#----------------------------------------------------------------------------
+sub getEditForm {
+ my $self = shift;
+ my $session = $self->session;
+ my $i18n = WebGUI::International->new( $session, 'MailingManager' );
+ my $f = WebGUI::HTMLForm->new( $session );
+
my %fields = %{ $self->getAsset->getMailingProperties( $self ) };
my $configuration = $self->get('configuration') || {};
while ( my( $name, $properties ) = each %fields ) {
$properties->{ name } = $name;
- $properties->{ value } = $configuration->{ $name } if exists $properties->{ value };
+ $properties->{ value } = $configuration->{ $name } if exists $configuration->{ $name };
my $formField = WebGUI::Form::DynamicField->new( $session, %{ $properties } );
- my $html = $self->admin->canOverride
- ? $formField->toHtml
- : $formField->getValueAsHtml
+ my $element = $formField->toHtml; # Works around a bug in WG::Form::Template in < 7.9.8
+ my $readonly = $formField->getValueAsHtml; # where getValueAsHtml wouldn't set the correct options hashref
+
+ my $html = $self->admin->canOverride || $properties->{ managerCanEdit }
+ ? $element
+ : $readonly
;
$f->readOnly(
@@ -440,12 +468,12 @@ sub www_edit {
$f->submit( value => $i18n->get( 'generate mailing' ) );
my $cancelUrl = $session->url->page( 'newsletter=manage' );
- $f->button(
+ $f->button(
value => $i18n->get( 'cancel' ),
extras => qq{onclick="window.location='$cancelUrl'" class="backwardButton"},
);
- return $self->renderInConsole( $f->print, $i18n->get('configure mailing') );
+ return $f;
}
#----------------------------------------------------------------------------
@@ -468,7 +496,7 @@ sub www_previewEmail {
my $session = $self->session;
my ( $form, $url ) = $session->quick( 'form', 'url' );
my $i18n = WebGUI::International->new( $session, 'MailingManager' );
-
+
return $session->privilege->insufficient unless $self->admin->canManage;
my $asset = $self->getAsset;
@@ -476,7 +504,7 @@ sub www_previewEmail {
my $manageUrl = $url->page('newsletter=manage');
my $subject = $asset->getSubject( $self->get('configuration') );
- my $userSelection =
+ my $userSelection =
qq{