| 1 | ################################################################################ |
1 | ################################################################################ |
| 2 | # WeBWorK Online Homework Delivery System |
2 | # WeBWorK Online Homework Delivery System |
| 3 | # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ |
3 | # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ |
| 4 | # $CVSHeader$ |
4 | # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SendMail.pm,v 1.40 2005/07/01 23:52:12 gage Exp $ |
| 5 | # |
5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify it under |
6 | # This program is free software; you can redistribute it and/or modify it under |
| 7 | # the terms of either: (a) the GNU General Public License as published by the |
7 | # the terms of either: (a) the GNU General Public License as published by the |
| 8 | # Free Software Foundation; either version 2, or (at your option) any later |
8 | # Free Software Foundation; either version 2, or (at your option) any later |
| 9 | # version, or (b) the "Artistic License" which comes with this package. |
9 | # version, or (b) the "Artistic License" which comes with this package. |
| … | |
… | |
| 379 | unless (ref($rh_merge_data) ) { |
379 | unless (ref($rh_merge_data) ) { |
| 380 | $self->addbadmessage(CGI::p("No merge data file")); |
380 | $self->addbadmessage(CGI::p("No merge data file")); |
| 381 | $self->addbadmessage(CGI::p("Can't read merge file $merge_file. No message sent")); |
381 | $self->addbadmessage(CGI::p("Can't read merge file $merge_file. No message sent")); |
| 382 | return; |
382 | return; |
| 383 | } ; |
383 | } ; |
|
|
384 | if (@recipients) { |
|
|
385 | $self->{rh_merge_data} = $rh_merge_data; |
|
|
386 | $self->{smtpServer} = $ce->{mail}->{smtpServer}; |
|
|
387 | my $post_connection_action = sub { |
|
|
388 | my $r = shift; |
|
|
389 | my $result_message = $self->mail_message_to_recipients(); |
|
|
390 | $self->email_notification($result_message); |
|
|
391 | }; |
|
|
392 | $r->post_connection($post_connection_action) ; |
| 384 | |
393 | } |
| 385 | |
|
|
| 386 | foreach my $recipient (@recipients) { |
394 | # foreach my $recipient (@recipients) { |
| 387 | #warn "FIXME sending email to $recipient"; |
395 | # #warn "FIXME sending email to $recipient"; |
| 388 | my $ur = $self->{db}->getUser($recipient); #checked |
396 | # my $ur = $self->{db}->getUser($recipient); #checked |
| 389 | die "record for user $recipient not found" unless $ur; |
397 | # die "record for user $recipient not found" unless $ur; |
| 390 | unless ($ur->email_address) { |
398 | # unless ($ur->email_address) { |
| 391 | $self->addbadmessage(CGI::p("user $recipient does not have an email address -- skipping")); |
399 | # $self->addbadmessage(CGI::p("user $recipient does not have an email address -- skipping")); |
| 392 | next; |
400 | # next; |
| 393 | } |
401 | # } |
| 394 | my ($msg, $preview_header); |
402 | # my ($msg, $preview_header); |
| 395 | eval{ ($msg,$preview_header) = $self->process_message($ur,$rh_merge_data); }; |
403 | # eval{ ($msg,$preview_header) = $self->process_message($ur,$rh_merge_data); }; |
| 396 | $self->addbadmessage(CGI::p("There were errors in processing user $ur, merge file $merge_file. $@")) if $@; |
404 | # $self->addbadmessage(CGI::p("There were errors in processing user $ur, merge file $merge_file. $@")) if $@; |
| 397 | my $mailer = Mail::Sender->new({ |
405 | # my $mailer = Mail::Sender->new({ |
| 398 | from => $from, |
406 | # from => $from, |
| 399 | to => $ur->email_address, |
407 | # to => $ur->email_address, |
| 400 | smtp => $ce->{mail}->{smtpServer}, |
408 | # smtp => $ce->{mail}->{smtpServer}, |
| 401 | subject => $subject, |
409 | # subject => $subject, |
| 402 | headers => "X-Remote-Host: ".$r->get_remote_host(), |
410 | # headers => "X-Remote-Host: ".$r->get_remote_host(), |
| 403 | }); |
411 | # }); |
| 404 | unless (ref $mailer) { |
412 | # unless (ref $mailer) { |
| 405 | $self->addbadmessage(CGI::p("Failed to create a mailer for user $recipient: $Mail::Sender::Error")); |
413 | # $self->addbadmessage(CGI::p("Failed to create a mailer for user $recipient: $Mail::Sender::Error")); |
| 406 | next; |
414 | # next; |
| 407 | } |
415 | # } |
| 408 | unless (ref $mailer->Open()) { |
416 | # unless (ref $mailer->Open()) { |
| 409 | $self->addbadmessage(CGI::p("Failed to open the mailer for user $recipient: $Mail::Sender::Error")); |
417 | # $self->addbadmessage(CGI::p("Failed to open the mailer for user $recipient: $Mail::Sender::Error")); |
| 410 | next; |
418 | # next; |
| 411 | } |
419 | # } |
| 412 | my $MAIL = $mailer->GetHandle() or $self->addbadmessage(CGI::p("Couldn't get handle")); |
420 | # my $MAIL = $mailer->GetHandle() or $self->addbadmessage(CGI::p("Couldn't get handle")); |
| 413 | print $MAIL $msg || $self->addbadmessage(CGI::p("Couldn't print to $MAIL")); |
421 | # print $MAIL $msg || $self->addbadmessage(CGI::p("Couldn't print to $MAIL")); |
| 414 | close $MAIL || $self->addbadmessage(CGI::p("Couldn't close $MAIL")); |
422 | # close $MAIL || $self->addbadmessage(CGI::p("Couldn't close $MAIL")); |
| 415 | #warn "FIXME mailed to ", $ur->email_address, "from $from subject $subject"; |
423 | # #warn "FIXME mailed to ", $ur->email_address, "from $from subject $subject"; |
| 416 | |
424 | # |
| 417 | } |
425 | # } |
| 418 | |
426 | |
| 419 | } else { |
427 | } else { |
| 420 | $self->addbadmessage(CGI::p("Didn't recognize button $action")); |
428 | $self->addbadmessage(CGI::p("Didn't recognize button $action")); |
| 421 | } |
429 | } |
| 422 | |
430 | |
| … | |
… | |
| 445 | unless $authz->hasPermissions($user, "send_mail"); |
453 | unless $authz->hasPermissions($user, "send_mail"); |
| 446 | |
454 | |
| 447 | if ($response eq 'preview') { |
455 | if ($response eq 'preview') { |
| 448 | $self->print_preview($setID); |
456 | $self->print_preview($setID); |
| 449 | } elsif (($response eq 'send_email')){ |
457 | } elsif (($response eq 'send_email')){ |
| 450 | $self->addgoodmessage(CGI::p("Email sent to ". scalar(@{$self->{ra_send_to}})." students.")); |
458 | my $message = CGI::i("Email is being sent to ". scalar(@{$self->{ra_send_to}})." recipients. You will be notified" |
| 451 | $self->{message} .= CGI::i("Email sent to ". scalar(@{$self->{ra_send_to}})." students."); |
459 | ." when the task is completed. This may take several minutes if the class is large." |
|
|
460 | ); |
|
|
461 | $self->addgoodmessage($message); |
|
|
462 | $self->{message} .= $message; |
|
|
463 | |
| 452 | $self->print_form($setID); |
464 | $self->print_form($setID); |
| 453 | } else { |
465 | } else { |
| 454 | $self->print_form($setID); |
466 | $self->print_form($setID); |
| 455 | } |
467 | } |
| 456 | |
468 | |