line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: Log::Dispatch::Email subclass that sends mail using Email::Mailer |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use 5.014; |
4
|
1
|
|
|
1
|
|
202886
|
use exact 'Log::Dispatch::Email'; |
|
1
|
|
|
|
|
8
|
|
5
|
1
|
|
|
1
|
|
350
|
use Email::Mailer; |
|
1
|
|
|
|
|
28955
|
|
|
1
|
|
|
|
|
3
|
|
6
|
1
|
|
|
1
|
|
223655
|
|
|
1
|
|
|
|
|
285483
|
|
|
1
|
|
|
|
|
419
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.12'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my %params = @_; |
10
|
|
|
|
|
|
|
delete $params{$_} for ( qw( buffer buffered level_names level_numbers max_level min_level name ) ); |
11
|
14
|
|
|
14
|
|
66
|
return %params; |
12
|
14
|
|
|
|
|
77
|
} |
13
|
14
|
|
|
|
|
52
|
|
14
|
|
|
|
|
|
|
my $self = shift->SUPER::new(@_); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my %params = _params( @_, %$self ); |
17
|
6
|
|
|
6
|
0
|
25093
|
delete $params{$_} for ( qw( message mailer level ) ); |
18
|
|
|
|
|
|
|
$self->{mailer} //= Email::Mailer->new(%params); |
19
|
6
|
|
|
|
|
1047
|
|
20
|
6
|
|
|
|
|
29
|
for ( qw( html text ) ) { |
21
|
6
|
|
33
|
|
|
54
|
$self->{$_} = $params{$_} if ( not exists $self->{$_} and exists $params{$_} ); |
22
|
|
|
|
|
|
|
} |
23
|
6
|
|
|
|
|
157
|
|
24
|
12
|
100
|
66
|
|
|
46
|
return $self; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
6
|
|
|
|
|
22
|
my $self = shift; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my %params = _params( @_, %$self ); |
30
|
|
|
|
|
|
|
$params{data}{messages} = ( ref $params{message} ) ? $params{message} : [ $params{message} ]; |
31
|
8
|
|
|
8
|
1
|
244
|
$params{data}{message} = join( "\n", @{ $params{data}{messages} } ); |
32
|
|
|
|
|
|
|
$params{text} = $params{data}{message} unless ( $self->{html} or $self->{text} ); |
33
|
8
|
|
|
|
|
33
|
delete $params{$_} for ( qw( message mailer level ) ); |
34
|
8
|
100
|
|
|
|
39
|
|
35
|
8
|
|
|
|
|
16
|
$self->{mailer}->send(%params); |
|
8
|
|
|
|
|
34
|
|
36
|
8
|
50
|
66
|
|
|
41
|
return; |
37
|
8
|
|
|
|
|
27
|
} |
38
|
|
|
|
|
|
|
|
39
|
8
|
|
|
|
|
36
|
my $self = shift; |
40
|
8
|
|
|
|
|
32743
|
|
41
|
|
|
|
|
|
|
if ( $self->{buffered} and @{ $self->{buffer} } ) { |
42
|
|
|
|
|
|
|
$self->send_email( message => $self->{buffer} ); |
43
|
|
|
|
|
|
|
$self->{buffer} = []; |
44
|
6
|
|
|
6
|
1
|
726
|
} |
45
|
|
|
|
|
|
|
|
46
|
6
|
100
|
66
|
|
|
23
|
return; |
|
5
|
|
|
|
|
16
|
|
47
|
5
|
|
|
|
|
15
|
} |
48
|
5
|
|
|
|
|
16
|
|
49
|
|
|
|
|
|
|
1; |
50
|
|
|
|
|
|
|
|
51
|
6
|
|
|
|
|
57
|
|
52
|
|
|
|
|
|
|
=pod |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=encoding UTF-8 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 NAME |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Log::Dispatch::Email::Mailer - Log::Dispatch::Email subclass that sends mail using Email::Mailer |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 VERSION |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
version 1.12 |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=for markdown [![test](https://github.com/gryphonshafer/Log-Dispatch-Email-Mailer/workflows/test/badge.svg)](https://github.com/gryphonshafer/Log-Dispatch-Email-Mailer/actions?query=workflow%3Atest) |
65
|
|
|
|
|
|
|
[![codecov](https://codecov.io/gh/gryphonshafer/Log-Dispatch-Email-Mailer/graph/badge.svg)](https://codecov.io/gh/gryphonshafer/Log-Dispatch-Email-Mailer) |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 SYNOPSIS |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
use Log::Dispatch; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# simple text email alert via Log::Dispatch |
72
|
|
|
|
|
|
|
my $log = Log::Dispatch->new( |
73
|
|
|
|
|
|
|
outputs => [ |
74
|
|
|
|
|
|
|
[ |
75
|
|
|
|
|
|
|
'Email::Mailer', |
76
|
|
|
|
|
|
|
min_level => 'alert', |
77
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
78
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
79
|
|
|
|
|
|
|
], |
80
|
|
|
|
|
|
|
], |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
$log->alert('This is to alert you something happened.'); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# simple text email alert via direct instantiation |
85
|
|
|
|
|
|
|
my $email = Log::Dispatch::Email::Mailer->new( |
86
|
|
|
|
|
|
|
min_level => 'alert', |
87
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
88
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
$email->log( |
91
|
|
|
|
|
|
|
message => 'This is to alert you something happened.', |
92
|
|
|
|
|
|
|
level => 'alert', |
93
|
|
|
|
|
|
|
); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# simple text email using an Email::Mailer object with explicit transport |
96
|
|
|
|
|
|
|
$log = Log::Dispatch->new( |
97
|
|
|
|
|
|
|
outputs => [ |
98
|
|
|
|
|
|
|
[ |
99
|
|
|
|
|
|
|
'Email::Mailer', |
100
|
|
|
|
|
|
|
min_level => 'alert', |
101
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
102
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
103
|
|
|
|
|
|
|
mailer => Email::Mailer->new( |
104
|
|
|
|
|
|
|
transport => Email::Sender::Transport::SMTP->new({ |
105
|
|
|
|
|
|
|
host => 'smtp.example.com', |
106
|
|
|
|
|
|
|
port => 25, |
107
|
|
|
|
|
|
|
}), |
108
|
|
|
|
|
|
|
), |
109
|
|
|
|
|
|
|
], |
110
|
|
|
|
|
|
|
], |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
$log->alert('This is to alert you something happened.'); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# HTML email alert with attached log file using Template Toolkit |
115
|
|
|
|
|
|
|
use Template; |
116
|
|
|
|
|
|
|
my $tt = Template->new; |
117
|
|
|
|
|
|
|
$log = Log::Dispatch->new( |
118
|
|
|
|
|
|
|
outputs => [ |
119
|
|
|
|
|
|
|
[ |
120
|
|
|
|
|
|
|
'Email::Mailer', |
121
|
|
|
|
|
|
|
min_level => 'alert', |
122
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
123
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
124
|
|
|
|
|
|
|
html => \q{ |
125
|
|
|
|
|
|
|
<pre>[% message %]</pre> |
126
|
|
|
|
|
|
|
<p>[% messages.join("<br>") %]</p> |
127
|
|
|
|
|
|
|
}, |
128
|
|
|
|
|
|
|
attachments => [ |
129
|
|
|
|
|
|
|
{ |
130
|
|
|
|
|
|
|
ctype => 'text/plain', |
131
|
|
|
|
|
|
|
content => 'This is plain text attachment content.', |
132
|
|
|
|
|
|
|
name => 'log_file.txt', |
133
|
|
|
|
|
|
|
}, |
134
|
|
|
|
|
|
|
], |
135
|
|
|
|
|
|
|
process => sub { |
136
|
|
|
|
|
|
|
my ( $template, $data ) = @_; |
137
|
|
|
|
|
|
|
my $content; |
138
|
|
|
|
|
|
|
$tt->process( \$template, $data, \$content ); |
139
|
|
|
|
|
|
|
return $content; |
140
|
|
|
|
|
|
|
}, |
141
|
|
|
|
|
|
|
], |
142
|
|
|
|
|
|
|
], |
143
|
|
|
|
|
|
|
); |
144
|
|
|
|
|
|
|
$log->alert('This is to alert you something happened.'); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 DESCRIPTION |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This is a subclass of L<Log::Dispatch::Email> that implements the C<send_email()> |
149
|
|
|
|
|
|
|
method using the L<Email::Mailer> module. Much like the L<Email::Mailer> module, |
150
|
|
|
|
|
|
|
you can send email in a great variety of ways including text-only, HTML with |
151
|
|
|
|
|
|
|
text auto-generated, including attachments, and even using your favorite |
152
|
|
|
|
|
|
|
templating system. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 Simple Text Email |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
The simplest way to use this module is to setup an "outputs" record with |
157
|
|
|
|
|
|
|
L<Log::Dispatch> much like you would any other email subclass. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
my $log = Log::Dispatch->new( |
160
|
|
|
|
|
|
|
outputs => [ |
161
|
|
|
|
|
|
|
[ |
162
|
|
|
|
|
|
|
'Email::Mailer', |
163
|
|
|
|
|
|
|
min_level => 'alert', |
164
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
165
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
166
|
|
|
|
|
|
|
], |
167
|
|
|
|
|
|
|
], |
168
|
|
|
|
|
|
|
); |
169
|
|
|
|
|
|
|
$log->alert('This is to alert you something happened.'); |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
By default, log messages are buffered and sent either when C<$log> is destroyed |
172
|
|
|
|
|
|
|
or when you call C<< $log->flush >>. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$log->alert('This message will appear in an email.'); |
175
|
|
|
|
|
|
|
$log->alert('This message will appear in the same email, but not yet...'); |
176
|
|
|
|
|
|
|
$log->flush; # now both alerts will get sent in one email |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Note that unlike many other L<Log::Dispatch::Email> subclasses, multiple |
179
|
|
|
|
|
|
|
buffered messages won't be concatenated together without spaces. Instead, the |
180
|
|
|
|
|
|
|
messages will appear in a text-only email as independent lines. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
As an alternative to buffering, you can explicitly set buffering off to have |
183
|
|
|
|
|
|
|
each log line send a single email. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my $log = Log::Dispatch->new( |
186
|
|
|
|
|
|
|
outputs => [ |
187
|
|
|
|
|
|
|
[ |
188
|
|
|
|
|
|
|
'Email::Mailer', |
189
|
|
|
|
|
|
|
min_level => 'alert', |
190
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
191
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
192
|
|
|
|
|
|
|
buffer => 0, |
193
|
|
|
|
|
|
|
], |
194
|
|
|
|
|
|
|
], |
195
|
|
|
|
|
|
|
); |
196
|
|
|
|
|
|
|
$log->alert('This will be in one email.'); |
197
|
|
|
|
|
|
|
$log->alert('This will be in a second email.'); |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 Simple Text Email with Explicit Transport |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
By default, this module will create its own L<Email::Mailer> object through |
202
|
|
|
|
|
|
|
which to send email. You can provide a "mailer" value of an explicit |
203
|
|
|
|
|
|
|
L<Email::Mailer> object you create and control, thus allowing you to set things |
204
|
|
|
|
|
|
|
like an explicit transport mechanism. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
my $log = Log::Dispatch->new( |
207
|
|
|
|
|
|
|
outputs => [ |
208
|
|
|
|
|
|
|
[ |
209
|
|
|
|
|
|
|
'Email::Mailer', |
210
|
|
|
|
|
|
|
min_level => 'alert', |
211
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
212
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
213
|
|
|
|
|
|
|
mailer => Email::Mailer->new( |
214
|
|
|
|
|
|
|
transport => Email::Sender::Transport::SMTP->new({ |
215
|
|
|
|
|
|
|
host => 'smtp.example.com', |
216
|
|
|
|
|
|
|
port => 25, |
217
|
|
|
|
|
|
|
}), |
218
|
|
|
|
|
|
|
), |
219
|
|
|
|
|
|
|
], |
220
|
|
|
|
|
|
|
], |
221
|
|
|
|
|
|
|
); |
222
|
|
|
|
|
|
|
$log->alert('This is to alert you something happened.'); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 HTML Email with Attached File Using Template Toolkit |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
If you want to have some real fun with sending email log messages (and let's be |
227
|
|
|
|
|
|
|
real here, who doesn't), try using this module to send templated HTML email |
228
|
|
|
|
|
|
|
with attachments. Any key/value you can pass to L<Email::Mailer>, you can pass |
229
|
|
|
|
|
|
|
as part of the "outputs" element. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
The following example uses an HTML template (which per L<Email::Mailer> needs |
232
|
|
|
|
|
|
|
to be a scalar reference) and a very simple Template Toolkit process subref. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
use Template; |
235
|
|
|
|
|
|
|
my $tt = Template->new; |
236
|
|
|
|
|
|
|
my $log = Log::Dispatch->new( |
237
|
|
|
|
|
|
|
outputs => [ |
238
|
|
|
|
|
|
|
[ |
239
|
|
|
|
|
|
|
'Email::Mailer', |
240
|
|
|
|
|
|
|
min_level => 'alert', |
241
|
|
|
|
|
|
|
to => [ qw( foo@example.com bar@example.org ) ], |
242
|
|
|
|
|
|
|
subject => 'Alert Log Message', |
243
|
|
|
|
|
|
|
html => \q{ |
244
|
|
|
|
|
|
|
<pre>[% message %]</pre> |
245
|
|
|
|
|
|
|
<p>[% messages.join("<br>") %]</p> |
246
|
|
|
|
|
|
|
}, |
247
|
|
|
|
|
|
|
attachments => [ |
248
|
|
|
|
|
|
|
{ |
249
|
|
|
|
|
|
|
ctype => 'text/plain', |
250
|
|
|
|
|
|
|
content => 'This is plain text attachment content.', |
251
|
|
|
|
|
|
|
name => 'log_file.txt', |
252
|
|
|
|
|
|
|
}, |
253
|
|
|
|
|
|
|
], |
254
|
|
|
|
|
|
|
process => sub { |
255
|
|
|
|
|
|
|
my ( $template, $data ) = @_; |
256
|
|
|
|
|
|
|
my $content; |
257
|
|
|
|
|
|
|
$tt->process( \$template, $data, \$content ); |
258
|
|
|
|
|
|
|
return $content; |
259
|
|
|
|
|
|
|
}, |
260
|
|
|
|
|
|
|
], |
261
|
|
|
|
|
|
|
], |
262
|
|
|
|
|
|
|
); |
263
|
|
|
|
|
|
|
$log->alert('This is to alert you something happened.'); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
What's happening behind the scenes is that the "data" value that you'd normally |
266
|
|
|
|
|
|
|
pass to L<Email::Mailer> that would work its way down into the "process" subref |
267
|
|
|
|
|
|
|
is in this case being generated for you. It gets populated with two sub-keys: |
268
|
|
|
|
|
|
|
message and messages. The first is a "\n"-separated string of log messages. |
269
|
|
|
|
|
|
|
The second is an arrayref of those strings. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 SEE ALSO |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
L<Email::Mailer>, L<Log::Dispatch::Email>, L<Log::Dispatch>. |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
You can also look for additional information at: |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=over 4 |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=item * |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
L<GitHub|https://github.com/gryphonshafer/Log-Dispatch-Email-Mailer> |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item * |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
L<MetaCPAN|https://metacpan.org/pod/Log::Dispatch::Email::Mailer> |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=item * |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
L<GitHub Actions|https://github.com/gryphonshafer/Log-Dispatch-Email-Mailer/actions> |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item * |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
L<Codecov|https://codecov.io/gh/gryphonshafer/Log-Dispatch-Email-Mailer> |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item * |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
L<CPANTS|http://cpants.cpanauthors.org/dist/Log-Dispatch-Email-Mailer> |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=item * |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
L<CPAN Testers|http://www.cpantesters.org/distro/D/Log-Dispatch-Email-Mailer.html> |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=back |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 AUTHOR |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
Gryphon Shafer <gryphon@cpan.org> |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
This software is Copyright (c) 2017-2050 by Gryphon Shafer. |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
This is free software, licensed under: |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=cut |