| … | |
… | |
| 88 | my %recitation = (); |
88 | my %recitation = (); |
| 89 | my %status = (); |
89 | my %status = (); |
| 90 | my %email = (); |
90 | my %email = (); |
| 91 | my %login = (); |
91 | my %login = (); |
| 92 | |
92 | |
|
|
93 | #timeout parameters |
|
|
94 | my $timeout_sec = 15; |
|
|
95 | my $max_timeout_attempts = 3; |
|
|
96 | my $timeout_attempts = 0; |
|
|
97 | |
|
|
98 | |
|
|
99 | #keep track of email sent and not sent |
|
|
100 | my $emails_sent = 0; |
|
|
101 | my $emails_not_sent = 0; |
|
|
102 | |
| 93 | #array for bad email addresses |
103 | #array for bad email addresses |
| 94 | my @bad_email_addresses = (); |
104 | my @bad_email_addresses = (); |
|
|
105 | my @unknown_problem = (); |
|
|
106 | my @timeout_problem = (); |
|
|
107 | my @exceeded_max_timeout = (); |
|
|
108 | my @no_record = (); |
| 95 | |
109 | |
| 96 | # get format information |
110 | # get format information |
| 97 | |
111 | |
| 98 | my $format = (defined($cgi->param('format'))) ? $cgi->param('format') : 'alph'; |
112 | my $format = (defined($cgi->param('format'))) ? $cgi->param('format') : 'alph'; |
| 99 | |
113 | |
| … | |
… | |
| 294 | } |
308 | } |
| 295 | |
309 | |
| 296 | |
310 | |
| 297 | my %mergeAArray = (); |
311 | my %mergeAArray = (); |
| 298 | unless ($mergeFile eq '') {%mergeAArray = &delim2aa($mergeFile);} |
312 | unless ($mergeFile eq '') {%mergeAArray = &delim2aa($mergeFile);} |
| 299 | |
313 | |
|
|
314 | &user_error('You didn\'t enter any message.') if ($cgi->param('body') eq ''); |
|
|
315 | |
| 300 | foreach my $studentID (@studentID) { |
316 | foreach my $studentID (@studentID) { |
| 301 | unless ((defined $mergeAArray{$studentID}) or ($mergeFile eq '')) { |
|
|
| 302 | next if $cgi->param('no_record'); |
|
|
| 303 | } |
|
|
| 304 | next if ($login{$studentID} =~ /^$practiceUser/); ## skip practice users |
|
|
| 305 | |
|
|
| 306 | @COL =(); |
317 | @COL =(); |
| 307 | $SID = $studentID; |
318 | $SID = $studentID; |
| 308 | $LN = defined $ln{$studentID} ? $ln{$studentID} :''; |
319 | $LN = defined $ln{$studentID} ? $ln{$studentID} :''; |
| 309 | $FN = defined $fn{$studentID} ? $fn{$studentID} :''; |
320 | $FN = defined $fn{$studentID} ? $fn{$studentID} :''; |
| 310 | $SECTION = defined $section{$studentID} ? $section{$studentID} :''; |
321 | $SECTION = defined $section{$studentID} ? $section{$studentID} :''; |
| 311 | $RECITATION = defined $recitation{$studentID} ? $recitation{$studentID} :''; |
322 | $RECITATION = defined $recitation{$studentID} ? $recitation{$studentID} :''; |
| 312 | $EMAIL = defined $email{$studentID} ? $email{$studentID} :''; |
323 | $EMAIL = defined $email{$studentID} ? $email{$studentID} :''; |
| 313 | $STATUS =defined $status{$studentID} ? $status{$studentID} :''; |
324 | $STATUS =defined $status{$studentID} ? $status{$studentID} :''; |
| 314 | $LOGIN = $login{$studentID}; |
325 | $LOGIN = $login{$studentID}; |
|
|
326 | |
|
|
327 | next if ($LOGIN =~ /^$practiceUser/); ## skip practice users |
|
|
328 | |
|
|
329 | if ($timeout_attempts >= $max_timeout_attempts) { ## have attemped to connect to smtp server |
|
|
330 | ## the max allowed times. Now just collect |
|
|
331 | ## data on emails not sent and exit |
|
|
332 | ++$emails_not_sent; |
|
|
333 | &log_error(\@exceeded_max_timeout,$FN,$LN,$EMAIL); |
|
|
334 | next; |
|
|
335 | } |
|
|
336 | |
|
|
337 | unless ((defined $mergeAArray{$studentID}) or ($mergeFile eq '')) { |
|
|
338 | if ($cgi->param('no_record')) { |
|
|
339 | ++$emails_not_sent; |
|
|
340 | &log_error(\@no_record,$FN,$LN,$EMAIL); |
|
|
341 | next; |
|
|
342 | } |
|
|
343 | } |
| 315 | |
344 | |
| 316 | my ($dbString, @dbArray); |
345 | my ($dbString, @dbArray); |
| 317 | if (defined $mergeAArray{$SID}) { |
346 | if (defined $mergeAArray{$SID}) { |
| 318 | $dbString = $mergeAArray{$SID}; ## get sid record from merge file |
347 | $dbString = $mergeAArray{$SID}; ## get sid record from merge file |
| 319 | @dbArray = &getRecord($dbString); |
348 | @dbArray = &getRecord($dbString); |
| 320 | unshift(@dbArray,$SID); |
349 | unshift(@dbArray,$SID); |
| 321 | unshift(@dbArray,""); ## note COL[1] is the first column |
350 | unshift(@dbArray,""); ## note COL[1] is the first column |
| 322 | @COL= @dbArray; ## put merge fields in COL array |
351 | @COL= @dbArray; ## put merge fields in COL array |
| 323 | $endCol = @COL; ## \endCol-1 gives last field, etc |
352 | $endCol = @COL; ## \endCol-1 gives last field, etc |
| 324 | } |
353 | } |
| 325 | |
354 | my $smtp; |
| 326 | &user_error('You didn\'t enter any message.') if ($cgi->param('body') eq ''); |
355 | if ($smtp = Net::SMTP->new($Global::smtpServer, Timeout => $timeout_sec)) {} else { |
| 327 | |
356 | # &internal_error("Couldn't contact SMTP server."); |
| 328 | my $smtp = Net::SMTP->new($Global::smtpServer, Timeout=>10) || &internal_error("Couldn't contact SMTP server."); |
357 | ++$emails_not_sent; |
|
|
358 | &log_error(\@timeout_problem,$FN,$LN,$EMAIL); |
|
|
359 | ++$timeout_attempts; |
|
|
360 | next; |
|
|
361 | } |
|
|
362 | |
| 329 | $smtp->mail($smtpSender); |
363 | $smtp->mail($smtpSender); |
| 330 | |
364 | |
| 331 | if ( $smtp->recipient($EMAIL)) { # this one's okay, keep going |
365 | if ( $smtp->recipient($EMAIL)) { # this one's okay, keep going |
| 332 | $smtp->data("To: $EMAIL\n" . output() ) || |
366 | if ( $smtp->data("To: $EMAIL\n" . output() ) ) { |
|
|
367 | ++$emails_sent; |
|
|
368 | } else { |
|
|
369 | ++$emails_not_sent; |
|
|
370 | &log_error(\@unknown_problem,$FN,$LN,$EMAIL); |
|
|
371 | next; |
|
|
372 | } |
| 333 | &internal_error("Unknown problem sending message data to SMTP server."); |
373 | # &internal_error("Unknown problem sending message data to SMTP server."); |
| 334 | } else { # we have a problem a problem with this address |
374 | } else { # we have a problem with this address |
| 335 | $smtp->reset; |
375 | $smtp->reset; |
| 336 | #&internal_error("SMTP server doesn't like this address: <$EMAIL>."); |
376 | #&internal_error("SMTP server doesn't like this address: <$EMAIL>."); |
| 337 | &log_error($FN,$LN,$EMAIL); |
377 | ++$emails_not_sent; |
|
|
378 | &log_error(\@bad_email_addresses,$FN,$LN,$EMAIL); |
| 338 | } |
379 | } |
| 339 | $smtp->quit; |
380 | $smtp->quit; |
| 340 | } |
381 | } |
| 341 | &success; |
382 | &success; |
| 342 | } |
383 | } |
| … | |
… | |
| 701 | return $msg; |
742 | return $msg; |
| 702 | } |
743 | } |
| 703 | |
744 | |
| 704 | sub success { |
745 | sub success { |
| 705 | print $cgi->header, |
746 | print $cgi->header, |
| 706 | $cgi->start_html( '-title'=>'Email Sent'), |
747 | $cgi->start_html( '-title'=>'Email Sent'),"\n", |
| 707 | $cgi->h1('Your message has been sent.'); |
748 | $cgi->h1("Your message has been sent to $emails_sent users."),"\n", |
|
|
749 | $cgi->br; |
|
|
750 | |
|
|
751 | if ($emails_not_sent > 0) { |
|
|
752 | print $cgi->h1("However, emails were not sent to the following $emails_not_sent users:"),"\n", |
|
|
753 | $cgi->br, |
|
|
754 | $cgi->h2('The format below is "FirstName LastName <EmailAddress>"'); |
|
|
755 | |
|
|
756 | if (scalar(@no_record) > 0) { |
|
|
757 | print "\n", $cgi->br, |
|
|
758 | $cgi->h3('The following have no record in the merge file'),"\n"; |
|
|
759 | foreach my $line (@no_record) {print "\n$line <BR>";} |
|
|
760 | } |
|
|
761 | if (scalar(@unknown_problem) > 0) { |
|
|
762 | print "\n", $cgi->br, |
|
|
763 | $cgi->h3('The following have an unknown problem'); |
|
|
764 | foreach my $line (@unknown_problem) {print "\n$line <BR>";} |
|
|
765 | } |
| 708 | if (scalar(@bad_email_addresses) > 0) { |
766 | if (scalar(@bad_email_addresses) > 0) { |
|
|
767 | print "\n", $cgi->br, |
| 709 | print $cgi->h1('However, the following users had a bad email address.'), |
768 | $cgi->h3('The following have a bad email address'); |
| 710 | $cgi->h1('The message was not sent to them.'); |
|
|
| 711 | foreach my $line (@bad_email_addresses) {print "$line <BR>";} |
769 | foreach my $line (@bad_email_addresses) {print "\n$line <BR>";} |
|
|
770 | } |
|
|
771 | if (scalar(@timeout_problem) > 0) { |
|
|
772 | print "\n", $cgi->br, |
|
|
773 | $cgi->h3('Connecting to the SMTP server timed out for the following'); |
|
|
774 | foreach my $line (@timeout_problem) {print "\n$line <BR>";} |
| 712 | } |
775 | } |
|
|
776 | if (scalar(@exceeded_max_timeout) > 0) { |
|
|
777 | print "\n", $cgi->br, |
|
|
778 | $cgi->h3("Connecting to the SMTP server failed $max_timeout_attempts times. No attempt was made to send emails to the following"); |
|
|
779 | foreach my $line (@exceeded_max_timeout) {print "\n$line <BR>";} |
|
|
780 | } |
|
|
781 | } |
| 713 | print $cgi->end_html; |
782 | print "\n", $cgi->end_html; |
| 714 | exit(0); |
783 | exit(0); |
| 715 | } |
784 | } |
| 716 | |
785 | |
| 717 | sub log_error { |
786 | sub log_error { |
| 718 | my ($FN,$LN,$EMAIL) = @_; |
787 | my ($ra_addresses,$FN,$LN,$EMAIL) = @_; |
| 719 | my $line = "$FN $LN <${EMAIL}>"; |
788 | my $line = "$FN $LN <${EMAIL}>"; |
| 720 | push @bad_email_addresses, $line; |
789 | push @$ra_addresses, $line; |
| 721 | } |
790 | } |