--- loncom/interface/lonmsg.pm 2014/12/11 01:53:22 1.237 +++ loncom/interface/lonmsg.pm 2015/02/12 21:22:07 1.238 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines for messaging # -# $Id: lonmsg.pm,v 1.237 2014/12/11 01:53:22 raeburn Exp $ +# $Id: lonmsg.pm,v 1.238 2015/02/12 21:22:07 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -120,7 +120,7 @@ Critical message to a user New routine that respects "forward" and calls old routine -=item * B: +=item * B: Sends a critical message $message to the $user at $domain. If $sendback is true, a receipt will be sent to the current user when $user receives the message. @@ -148,7 +148,7 @@ New routine that respects "forward" and =item * B: + $error,$nosentstore,$recipid,$permresults)>: Sends a message to the $user at $domain, with subject $subject and message $message. Additionally it will check if the user has a Forwarding address @@ -424,6 +424,7 @@ sub sendemail { my ($to,$subject,$body,$to_uname,$to_udom,$user_lh)=@_; my $senderaddress=''; my $replytoaddress=''; + my $msgsent; if ($env{'form.can_reply'} eq 'N') { my $lonhost = $Apache::lonnet::perlvar{'lonHostID'}; my $hostname = &Apache::lonnet::hostname($lonhost); @@ -469,7 +470,9 @@ sub sendemail { if (my $fh = $msg->open()) { print $fh $body; $fh->close; + $msgsent = 1; } + return $msgsent; } # ==================================================== Send notification emails @@ -481,6 +484,7 @@ sub sendnotification { $sender=$env{'user.name'}.':'.$env{'user.domain'}; } my $critical=($crit?' critical':''); + my $numsent = 0; $text=~s/\<\;/\/gs; @@ -558,7 +562,9 @@ to access the full message.',$url); } $body = $bodybegin.$bodysubj.$sendtext.$bodyend; } - &sendemail($addr,$subject,$body,$touname,$toudom,$user_lh); + if (&sendemail($addr,$subject,$body,$touname,$toudom,$user_lh)) { + $numsent ++; + } } } else { if ($blocked) { @@ -567,8 +573,11 @@ to access the full message.',$url); my $htmlfree = &make_htmlfree($text); $body = $bodybegin.$bodysubj.$htmlfree.$bodyend; } - &sendemail($to,$subject,$body,$touname,$toudom,$user_lh); + if (&sendemail($to,$subject,$body,$touname,$toudom,$user_lh)) { + $numsent ++; + } } + return $numsent; } sub make_htmlfree { @@ -696,7 +705,7 @@ sub store_instructor_comment { sub user_crit_msg_raw { my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage, - $nosentstore,$recipid,$attachmenturl)=@_; + $nosentstore,$recipid,$attachmenturl,$permresults)=@_; # Check if allowed missing my ($status,$packed_message); my $msgid='undefined'; @@ -727,22 +736,40 @@ sub user_crit_msg_raw { # Notifications my %userenv = &Apache::loncommon::getemails($user,$domain); - if ($userenv{'critnotification'}) { - &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1, - $text,$msgid); - } - if ($toperm && $userenv{'permanentemail'}) { - &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1, - $text,$msgid); + my $critnotify = $userenv{'critnotification'}; + my $permemail = $userenv{'permanentemail'}; + my $numcrit = 0; + my $numperm = 0; + my $permlogmsgstatus; + if ($critnotify) { + $numcrit = &sendnotification($critnotify,$user,$domain,$subject,1,$text,$msgid); + } + if ($toperm && $permemail) { + if ($critnotify && $numcrit) { + if (grep(/^\Q$permemail\E/,split(/,/,$critnotify))) { + $numperm = 1; + } + } + unless ($numperm) { + $numperm = &sendnotification($permemail,$user,$domain,$subject,1,$text,$msgid); + } + } + if ($toperm) { + $permlogmsgstatus = '. Perm. email log status '. + &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},$env{'user.home'}, + "Perm. e-mail count $numperm for $user at $domain"); + if (ref($permresults) eq 'HASH') { + $permresults->{"$user:$domain"} = $numperm; + } } # Log this &Apache::lonnet::logthis( - 'Sending critical email '.$msgid. + 'Sending critical '.$msgid. ', log status: '. &Apache::lonnet::log($env{'user.domain'},$env{'user.name'}, $env{'user.home'}, - 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: ' - .$status)); + 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status ' + .$status).$permlogmsgstatus); return $status; } @@ -750,7 +777,7 @@ sub user_crit_msg_raw { sub user_crit_msg { my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage, - $nosentstore,$recipid,$attachmenturl)=@_; + $nosentstore,$recipid,$attachmenturl,$permresults)=@_; my @status; my %userenv = &Apache::lonnet::get('environment',['msgforward'], $domain,$user); @@ -761,13 +788,13 @@ sub user_crit_msg { push(@status, &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message, $sendback,$toperm,$sentmessage,$nosentstore, - $recipid,$attachmenturl)); + $recipid,$attachmenturl,$permresults)); } } else { push(@status, &user_crit_msg_raw($user,$domain,$subject,$message,$sendback, $toperm,$sentmessage,$nosentstore,$recipid, - $attachmenturl)); + $attachmenturl,$permresults)); } if (wantarray) { return @status; @@ -816,7 +843,7 @@ sub user_crit_received { sub user_normal_msg_raw { my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl, $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle, - $error,$nosentstore,$recipid)=@_; + $error,$nosentstore,$recipid,$permresults)=@_; # Check if allowed missing my ($status,$packed_message); my $msgid='undefined'; @@ -853,19 +880,37 @@ sub user_normal_msg_raw { } # Notifications my %userenv = &Apache::loncommon::getemails($user,$domain); - if ($userenv{'notification'}) { - &sendnotification($userenv{'notification'},$user,$domain,$subject,0, - $text,$msgid); + my $notify = $userenv{'notification'}; + my $permemail = $userenv{'permanentemail'}; + my $numnotify = 0; + my $numperm = 0; + my $permlogmsgstatus; + if ($notify) { + $numnotify = &sendnotification($notify,$user,$domain,$subject,0,$text,$msgid); + } + if ($toperm && $permemail) { + if ($notify && $numnotify) { + if (grep(/^\Q$permemail\E/,split(/,/,$notify))) { + $numperm = 1; + } + } + unless ($numperm) { + $numperm = &sendnotification($permemail,$user,$domain,$subject,0, + $text,$msgid); + } } - if ($toperm && $userenv{'permanentemail'}) { - if ((!$userenv{'notification'}) || ($userenv{'notification'} ne $userenv{'permanentemail'})) { - &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0, - $text,$msgid); + if ($toperm) { + $permlogmsgstatus = '. Perm. email log status '. + &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},$env{'user.home'}, + "Perm. e-mail count $numperm for $user at $domain"); + if (ref($permresults) eq 'HASH') { + $permresults->{"$user:$domain"} = $numperm; } } &Apache::lonnet::log($env{'user.domain'},$env{'user.name'}, $env{'user.home'}, - 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status); + 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status '.$status. + $permlogmsgstatus); } else { $status='no_host'; } @@ -874,7 +919,8 @@ sub user_normal_msg_raw { sub user_normal_msg { my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl, - $toperm,$sentmessage,$symb,$restitle,$error,$nosentstore,$recipid)=@_; + $toperm,$sentmessage,$symb,$restitle,$error,$nosentstore,$recipid, + $permresults)=@_; my @status; my %userenv = &Apache::lonnet::get('environment',['msgforward'], $domain,$user); @@ -886,13 +932,13 @@ sub user_normal_msg { &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message, $citation,$baseurl,$attachmenturl,$toperm, undef,undef,$sentmessage,undef,$symb, - $restitle,$error,$nosentstore,$recipid)); + $restitle,$error,$nosentstore,$recipid,$permresults)); } } else { push(@status,&user_normal_msg_raw($user,$domain,$subject,$message, $citation,$baseurl,$attachmenturl,$toperm, undef,undef,$sentmessage,undef,$symb, - $restitle,$error,$nosentstore,$recipid)); + $restitle,$error,$nosentstore,$recipid,$permresults)); } if (wantarray) { return @status;