Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package PEEPSMailer;use strict;use Exporter 'import';use Net::SMTPS;use Email::Simple;use WebDB;use HTML::Tiny;#use Email::MessageID;our @EXPORT = qw( EmailUser EmailMultipleUsers emailAffiliationRequest emailAffiliationAdded emailAffiliationRemoved emailNewLeagueRole );my $server = "smtp.gmail.com";my $port = 587;my $user = 'peeps@wftda.com';my $password = 'pmrpegrigryhavpe';my $h = HTML::Tiny->new( mode => 'html' );#warn "loaded PEEPSMailer...";sub EmailUser {my $email = shift;my $subject = shift;my $body = shift;my $dev = $ENV{SERVER_NAME} eq "peeps.wftda.com" ? "" : '[DEV] ';if ($body !~ /^<p>/i) {$body = $h->p (split "\n", $body);}#warn "DEBUG: Emailing $email...";IO::Socket::SSL::set_client_defaults(SSL_verifycn_name => 'smtp.gmail.com',);my $smtp = Net::SMTPS->new($server,Hello => 'gmail.com',Port => $port,doSSL => 'starttls',Timeout => 15,Debug => 0);die "Could not connect to SMTP server!" unless $smtp;my $msg = Email::Simple->create(header => [To => $email,# Bcc => $user,From => $user,Subject => $dev.$subject,# 'Message-Id' => Email::MessageID->new->in_brackets,'Content-type' => 'text/html',],body => $body,);$smtp->auth ($user, $password);$smtp->mail ($user);$smtp->to ($email);# $smtp->bcc ($user);$smtp->data ();$smtp->datasend ($msg->as_string);$smtp->quit;}sub EmailMultipleUsers {my $parameters = shift;my $email = join ",", @{$parameters->{to}};my $cc = join ",", @{$parameters->{cc}};my $subject = $parameters->{subject};my $body = $parameters->{body};my $dev = $ENV{SERVER_NAME} eq "peeps.wftda.com" ? "" : '[DEV] ';if ($body !~ /^<p>/i) {$body = $h->p (split "\n", $body);}#warn "DEBUG: Emailing $email...";IO::Socket::SSL::set_client_defaults(SSL_verifycn_name => 'smtp.gmail.com',);my $smtp = Net::SMTPS->new($server,Hello => 'gmail.com',Port => $port,doSSL => 'starttls',Timeout => 15,Debug => 0);die "Could not connect to SMTP server!" unless $smtp;my $msg = Email::Simple->create(header => [To => $email,Cc => $cc,# Bcc => $user,From => $user,Subject => $dev.$subject,# 'Message-Id' => Email::MessageID->new->in_brackets,'Content-type' => 'text/html',],body => $body,);$smtp->auth ($user, $password);$smtp->mail ($user);$smtp->to ($email);$smtp->cc ($cc);# $smtp->bcc ($user);$smtp->data ();$smtp->datasend ($msg->as_string);$smtp->quit;}sub emailAffiliationRequest {my $newpersonid = shift // return;my $leagueid = shift // return;my $newb = PEEPS::getUser ($newpersonid);my $dbh = PEEPS::getRCDBH ();my @leagueAdmins = map { @{$_} } @{ $dbh->selectall_arrayref ("select email from role join person on role.person_id = person.id where role.member_org_id = ? and role.role = ?", undef, $leagueid, "League Admin") };my $leagueName = PEEPS::getLeagueName ($leagueid);my $subject = $leagueName." - New Affiliation Request";my $body = $h->p ($h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),"Greetings,","You have a new League Affiliation Request for $leagueName:".$h->br,"Derby Name: ".$newb->{derby_name}.$h->br."Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br."PEEPS ID: ".$newb->{id}.$h->br.$h->br,"Log into ".$h->a ({href=>CGI::url ()}, "WFTDA PEEPS")." to accept or reject the affiliation request.".$h->br.$h->br,"-WFTDA PEEPS","NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!");PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });}sub emailAffiliationAdded {my $newpersonid = shift // return;my $leagueid = shift // return;my $newb = PEEPS::getUser ($newpersonid);my $dbh = PEEPS::getRCDBH ();my @leagueAdmins = map { @{$_} } @{ $dbh->selectall_arrayref ("select email from role join person on role.person_id = person.id where role.member_org_id = ? and role.role = ?", undef, $leagueid, "League Admin") };my ($role) = $dbh->selectrow_array ("select role from role where person_id = ? and member_org_id = ?", undef, $newpersonid, $leagueid);my $leagueName = PEEPS::getLeagueName ($leagueid);my $subject = $leagueName." - Affiliation Confirmed";my $body = $h->p ($h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),"Greetings,","League Affiliation for $leagueName has been confirmed:".$h->br,"Derby Name: ".$newb->{derby_name}.$h->br."Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br."PEEPS ID: ".$newb->{id}.$h->br,"Role: ".$role.$h->br.$h->br,"No further action is necessary.".$h->br.$h->br,"-WFTDA PEEPS","NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!");PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });}sub emailNewLeagueRole {my $newpersonid = shift // return;my $leagueid = shift // return;my $newrole = shift // return;my $newb = PEEPS::getUser ($newpersonid);my $dbh = PEEPS::getRCDBH ();my @leagueAdmins = map { @{$_} } @{ $dbh->selectall_arrayref ("select email from role join person on role.person_id = person.id where role.member_org_id = ? and role.role = ?", undef, $leagueid, "League Admin") };my $leagueName = PEEPS::getLeagueName ($leagueid);my $subject = $leagueName." - New ".$newrole;my $body = $h->p ($h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),"Greetings,","A new role has been added for $leagueName:".$h->br,"Derby Name: ".$newb->{derby_name}.$h->br."Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br."PEEPS ID: ".$newb->{id}.$h->br,"Role: ".$newrole.$h->br.$h->br,"No further action is necessary.".$h->br.$h->br,"-WFTDA PEEPS","NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!");PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });}sub emailAffiliationRemoved {my $personid = shift // return;my $leagueid = shift // return;my $newb = PEEPS::getUser ($personid);my $dbh = PEEPS::getRCDBH ();my @leagueAdmins = map { @{$_} } @{ $dbh->selectall_arrayref ("select email from role join person on role.person_id = person.id where role.member_org_id = ? and role.role = ?", undef, $leagueid, "League Admin") };my $leagueName = PEEPS::getLeagueName ($leagueid);my $subject = $leagueName." - Affiliation Removed";my $body = $h->p ($h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),"Greetings,","League Affiliation has been removed from $leagueName:".$h->br,"Derby Name: ".$newb->{derby_name}.$h->br."Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br."PEEPS ID: ".$newb->{id}.$h->br.$h->br,"No further action is necessary.".$h->br.$h->br,"-WFTDA PEEPS","NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!");PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });}1;