SMS coding complete, beginning testing.
Added getInboxAddresses method to User.pm, with tests. All SMS/Email address choices are made in Mail/Send, on create and on send, since that's when it handles groups. Test variable addresses on creating mails. Updated t/smtpd.pl to handle receiving and sending multiple messages.
This commit is contained in:
parent
bc3b204d9b
commit
7336d2d630
6 changed files with 174 additions and 51 deletions
|
|
@ -151,16 +151,17 @@ sub create {
|
|||
|
||||
my $subject = (defined $properties->{emailSubject}) ? $properties->{emailSubject} : $self->{_properties}{subject};
|
||||
my $mail = WebGUI::Mail::Send->create($session, {
|
||||
toUser=>$self->{_properties}{userId},
|
||||
toGroup=>$self->{_properties}{groupId},
|
||||
subject=>$subject,
|
||||
});
|
||||
toUser => $self->{_properties}{userId},
|
||||
toGroup => $self->{_properties}{groupId},
|
||||
subject => $subject,
|
||||
},
|
||||
'fromInbox',
|
||||
);
|
||||
if (defined $mail) {
|
||||
my $preface = "";
|
||||
my $fromUser = WebGUI::User->new($session, $properties->{sentBy});
|
||||
#Don't append prefaces to the visitor users or messages that don't specify a user (default case)
|
||||
unless ($fromUser->isVisitor || $fromUser->userId eq 3) { #Can't use isAdmin because it will not send prefaces from normal users who in the admin group
|
||||
my $i18n = WebGUI::International->new($session, 'Inbox_Message');
|
||||
$preface = sprintf($i18n->get('from user preface'), $fromUser->username);
|
||||
}
|
||||
my $msg = (defined $properties->{emailMessage}) ? $properties->{emailMessage} : $self->{_properties}{message};
|
||||
|
|
|
|||
|
|
@ -267,16 +267,28 @@ A unique id for this message, in case you want to see what replies come in for i
|
|||
|
||||
If this is a reply to a previous message, then you should specify the messageId of the previous message here.
|
||||
|
||||
=head3 isInbox
|
||||
|
||||
A flag indicating that this email message is from the Inbox, and should follow per user settings
|
||||
for delivery.
|
||||
|
||||
=cut
|
||||
|
||||
sub create {
|
||||
my $class = shift;
|
||||
my $session = shift;
|
||||
my $headers = shift;
|
||||
my $isInbox = shift;
|
||||
if ($headers->{toUser}) {
|
||||
my $user = WebGUI::User->new($session, $headers->{toUser});
|
||||
if (defined $user) {
|
||||
my $email = $user->profileField("email");
|
||||
my $email;
|
||||
if ($isInbox) {
|
||||
$email = $user->getInboxAddresses;
|
||||
}
|
||||
else {
|
||||
$email = $user->profileField("email");
|
||||
}
|
||||
if ($email) {
|
||||
if ($headers->{to}) {
|
||||
$headers->{to} .= ','.$email;
|
||||
|
|
@ -286,9 +298,9 @@ sub create {
|
|||
}
|
||||
}
|
||||
}
|
||||
my $from = $headers->{from} || $session->setting->get('comanyName') . " <".$session->setting->get("companyEmail").">";
|
||||
my $from = $headers->{from} || $session->setting->get('comanyName') . " <".$session->setting->get("companyEmail").">";
|
||||
my $type = $headers->{contentType} || "multipart/mixed";
|
||||
my $replyTo = $headers->{replyTo} || $session->setting->get("mailReturnPath");
|
||||
my $replyTo = $headers->{replyTo} || $session->setting->get("mailReturnPath");
|
||||
|
||||
# format of Message-Id should be '<unique-id@domain>'
|
||||
my $id = $headers->{messageId} || "WebGUI-" . $session->id->generate;
|
||||
|
|
@ -324,7 +336,7 @@ sub create {
|
|||
delete $headers->{toGroup};
|
||||
$message->attach(Data=>"This message was intended for ".$to." but was overridden in the config file.\n\n");
|
||||
}
|
||||
bless {_message=>$message, _session=>$session, _toGroup=>$headers->{toGroup} }, $class;
|
||||
bless {_message=>$message, _session=>$session, _toGroup=>$headers->{toGroup}, _isInbox => $isInbox }, $class;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -467,10 +479,10 @@ sub send {
|
|||
else {
|
||||
my $smtp = Net::SMTP->new($smtpServer); # connect to an SMTP server
|
||||
if (defined $smtp) {
|
||||
$smtp->mail($mail->head->get("X-Return-Path"));
|
||||
$smtp->to(split(",",$mail->head->get("to")));
|
||||
$smtp->cc(split(",",$mail->head->get("cc")));
|
||||
$smtp->bcc(split(",",$mail->head->get("bcc")));
|
||||
$smtp->mail($mail->head->get('X-Return-Path'));
|
||||
$smtp->to( split(',', $mail->head->get('to') ));
|
||||
$smtp->cc( split(',', $mail->head->get('cc') ));
|
||||
$smtp->bcc( split(',', $mail->head->get('bcc') ));
|
||||
$smtp->data(); # Start the mail
|
||||
$smtp->datasend($mail->stringify);
|
||||
$smtp->dataend(); # Finish sending the mail
|
||||
|
|
@ -490,15 +502,21 @@ sub send {
|
|||
if ($group) {
|
||||
my $group = WebGUI::Group->new($self->session, $group);
|
||||
return $status if !defined $group;
|
||||
$mail->head->replace("bcc", undef);
|
||||
$mail->head->replace("cc", undef);
|
||||
foreach my $userId (@{$group->getAllUsers(1)}) {
|
||||
$mail->head->replace('bcc', undef);
|
||||
$mail->head->replace('cc', undef);
|
||||
USER: foreach my $userId (@{$group->getAllUsers(1)}) {
|
||||
my $user = WebGUI::User->new($self->session, $userId);
|
||||
next unless $user->status eq 'Active'; ##Don't send this to invalid user accounts
|
||||
if ($user->profileField("email")) {
|
||||
$mail->head->replace("To",$user->profileField("email"));
|
||||
$self->queue;
|
||||
next USER unless $user->status eq 'Active'; ##Don't send this to invalid user accounts
|
||||
my $emailAddress;
|
||||
if ($self->{_isInbox}) {
|
||||
$emailAddress = $user->getInboxAddresses;
|
||||
}
|
||||
else {
|
||||
$emailAddress = $user->profileField('email');
|
||||
}
|
||||
next USER unless $emailAddress;
|
||||
$mail->head->replace('To', $emailAddress);
|
||||
$self->queue;
|
||||
}
|
||||
#Delete the group if it is flagged as an AdHocMailGroup
|
||||
$group->delete if ($group->isAdHocMailGroup);
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ my $session = start( $webguiRoot, $configFile );
|
|||
|
||||
installUserProfileFields($session);
|
||||
installSettings($session);
|
||||
upgradeMailQueue($session);
|
||||
|
||||
# Do your work here
|
||||
finish($session);
|
||||
|
|
@ -58,7 +59,7 @@ sub installUserProfileFields {
|
|||
);
|
||||
WebGUI::ProfileField->create(
|
||||
$session,
|
||||
'receiveInboxSMSNotifications',
|
||||
'receiveInboxSmsNotifications',
|
||||
{
|
||||
label => q!WebGUI::International::get('receive inbox sms','Message_Center')!,
|
||||
visible => 1,
|
||||
|
|
@ -77,6 +78,11 @@ sub installSettings {
|
|||
$session->setting->add('smsGateway', '');
|
||||
}
|
||||
|
||||
sub upgradeMailQueue {
|
||||
my $session = shift;
|
||||
$session->db->write('alter table mailQueue add column isInbox TINYINT(4) default 0');
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub start {
|
||||
my $webguiRoot = shift;
|
||||
|
|
|
|||
136
t/Mail/Send.t
136
t/Mail/Send.t
|
|
@ -17,10 +17,13 @@ use strict;
|
|||
use lib "$FindBin::Bin/../lib";
|
||||
use JSON qw( from_json to_json );
|
||||
use Test::More;
|
||||
use Test::Deep;
|
||||
use File::Spec;
|
||||
use Data::Dumper;
|
||||
use WebGUI::Test;
|
||||
|
||||
use WebGUI::Mail::Send;
|
||||
use WebGUI::User;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
|
|
@ -43,13 +46,17 @@ my $SMTP_PORT = '54921';
|
|||
if ($hasServer) {
|
||||
$oldSettings{ smtpServer } = $session->setting->get('smtpServer');
|
||||
$session->setting->set( 'smtpServer', $SMTP_HOST . ':' . $SMTP_PORT );
|
||||
|
||||
|
||||
my $smtpd = File::Spec->catfile( WebGUI::Test->root, 't', 'smtpd.pl' );
|
||||
open MAIL, "perl $smtpd $SMTP_HOST $SMTP_PORT 4 |"
|
||||
or die "Could not open pipe to SMTPD: $!";
|
||||
sleep 1; # Give the smtpd time to establish itself
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
plan tests => 6; # Increment this number for each test you create
|
||||
plan tests => 9; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Test create
|
||||
|
|
@ -86,7 +93,7 @@ is( $mime->parts(0)->as_string =~ m/\n/, $newlines,
|
|||
#----------------------------------------------------------------------------
|
||||
# Test addHtml
|
||||
$mail = WebGUI::Mail::Send->create( $session );
|
||||
my $text = <<'EOF';
|
||||
$text = <<'EOF';
|
||||
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse eu lacus ut ligula fringilla elementum. Cras condimentum, velit commodo pretium semper, odio ante accumsan orci, a ultrices risus justo a nulla. Aliquam erat volutpat.
|
||||
EOF
|
||||
|
||||
|
|
@ -97,7 +104,7 @@ $mime = $mail->getMimeEntity;
|
|||
# TODO: Test that addHtml creates a body with the right content type
|
||||
|
||||
# addHtml should add newlines after 78 characters
|
||||
my $newlines = length $text / 78;
|
||||
$newlines = length $text / 78;
|
||||
is( $mime->parts(0)->as_string =~ m/\n/, $newlines,
|
||||
"addHtml should add newlines after 78 characters",
|
||||
);
|
||||
|
|
@ -107,7 +114,7 @@ is( $mime->parts(0)->as_string =~ m/\n/, $newlines,
|
|||
#----------------------------------------------------------------------------
|
||||
# Test addHtmlRaw
|
||||
$mail = WebGUI::Mail::Send->create( $session );
|
||||
my $text = <<'EOF';
|
||||
$text = <<'EOF';
|
||||
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse eu lacus ut ligula fringilla elementum. Cras condimentum, velit commodo pretium semper, odio ante accumsan orci, a ultrices risus justo a nulla. Aliquam erat volutpat.
|
||||
EOF
|
||||
|
||||
|
|
@ -117,13 +124,15 @@ $mime = $mail->getMimeEntity;
|
|||
# TODO: Test that addHtmlRaw doesn't add an HTML wrapper
|
||||
|
||||
# addHtmlRaw should add newlines after 78 characters
|
||||
my $newlines = length $text / 78;
|
||||
$newlines = length $text / 78;
|
||||
is( $mime->parts(0)->as_string =~ m/\n/, $newlines,
|
||||
"addHtmlRaw should add newlines after 78 characters",
|
||||
);
|
||||
|
||||
# TODO: Test that addHtml creates a body with the right content type
|
||||
|
||||
my $smtpServerOk = 0;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Test emailOverride
|
||||
SKIP: {
|
||||
|
|
@ -138,23 +147,25 @@ SKIP: {
|
|||
if ( !$hasServer ) {
|
||||
skip "Cannot test emailOverride: Module Net::SMTP::Server not loaded!", $numtests;
|
||||
}
|
||||
|
||||
|
||||
$smtpServerOk = 1;
|
||||
|
||||
# Override the emailOverride
|
||||
my $oldEmailOverride = $session->config->get('emailOverride');
|
||||
$session->config->set( 'emailOverride', 'dufresne@localhost' );
|
||||
my $oldEmailToLog = $session->config->get('emailToLog');
|
||||
$session->config->set( 'emailToLog', 0 );
|
||||
|
||||
|
||||
# Send the mail
|
||||
my $mail
|
||||
= WebGUI::Mail::Send->create( $session, {
|
||||
to => 'norton@localhost',
|
||||
} );
|
||||
$mail->addText( 'His judgement cometh and that right soon.' );
|
||||
|
||||
|
||||
my $received = sendToServer( $mail );
|
||||
|
||||
if (!$received) {
|
||||
|
||||
if ($received->{error}) {
|
||||
skip "Cannot test emailOverride: No response received from smtpd", $numtests;
|
||||
}
|
||||
|
||||
|
|
@ -168,6 +179,79 @@ SKIP: {
|
|||
$session->config->set( 'emailToLog', $oldEmailToLog );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
#
|
||||
# Test sending an Inbox message to a user who has various notifications configured
|
||||
#
|
||||
#----------------------------------------------------------------------------
|
||||
|
||||
my $inboxUser = WebGUI::User->create($session);
|
||||
$inboxUser->username('red');
|
||||
$inboxUser->profileField('receiveInboxEmailNotifications', 1);
|
||||
$inboxUser->profileField('receiveInboxSmsNotifications', 0);
|
||||
$inboxUser->profileField('email', 'ellis_boyd_redding@shawshank.gov');
|
||||
$inboxUser->profileField('cellPhone', '55555');
|
||||
$oldSettings{smsGateway} = $session->setting->get('smsGateway');
|
||||
$session->setting->set('smsGateway', 'textme.com');
|
||||
|
||||
SKIP: {
|
||||
my $numtests = 1; # Number of tests in this block
|
||||
|
||||
# Must be able to write the config, or we'll die
|
||||
skip "Cannot test email notifications", $numtests unless $smtpServerOk;
|
||||
|
||||
# Send the mail
|
||||
$mail = WebGUI::Mail::Send->create( $session, {
|
||||
toUser => $inboxUser->userId,
|
||||
},
|
||||
'fromInbox',
|
||||
);
|
||||
$mail->addText( 'sent via email' );
|
||||
|
||||
my $received = sendToServer( $mail ) ;
|
||||
|
||||
# Test the mail
|
||||
is($received->{to}->[0], '<ellis_boyd_redding@shawshank.gov>', 'send, toUser with email address');
|
||||
|
||||
$inboxUser->profileField('receiveInboxEmailNotifications', 0);
|
||||
$inboxUser->profileField('receiveInboxSmsNotifications', 1);
|
||||
|
||||
# Send the mail
|
||||
$mail = WebGUI::Mail::Send->create( $session, {
|
||||
toUser => $inboxUser->userId,
|
||||
},
|
||||
'fromInbox',
|
||||
);
|
||||
$mail->addText( 'sent via SMS' );
|
||||
|
||||
my $received = sendToServer( $mail ) ;
|
||||
|
||||
# Test the mail
|
||||
is($received->{to}->[0], '<55555@textme.com>', 'send, toUser with SMS address');
|
||||
|
||||
$inboxUser->profileField('receiveInboxEmailNotifications', 1);
|
||||
$inboxUser->profileField('receiveInboxSmsNotifications', 1);
|
||||
|
||||
# Send the mail
|
||||
$mail = WebGUI::Mail::Send->create( $session, {
|
||||
toUser => $inboxUser->userId,
|
||||
},
|
||||
'fromInbox',
|
||||
);
|
||||
$mail->addText( 'sent via SMS' );
|
||||
|
||||
my $received = sendToServer( $mail ) ;
|
||||
|
||||
# Test the mail
|
||||
cmp_bag(
|
||||
$received->{to},
|
||||
['<55555@textme.com>', '<ellis_boyd_redding@shawshank.gov>',],
|
||||
'send, toUser with SMS and email addresses'
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
# TODO: Test the emailToLog config setting
|
||||
|
||||
|
||||
|
|
@ -177,6 +261,14 @@ END {
|
|||
for my $name ( keys %oldSettings ) {
|
||||
$session->setting->set( $name, $oldSettings{ $name } );
|
||||
}
|
||||
|
||||
if ($inboxUser) {
|
||||
$inboxUser->delete;
|
||||
}
|
||||
|
||||
close MAIL
|
||||
or die "Could not close pipe to SMTPD: $!";
|
||||
sleep 1;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -193,21 +285,17 @@ END {
|
|||
# by a MIME::Entity parser
|
||||
sub sendToServer {
|
||||
my $mail = shift;
|
||||
|
||||
my $smtpd = File::Spec->catfile( WebGUI::Test->root, 't', 'smtpd.pl' );
|
||||
open MAIL, "perl $smtpd $SMTP_HOST $SMTP_PORT |"
|
||||
or die "Could not open pipe to SMTPD: $!";
|
||||
sleep 1; # Give the smtpd time to establish itself
|
||||
|
||||
$mail->send;
|
||||
my $status = $mail->send;
|
||||
my $json;
|
||||
while ( my $line = <MAIL> ) {
|
||||
$json .= $line;
|
||||
if ($status) {
|
||||
$json = <MAIL>;
|
||||
}
|
||||
else {
|
||||
$json = ' { "error": "mail not sent" } ';
|
||||
}
|
||||
if (!$json) {
|
||||
$json = ' { "error": "error in getting mail" } ';
|
||||
}
|
||||
|
||||
close MAIL
|
||||
or die "Could not close pipe to SMTPD: $!";
|
||||
|
||||
return from_json( $json );
|
||||
}
|
||||
|
||||
|
|
|
|||
8
t/User.t
8
t/User.t
|
|
@ -658,17 +658,17 @@ $inmate->profileField('email', 'andy@shawshank.com');
|
|||
is ($inmate->getInboxAddresses, 'andy@shawshank.com', 'getInboxAddresses: email address only');
|
||||
|
||||
$inmate->profileField('receiveInboxSmsNotifications', 1);
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com', 'getInboxAddresses: receive only email address, with receiveInboSmsNotifications=1 but no other profile info');
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com', 'getInboxAddresses: receive only email address, with receiveInboSMSNotifications=1 but no other profile info');
|
||||
|
||||
$inmate->profileField('cellPhone', '37927');
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com', 'getInboxAddresses: receive only email address, with receiveInboSmsNotifications=1 and cell phone but no gateway');
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com', 'getInboxAddresses: receive only email address, with receiveInboSMSNotifications=1 and cell phone but no gateway');
|
||||
|
||||
$inmate->profileField('cellPhone', '');
|
||||
$session->setting->set('smsGateway', 'textme.com');
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com', 'getInboxAddresses: receive only email address, with receiveInboSmsNotifications=1 and gateway but no cell phone');
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com', 'getInboxAddresses: receive only email address, with receiveInboSMSNotifications=1 and gateway but no cell phone');
|
||||
|
||||
$inmate->profileField('cellPhone', '37927');
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com,37927@textme.com', 'getInboxAddresses: receive only email address, with receiveInboSmsNotifications=1 and gateway but no cell phone');
|
||||
is ($inmate->getInboxAddresses, 'andy@shawshank.com,37927@textme.com', 'getInboxAddresses: receive only email address, with receiveInboSMSNotifications=1 and gateway but no cell phone');
|
||||
|
||||
$inmate->profileField('receiveInboxEmailNotifications', 0);
|
||||
is ($inmate->getInboxAddresses, '37927@textme.com', 'getInboxAddresses: can get SMS and no email, even with email info present');
|
||||
|
|
|
|||
14
t/smtpd.pl
14
t/smtpd.pl
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
my $HOST = shift;
|
||||
my $PORT = shift;
|
||||
my $EMAILS = shift || 1;
|
||||
|
||||
die "HOST must be first argument"
|
||||
unless $HOST;
|
||||
|
|
@ -13,17 +14,26 @@ use Net::SMTP::Server::Client;
|
|||
|
||||
my $server = Net::SMTP::Server->new( $HOST, $PORT );
|
||||
|
||||
while ( my $conn = $server->accept ) {
|
||||
my $counter = 0;
|
||||
|
||||
$| = 1;
|
||||
|
||||
CONNECTION: while ( my $conn = $server->accept ) {
|
||||
my $client = Net::SMTP::Server::Client->new( $conn );
|
||||
$client->process;
|
||||
print to_json({
|
||||
to => $client->{TO},
|
||||
from => $client->{FROM},
|
||||
contents => $client->{MSG},
|
||||
counter => $counter,
|
||||
emails => $EMAILS,
|
||||
});
|
||||
exit(0);
|
||||
print "\n";
|
||||
last CONNECTION if ++$counter >= $EMAILS;
|
||||
}
|
||||
|
||||
sleep 3;
|
||||
exit(0);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue