File Coverage

blib/lib/Log/Log4perl/Appender/SMTP.pm
Criterion Covered Total %
statement 19 30 63.3
branch 0 8 0.0
condition 2 4 50.0
subroutine 6 7 85.7
pod 0 2 0.0
total 27 51 52.9


line stmt bran cond sub pod time code
1             package Log::Log4perl::Appender::SMTP;
2              
3             our @ISA = qw(Log::Log4perl::Appender);
4              
5 1     1   68204 use strict;
  1         3  
  1         30  
6 1     1   5 use warnings;
  1         2  
  1         38  
7 1     1   7 use Carp;
  1         2  
  1         68  
8 1     1   492 use Net::Domain 'hostfqdn';
  1         8699  
  1         63  
9 1     1   583 use Net::SMTP;
  1         92621  
  1         373  
10              
11             our $VERSION = '0.04';
12              
13             sub new {
14 1     1 0 710 my($class, @options) = @_;
15              
16 1   50     7 my $hname = hostfqdn() || '';
17 1   50     653 my $user = getlogin || getpwuid($<) || 'log4perl';
18              
19 1         20 return bless {
20             from => $user.'@'.$hname,
21             to => 'postmaster',
22             subject => "Subject: Log4perl from $hname\n",
23             Host => 'localhost',
24             @options
25             }, $class;
26             }
27              
28             sub log {
29 0     0 0   my ($self, %params) = @_;
30              
31 0 0         my $smtp = Net::SMTP->new(%$self) or return
32             carp "log4perl: could not connect to the SMTP server";
33              
34 0 0         $smtp->mail($self->{from}) or return
35             carp "log4perl: sender rejected: $self->{from}";
36              
37 0 0         $smtp->to(split /,/, $self->{to}) or return
38             carp "log4perl: recipient(s) rejected: $self->{to}";
39              
40 0           $smtp->data;
41 0           $smtp->datasend("From: ".$self->{from}."\n");
42 0           $smtp->datasend("To: ".$self->{to}."\n");
43 0           $smtp->datasend("Subject: ".$self->{subject}."\n");
44 0           $smtp->datasend("\n" . $params{message} . "\n");
45 0 0         $smtp->dataend or carp "log4perl: message could not be sent by smtp";
46 0           $smtp->quit;
47             }
48              
49             1;
50              
51             __END__