📄 email.pm
字号:
my $addr = $ENV{'REMOTE_ADDR'}; $host = "(unknown)" if !defined($host); $addr = "(unknown)" if !defined($addr); $changes = "The following changes were made by an unknown user from " . "host $host and address $addr\n" . $changes; } # See if anybody needs an mail, if so then send it out. if (@to_list) { my $from = $user_that_made_the_change; my $bcc = ""; if ($user_that_made_the_change eq "") { $from = $topic->{author}; } else { $bcc = $user_that_made_the_change; } # Remove the $user_that_made_the_change, they are bcc'ed, don't want to # send the email out twice. my @final_to_list; foreach my $user (@to_list) { push (@final_to_list,$user) if $user ne $user_that_made_the_change; } if (@to_list > 0 && @final_to_list == 0) { push(@final_to_list, $user_that_made_the_change); $bcc = ""; } my $to = join ', ', sort @final_to_list; my $cc = ""; # Send off the email to the revelant parties. return $self->_send_topic_email($topic, 0, $change_event_name , 1, $from, $to, $cc, $bcc, $changes); }}sub comment_create($$$) { my ($self, $topic, $comment) = @_; # Check if this action doesn't need to be logged. if (defined $topic->{email_event} && $topic->{email_event} == 0) { return ''; } my $query = new CGI; my $url_builder = Codestriker::Http::UrlBuilder->new($query); # Send an email to the document author and all contributors with the # relevant information. The person who wrote the comment is indicated # in the "From" field, and is BCCed the email so they retain a copy. my $edit_url = $url_builder->edit_url(filenumber => $comment->{filenumber}, line => $comment->{fileline}, new => $comment->{filenew}, topicid => $topic->{topicid}, projectid => $topic->{project_id}); # Retrieve the comment details for this topic. my @comments = $topic->read_comments(); my %contributors = (); $contributors{$comment->{author}} = 1; my @cc_recipients; for (my $i = 0; $i <= $#comments; $i++) { if ( $comments[$i]{fileline} == $comment->{fileline} && $comments[$i]{filenumber} == $comment->{filenumber} && $comments[$i]{filenew} == $comment->{filenew} && $comments[$i]{author} ne $topic->{author} && ! exists $contributors{$comments[$i]{author}}) { $contributors{$comments[$i]{author}} = 1; push(@cc_recipients, $comments[$i]{author}); } } push @cc_recipients, (split ',', $comment->{cc}); my $from = $comment->{author}; my $to = $topic->{author}; # don't blind copy the comment authors unless configured to. my $bcc = ""; if ( $Codestriker::email_send_options->{comments_sent_to_commenter} ) { $bcc = $comment->{author}; } my $subject = "[REVIEW] Topic \"$topic->{title}\" " . "comment added by $comment->{author}"; my $body = "$comment->{author} added a comment to " . "Topic \"$topic->{title}\".\n\n" . "URL: $edit_url\n\n"; if (defined $comment->{filename} && $comment->{filename} ne '') { $body .= "File: " . $comment->{filename}; } if ($comment->{fileline} != -1) { $body .= " line $comment->{fileline}.\n\n"; # Retrieve the diff hunk for this file and line number. my $delta = Codestriker::Model::Delta->get_delta($comment->{topicid}, $comment->{filenumber}, $comment->{fileline}, $comment->{filenew}); if (defined $delta) { # Only show the context for a comment made against a specific line # in the original review text. $body .= "Context:\n$EMAIL_HR\n\n"; my $email_context = $Codestriker::EMAIL_CONTEXT; my @text = (); my $offset = $delta->retrieve_context($comment->{fileline}, $comment->{filenew}, $email_context, \@text); for (my $i = 0; $i <= $#text; $i++) { if ($i == $offset) { $text[$i] = "* " . $text[$i]; } else { $text[$i] = " " . $text[$i]; } } $body .= join "\n", @text; $body .= "\n\n"; $body .= "$EMAIL_HR"; } } $body .= "\n\n"; # Now display the comments that have already been submitted. for (my $i = $#comments; $i >= 0; $i--) { if ($comments[$i]{fileline} == $comment->{fileline} && $comments[$i]{filenumber} == $comment->{filenumber} && $comments[$i]{filenew} == $comment->{filenew}) { my $data = $comments[$i]{data}; $body .= "$comments[$i]{author} $comments[$i]{date}\n\n$data\n\n"; $body .= "$EMAIL_HR\n\n"; } } # Send the email notification out, if it is allowed in the config file. if ( $Codestriker::email_send_options->{comments_sent_to_topic_author} || $comment->{cc} ne "") { return $self->doit(0, $comment->{topicid}, $from, $to, join(', ',@cc_recipients), $bcc, $subject, $body); } return '';}# This is a private helper function that is used to send topic emails. Topic# emails include topic creation, state changes, and deletes.sub _send_topic_email { my ($self, $topic, $new, $event_name, $include_url, $from, $to, $cc, $bcc, $notes) = @_; my $query = new CGI; my $url_builder = Codestriker::Http::UrlBuilder->new($query); my $topic_url = $url_builder->view_url(topicid => $topic->{topicid}, projectid => $topic->{project_id}); my $subject = "[REVIEW] Topic $event_name \"" . $topic->{title} . "\""; # Set the bug ID line to report the actual URL links if possible. my $bug_id_line = ""; if (defined $topic->{bug_ids} && $topic->{bug_ids} ne "") { $bug_id_line = "Bug IDs: "; if (defined $Codestriker::bugtracker) { my @bug_id_array = split /[\s,]+/, $topic->{bug_ids}; for (my $i = 0; $i <= $#bug_id_array; $i++) { $bug_id_line .= $Codestriker::bugtracker . $bug_id_array[$i]; if ($i < $#bug_id_array) { $bug_id_line .= "\n "; } } $bug_id_line .= "\n"; } else { $bug_id_line .= $topic->{bug_ids} . "\n"; } } my $body = "Topic \"$topic->{title}\"\n" . "Author: $topic->{author}\n" . $bug_id_line . "Reviewers: $topic->{reviewers}\n" . (($include_url) ? "URL: $topic_url\n\n" : "") . "$EMAIL_HR\n" . $notes; # Send the email notification out. return $self->doit($new, $topic->{topicid}, $from, $to, $cc, $bcc, $subject, $body);}# Send an email with the specified data. Return a non-empty message if the# mail can't be successfully delivered, empty string otherwise.sub doit($$$$$$$$$) { my ($type, $new, $topicid, $from, $to, $cc, $bcc, $subject, $body) = @_; return '' if ($DEVNULL_EMAIL); my $smtp = Net::SMTP->new($Codestriker::mailhost); defined $smtp || return "Unable to connect to mail server: $!"; # Perform SMTP authentication if required. if (defined $Codestriker::mailuser && $Codestriker::mailuser ne "" && defined $Codestriker::mailpasswd) { eval 'use Authen::SASL'; die "Unable to load Authen::SASL module: $@\n" if $@; $smtp->auth($Codestriker::mailuser, $Codestriker::mailpasswd); } $smtp->mail($from); $smtp->ok() || return "Couldn't set sender to \"$from\" $!, " . $smtp->message(); # $to has to be defined. my $recipients = $to; $recipients .= ", $cc" if $cc ne ""; $recipients .= ", $bcc" if $bcc ne ""; my @receiver = split /, /, $recipients; for (my $i = 0; $i <= $#receiver; $i++) { if ($receiver[$i] ne "") { $smtp->recipient($receiver[$i]); $smtp->ok() || return "Couldn't send email to \"$receiver[$i]\" $!, " . $smtp->message(); } else { # Can't track down why, but sometimes an empty email address # pops into here and kills the entire thing. This makes the # problem go away. } } $smtp->data(); $smtp->datasend("From: $from\n"); $smtp->datasend("To: $to\n"); $smtp->datasend("Cc: $cc\n") if $cc ne ""; # If the message is new, create the appropriate message id, otherwise # construct a message which refers to the original message. This will # allow for threading, for those email clients which support it. my $message_id = "<Codestriker-" . hostname() . "-${topicid}>"; if ($new) { $smtp->datasend("Message-Id: $message_id\n"); } else { $smtp->datasend("References: $message_id\n"); $smtp->datasend("In-Reply-To: $message_id\n"); } # Make sure the subject is appropriately encoded to handle UTF-8 # characters. $subject = encode_qp(encode("UTF-8", $subject), ""); # RFC 2047 fixup that is a documented deviation from quoted-printable $subject =~ s/\?/=3F/g; $subject =~ s/_/=5F/g; $subject =~ s/ /_/g; $smtp->datasend("Subject: =?UTF-8?Q?${subject}?=\n"); # Set the content type to be text/plain with UTF8 encoding, to handle # unicode characters. $smtp->datasend("Content-Type: text/plain; charset=\"utf-8\"\n"); $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n"); $smtp->datasend("MIME-Version: 1.0\n"); # Insert a blank line for the body. $smtp->datasend("\n"); # Encode the mail body using quoted-printable to handle unicode # characters. $smtp->datasend(encode_qp(encode("UTF-8", $body))); $smtp->dataend(); $smtp->ok() || return "Couldn't send email $!, " . $smtp->message(); $smtp->quit(); $smtp->ok() || return "Couldn't send email $!, " . $smtp->message(); return '';}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -