Reporter/Sender/Email.pm

filter: Statementstmt 98.9% 94 / 95 Branchbran 75.0% 18 / 24 Conditioncond 42.8% 9 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 95.0% 19 / 20 Podpod 28.5% 2 / 7 totaltotal 79.8% 143 / 179 SCAR 31.9
CC
23
Cov
86%
new
15.6
_build_email
12.0
send_report
3.1
CRAP 24.3
↑ Summary Reporter/Dumpable/File.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Exception::Reporter::Sender::Email;
4 # ABSTRACT: a report sender that sends detailed dumps via email
5 1 $Exception::Reporter::Sender::Email::VERSION = '0.014';
6 1 ▶use parent 'Exception::Reporter::Sender';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod my $sender = Exception::Reporter::Sender::Email->new({
11 #pod from => 'root',
12 #pod to => 'Beloved SysAdmins <sysadmins@example.com>',
13 #pod });
14 #pod
15 #pod =head1 OVERVIEW
16 #pod
17 #pod This is the only report sender you'll probably ever need.
18 #pod
19 #pod It turns the report into a multipart email message and sends it via email.
20 #pod
21 #pod Each set of summaries is turned into a MIME message part. If a dumpable has
22 #pod become more than one summary, its summaries will be children of a
23 #pod C<multipart/related> part. Otherwise, its summary will become a part of the
24 #pod kind indicated in the summary.
25 #pod
26 #pod The C<ident> of the first summary will be used for the subject of the message.
27 #pod
28 #pod The GUID of the exception report (the thing returned by the reporter's
29 #pod C<report_exception> method) is used as the local part of the email message's
30 #pod Message-ID.
31 #pod
32 #pod Every reported message has a In-Reply-To header formed by combining a
33 #pod slightly-munged version of the C<ident> and the C<reporter>. This means that
34 #pod similar exception report emails will thread together in a thread-capable email
35 #pod reader.
36 #pod
37 #pod =cut
38  
39 1 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Email::Address ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Email::MIME::Creator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Email::MessageID ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Email::Sender::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use String::Truncate;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 sub new {
48 2 ▶ my ($class, $arg) = @_;
Subroutine new: called CC=10 SCAR=27.5
Pod undocumented
49  
50 2 ▶ my $from = $arg->{from} || Carp::confess("missing 'from' argument");
Condition33%1/3
$arg->{'from'} || Carp::confess(q[missing 'from' argument])
l!l&&r!l&&!r
200
MC/DC0%0/2
$arg->{'from'} || Carp::confess(q[missing 'from' argument])
$arg->{'from'} Carp::confess(q[missing 'from' argument])
Truth table33%1/3
A || B
A= $arg->{'from'}B= Carp::confess(q[missing 'from' argument])
ABresult
1X1
011
000
51 2 ▶ my $to = $arg->{to} || Carp::confess("missing 'to' argument"),
Condition33%1/3
$arg->{'to'} || Carp::confess(q[missing 'to' argument])
l!l&&r!l&&!r
200
MC/DC0%0/2
$arg->{'to'} || Carp::confess(q[missing 'to' argument])
$arg->{'to'} Carp::confess(q[missing 'to' argument])
Truth table33%1/3
A || B
A= $arg->{'to'}B= Carp::confess(q[missing 'to' argument])
ABresult
1X1
011
000
52  
53 ($from) = Email::Address->parse($from);
54 2 ▶ ($to) = [ map {; Email::Address->parse($_) } (ref $to ? @$to : $to) ];
Branch: 1 branch
BranchTrueFalseTotal
ref $to ? : 0 2 2
55  
56 # Allow mail from a simple, bare local-part like "root" -- rjbs, 2012-07-03
57 $from = Email::Address->new(undef, $arg->{from})
58 2 ▶ if ! $from and $arg->{from} =~ /\A[-.0-9a-zA-Z]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not $from and $arg->{"from"} =~ /\A[-.0-9a-zA-Z]+\z/ 2 0 2
Condition33%1/3
not $from and $arg->{"from"} =~ /\A[-.0-9a-zA-Z]+\z/
!ll&&!rl&&r
002
MC/DC0%0/2
not $from and $arg->{"from"} =~ /\A[-.0-9a-zA-Z]+\z/
not $from $arg->{"from"} =~ /\A[-.0-9a-zA-Z]+\z/
Truth table33%1/3
not(A) and B
A= not $fromB= $arg->{"from"} =~ /\A[-.0-9a-zA-Z]+\z/
ABresult
0X0
100
111
59  
60 2 ▶ Carp::confess("couldn't interpret $arg->{from} as an email address")
Branch: 1 branch
BranchTrueFalseTotal
unless $from 0 2 2
61 unless $from;
62  
63 2 ▶ my $env_from = $arg->{env_from} || $from->address;
Condition33%1/3
$arg->{'env_from'} || $from->address
l!l&&r!l&&!r
020
MC/DC0%0/2
$arg->{'env_from'} || $from->address
$arg->{'env_from'} $from->address
Truth table33%1/3
A || B
A= $arg->{'env_from'}B= $from->address
ABresult
1X1
011
000
64 2 ▶ my $env_to = $arg->{env_to} || [ map {; $_->address } @$to ];
Condition50%1/2
$arg->{'env_to'} || [map({$_->address;} @$to)]
l!l
02
MC/DC0%0/1
$arg->{'env_to'} || [map({$_->address;} @$to)]
$arg->{'env_to'}
Truth table50%1/2
A
A= $arg->{'env_to'}
Aresult
11
00
65  
66 2 ▶ $env_to = [ $env_to ] unless ref $env_to;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $env_to 0 2 2
67  
68 2 return bless {
69 from => $from,
70 to => $to,
71 env_to => $env_to,
72 env_from => $env_from,
73 }, $class;
74 }
75  
76 sub from_header {
77 3 ▶ my ($self) = @_;
Subroutine from_header: called CC=1 SCAR=0.0
Pod undocumented
78 3 return $self->{from}->as_string;
79 }
80  
81 sub to_header {
82 3 ▶ my ($self) = @_;
Subroutine to_header: called CC=1 SCAR=0.0
Pod undocumented
83 3 return join q{, }, map {; $_->as_string } @{ $self->{to} };
84 }
85  
86 sub env_from {
87 3 ▶ my ($self) = @_;
Subroutine env_from: called CC=1 SCAR=0.0
Pod undocumented
88 3 return $self->{env_from};
89 }
90  
91 sub env_to {
92 3 ▶ my ($self) = @_;
Subroutine env_to: called CC=1 SCAR=0.0
Pod undocumented
93 3 return @{ $self->{env_to} };
94 }
95  
96 #pod =head2 send_report
97 #pod
98 #pod $email_reporter->send_report(\@summaries, \%arg, \%internal_arg);
99 #pod
100 #pod This method builds a multipart email message from the given summaries and
101 #pod sends it.
102 #pod
103 #pod C<%arg> is the same set of arguments given to Exception::Reporter's
104 #pod C<report_exception> method. Arguments that will have an effect include:
105 #pod
106 #pod extra_rcpts - an arrayref of extra envelope recipients
107 #pod reporter - the name of the program reporting the exception
108 #pod handled - if true, the reported exception was handled and the user
109 #pod saw a simple error message; sets X-Exception-Handled header
110 #pod and adds a text part at the beginning of the report,
111 #pod calling out the "handled" status"
112 #pod
113 #pod C<%internal_arg> contains data produced by the Exception::Reporter using this
114 #pod object. It includes the C<guid> of the report and the C<caller> calling the
115 #pod reporter.
116 #pod
117 #pod The mail is sent with the L<C<send_email>> method, which can be replaced in a
118 #pod subclass.
119 #pod
120 #pod The return value of C<send_report> is not defined.
121 #pod
122 #pod =cut
123  
124 sub send_report {
125 3 ▶ my ($self, $summaries, $arg, $internal_arg) = @_;
Subroutine send_report: called CC=3 SCAR=11.2
Pod documented
126  
127 # ?!? Presumably this can't really happen, but... you know what they say
128 # about zero-summary incidents, right? -- rjbs, 2012-07-03
129 3 ▶ Carp::confess("can't report a zero-summary incident!") unless @$summaries;
Branch: 1 branch
BranchTrueFalseTotal
unless @$summaries 0 3 3
130  
131 3 my $email = $self->_build_email($summaries, $arg, $internal_arg);
132  
133 # Maybe we should try{} to sanity check the extra rcpts first. -- rjbs,
134 # 2012-07-05
135 $self->send_email(
136 $email,
137 {
138 from => $self->env_from,
139 3 ▶ to => [ $self->env_to, @{ $arg->{extra_rcpts} || [] } ],
Branch: 1 branch
BranchTrueFalseTotal
$arg->{"extra_rcpts"} or [] 3 0 3
140 }
141 );
142  
143 3 return;
144 }
145  
146 #pod =method send_email
147 #pod
148 #pod $sender->send_email($email, \%env);
149 #pod
150 #pod This method expects an email object (such as can be handled by
151 #pod L<Email::Sender>) and a a hashref that will have these two keys:
152 #pod
153 #pod from - an envelope sender
154 #pod to - an arrayref of envelope recipients
155 #pod
156 #pod It sends the email. It should not throw an exception on failure. The default
157 #pod implementation uses Email::Sender. If the email injection fails, a warning is
158 #pod issued.
159 #pod
160 #pod =cut
161  
162 sub send_email {
163 3 ▶ my ($self, $email, $env) = @_;
Subroutine send_email: called CC=1 SCAR=0.1
Pod documented
164  
165 try {
166 3 ▶ Email::Sender::Simple->send($email, $env);
Subroutine __ANON__: called CC=1 SCAR=0.0
167 } catch {
168 0 ▶ Carp::cluck "failed to send exception report: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 3 };
170  
171 3 return;
172 }
173  
174 sub _build_email {
175 3 ▶ my ($self, $summaries, $arg, $internal_arg) = @_;
Subroutine _build_email: called CC=12 SCAR=24.9
176  
177 3 my @parts;
178 3 GROUP: for my $summary (@$summaries) {
179 9 my @these_parts;
180 9 for my $summary (@{ $summary->[1] }) {
181 push @these_parts, Email::MIME->create(
182 ($summary->{body_is_bytes} ? 'body' : 'body_str') => $summary->{body},
183 attributes => {
184 filename => $summary->{filename},
185 content_type => $summary->{mimetype},
186 encoding => 'quoted-printable',
187  
188 ($summary->{body_is_bytes}
189 ? ($summary->{charset} ? (charset => $summary->{charset}) : ())
190 11 ▶ : (charset => $summary->{charset} || 'utf-8')),
Branch: 3 branches
BranchTrueFalseTotal
$summary->{'body_is_bytes'} ? : 2 9 11
$summary->{'body_is_bytes'} ? : 2 9 11
$summary->{'charset'} ? : 1 1 2
Condition100%2/2
$summary->{"charset"} or "utf-8"
l!l
36
MC/DC100%1/1
$summary->{"charset"} or "utf-8"
$summary->{"charset"}
Truth table100%2/2
A
A= $summary->{"charset"}
Aresult
11
00
191 },
192 );
193  
194 11 $these_parts[-1]->header_set(Date=>);
195 11 $these_parts[-1]->header_set('MIME-Version'=>);
196 }
197  
198 9 ▶ if (@these_parts == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@these_parts == 1) { } 8 1 9
199 8 push @parts, @these_parts;
200 } else {
201 1 push @parts, Email::MIME->create(
202 attributes => { content_type => 'multipart/related' },
203 parts => \@these_parts,
204 );
205 1 $parts[-1]->header_set(Date=>);
206 1 $parts[-1]->header_set('MIME-Version'=>);
207 }
208  
209 9 $parts[-1]->name_set($summary->[0]);
210 }
211  
212 3 ▶ if ($arg->{handled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"handled"}) 1 2 3
213 1 unshift @parts, Email::MIME->create(
214 body_str => "DON'T PANIC!\n"
215 . "THIS EXCEPTION WAS CAUGHT AND EXECUTION CONTINUED\n"
216 . "THIS REPORT IS PROVIDED FOR INFORMATIONAL PURPOSES\n",
217 attributes => {
218 content_type => "text/plain",
219 charset => 'utf-8',
220 encoding => 'quoted-printable',
221 name => 'prelude',
222 },
223 );
224 1 $parts[-1]->header_set(Date=>);
225 1 $parts[-1]->header_set('MIME-Version'=>);
226 }
227  
228 my $ident = $summaries->[0][1][0]{ident} && $summaries->[0][1][0]{ident}
229 3 ▶ || "(unknown exception)";;
Condition40%2/5
$summaries->[0][1][0]{'ident'} && $summaries->[0][1][0]{'ident'} || '(unknown exception)'
l!l
30
$summaries->[0][1][0]{"ident"} and $summaries->[0][1][0]{"ident"}
!ll&&!rl&&r
003
MC/DC0%0/2
$summaries->[0][1][0]{'ident'} && $summaries->[0][1][0]{'ident'} || '(unknown exception)'
$summaries->[0][1][0]{"ident"} $summaries->[0][1][0]{"ident"}
Truth table33%1/3
A and B
A= $summaries->[0][1][0]{"ident"}B= $summaries->[0][1][0]{"ident"}
ABresult
111
0X0
100
230  
231 3 ($ident) = split /\n/, $ident;
232 3 $ident =~ s/\s+(?:at .+?)? ?line\s\d+\.?$//;
233  
234 3 my $digest_ident = $ident;
235 3 $digest_ident =~ s/\(.+//g;
236  
237 3 my ($package, $filename, $line) = @{ $internal_arg->{caller} };
238  
239 3 my $reporter = $arg->{reporter};
240  
241 my $email = Email::MIME->create(
242 attributes => { content_type => 'multipart/mixed' },
243 parts => \@parts,
244 header_str => [
245 From => $self->from_header,
246 To => $self->to_header,
247 Subject => String::Truncate::elide("$reporter: $ident", 65),
248 'X-Mailer' => (ref $self),
249 'Message-Id' => Email::MessageID->new(user => $internal_arg->{guid})
250 ->in_brackets,
251 'In-Reply-To'=> Email::MessageID->new(
252 user => Digest::MD5::md5_hex($digest_ident),
253 host => $reporter,
254 )->in_brackets,
255 'X-Exception-Reporter-Reporter' => $arg->{reporter},
256 'X-Exception-Reporter-Caller' => "$filename line $line ($package)",
257  
258 3 ▶ ($arg->{handled} ? ('X-Exception-Reporter-Handled' => 1) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'handled'} ? : 1 2 3
259 ],
260 );
261  
262 3 return $email;
263 }
264  
265 1 1;
266  
267 __END__
↑ Summary Reporter/Dumpable/File.pm »