Subversion Repositories PEEPS

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
package PEEPSMailer;
2
 
3
use strict;
4
use Exporter 'import';
5
use Net::SMTPS;
6
use Email::Simple;
19 - 7
use WebDB;
8
use HTML::Tiny;
9
 
2 - 10
#use Email::MessageID;
11
 
19 - 12
our @EXPORT = qw( EmailUser EmailMultipleUsers emailAffiliationRequest emailAffiliationAdded emailAffiliationRemoved emailNewLeagueRole );
2 - 13
 
14
my $server   = "smtp.gmail.com";
15
my $port     = 587;
16
my $user     = 'peeps@wftda.com';
17
my $password = 'pmrpegrigryhavpe';
18
 
19 - 19
my $h = HTML::Tiny->new( mode => 'html' );
20
 
2 - 21
#warn "loaded PEEPSMailer...";
22
 
23
sub EmailUser {
24
	my $email = shift;
25
	my $subject = shift;
26
	my $body = shift;
27
 
28
	my $dev = $ENV{SERVER_NAME} eq "peeps.wftda.com" ? "" : '[DEV] ';
29
 
30
  if ($body !~ /^<p>/i) {
19 - 31
    $body = $h->p (split "\n", $body);
32
  }
33
 
34
#warn "DEBUG: Emailing $email...";
35
 
36
  IO::Socket::SSL::set_client_defaults(
37
    SSL_verifycn_name => 'smtp.gmail.com',
38
  );
39
 
40
  my $smtp = Net::SMTPS->new(
41
    $server,
42
    Hello => 'gmail.com',
43
    Port => $port,
44
    doSSL => 'starttls',
45
    Timeout => 15,
46
    Debug => 0
47
  );
48
  die "Could not connect to SMTP server!" unless $smtp;
49
 
50
	my $msg = Email::Simple->create(
51
	  header => [
52
	    To             => $email,
53
#	    Bcc            => $user,
54
	    From           => $user,
55
	    Subject        => $dev.$subject,
56
#	    'Message-Id'   => Email::MessageID->new->in_brackets,
57
	    'Content-type' => 'text/html',
58
    ],
59
	  body => $body,
60
	);
2 - 61
 
19 - 62
  $smtp->auth ($user, $password);
63
  $smtp->mail ($user);
64
  $smtp->to ($email);
65
#  $smtp->bcc ($user);
66
  $smtp->data ();
67
  $smtp->datasend ($msg->as_string);
68
  $smtp->quit;
69
 
70
}
71
 
72
 
73
sub EmailMultipleUsers {
74
  my $parameters = shift;
75
	my $email = join ",", @{$parameters->{to}};
76
	my $cc    = join ",", @{$parameters->{cc}};
77
	my $subject = $parameters->{subject};
78
	my $body = $parameters->{body};
79
 
80
	my $dev = $ENV{SERVER_NAME} eq "peeps.wftda.com" ? "" : '[DEV] ';
81
 
82
  if ($body !~ /^<p>/i) {
2 - 83
    $body = $h->p (split "\n", $body);
84
  }
85
 
86
#warn "DEBUG: Emailing $email...";
19 - 87
 
2 - 88
  IO::Socket::SSL::set_client_defaults(
89
    SSL_verifycn_name => 'smtp.gmail.com',
90
  );
91
 
92
  my $smtp = Net::SMTPS->new(
93
    $server,
94
    Hello => 'gmail.com',
95
    Port => $port,
96
    doSSL => 'starttls',
97
    Timeout => 15,
98
    Debug => 0
99
  );
100
  die "Could not connect to SMTP server!" unless $smtp;
101
 
102
	my $msg = Email::Simple->create(
103
	  header => [
104
	    To             => $email,
19 - 105
	    Cc             => $cc,
2 - 106
#	    Bcc            => $user,
107
	    From           => $user,
108
	    Subject        => $dev.$subject,
109
#	    'Message-Id'   => Email::MessageID->new->in_brackets,
110
	    'Content-type' => 'text/html',
111
    ],
112
	  body => $body,
113
	);
114
 
115
  $smtp->auth ($user, $password);
116
  $smtp->mail ($user);
117
  $smtp->to ($email);
19 - 118
  $smtp->cc ($cc);
2 - 119
#  $smtp->bcc ($user);
120
  $smtp->data ();
121
  $smtp->datasend ($msg->as_string);
122
  $smtp->quit;
123
 
124
}
125
 
126
 
19 - 127
 
128
sub emailAffiliationRequest {
129
  my $newpersonid = shift // return;
130
  my $leagueid = shift // return;
131
 
132
  my $newb = PEEPS::getUser ($newpersonid);
133
  my $dbh = PEEPS::getRCDBH ();
134
  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") };
135
 
136
  my $leagueName = PEEPS::getLeagueName ($leagueid);
137
 
138
  my $subject = $leagueName." - New Affiliation Request";
139
  my $body = $h->p (
140
                      $h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),
141
                      "Greetings,",
142
                      "You have a new League Affiliation Request for $leagueName:".$h->br,
143
                      "Derby Name: ".$newb->{derby_name}.$h->br.
144
                      "Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br.
145
                      "PEEPS ID: ".$newb->{id}.$h->br.$h->br,
146
                      "Log into ".$h->a ({href=>CGI::url ()}, "WFTDA PEEPS")." to accept or reject the affiliation request.".$h->br.$h->br,
147
                      "-WFTDA PEEPS",
148
                      "NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!"
149
                   );
150
 
151
  PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });
152
}
153
 
154
sub emailAffiliationAdded {
155
  my $newpersonid = shift // return;
156
  my $leagueid = shift // return;
157
 
158
  my $newb = PEEPS::getUser ($newpersonid);
159
  my $dbh = PEEPS::getRCDBH ();
160
  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") };
161
  my ($role) = $dbh->selectrow_array ("select role from role where person_id = ? and member_org_id = ?", undef, $newpersonid, $leagueid);
162
 
163
  my $leagueName = PEEPS::getLeagueName ($leagueid);
164
 
165
  my $subject = $leagueName." - Affiliation Confirmed";
166
  my $body = $h->p (
167
                      $h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),
168
                      "Greetings,",
169
                      "League Affiliation for $leagueName has been confirmed:".$h->br,
170
                      "Derby Name: ".$newb->{derby_name}.$h->br.
171
                      "Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br.
172
                      "PEEPS ID: ".$newb->{id}.$h->br,
173
                      "Role: ".$role.$h->br.$h->br,
174
                      "No further action is necessary.".$h->br.$h->br,
175
                      "-WFTDA PEEPS",
176
                      "NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!"
177
                   );
178
 
179
  PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });
180
}
181
 
182
sub emailNewLeagueRole {
183
  my $newpersonid = shift // return;
184
  my $leagueid = shift // return;
185
  my $newrole = shift // return;
186
 
187
  my $newb = PEEPS::getUser ($newpersonid);
188
  my $dbh = PEEPS::getRCDBH ();
189
  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") };
190
 
191
  my $leagueName = PEEPS::getLeagueName ($leagueid);
192
 
193
  my $subject = $leagueName." - New ".$newrole;
194
  my $body = $h->p (
195
                      $h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),
196
                      "Greetings,",
197
                      "A new role has been added for $leagueName:".$h->br,
198
                      "Derby Name: ".$newb->{derby_name}.$h->br.
199
                      "Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br.
200
                      "PEEPS ID: ".$newb->{id}.$h->br,
201
                      "Role: ".$newrole.$h->br.$h->br,
202
                      "No further action is necessary.".$h->br.$h->br,
203
                      "-WFTDA PEEPS",
204
                      "NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!"
205
                   );
206
 
207
  PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });
208
}
209
 
210
sub emailAffiliationRemoved {
211
  my $personid = shift // return;
212
  my $leagueid = shift // return;
213
 
214
  my $newb = PEEPS::getUser ($personid);
215
  my $dbh = PEEPS::getRCDBH ();
216
  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") };
217
 
218
  my $leagueName = PEEPS::getLeagueName ($leagueid);
219
 
220
  my $subject = $leagueName." - Affiliation Removed";
221
  my $body = $h->p (
222
                      $h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftdapeeps-powerby-wftdainsurance-2.svg", style => "width: 400px; height: 75px;"}),
223
                      "Greetings,",
224
                      "League Affiliation has been removed from $leagueName:".$h->br,
225
                      "Derby Name: ".$newb->{derby_name}.$h->br.
226
                      "Civil Name: ".join " ", $newb->{name_first}, $newb->{name_last}.$h->br.
227
                      "PEEPS ID: ".$newb->{id}.$h->br.$h->br,
228
                      "No further action is necessary.".$h->br.$h->br,
229
                      "-WFTDA PEEPS",
230
                      "NOTE: THIS IS A DEVELOPMENT SYSTEM, PLEASE IGNORE!"
231
                   );
232
 
233
  PEEPSMailer::EmailMultipleUsers ({ to => \@leagueAdmins, cc => [$newb->{email}], subject=>$subject, body=>$body });
234
}
235
 
236
 
2 - 237
1;