File Coverage

blib/lib/App/MonM/Notifier/Channel/Email.pm
Criterion Covered Total %
statement 27 63 42.8
branch 0 22 0.0
condition 0 12 0.0
subroutine 9 14 64.2
pod 1 1 100.0
total 37 112 33.0


line stmt bran cond sub pod time code
1             package App::MonM::Notifier::Channel::Email; # $Id: Email.pm 60 2019-07-14 09:57:26Z abalama $
2 1     1   7 use strict;
  1         1  
  1         24  
3 1     1   5 use utf8;
  1         1  
  1         5  
4              
5             =encoding utf-8
6              
7             =head1 NAME
8              
9             App::MonM::Notifier::Channel::Email - monotifier email channel
10              
11             =head1 VERSION
12              
13             Version 1.01
14              
15             =head1 SYNOPSIS
16              
17            
18             Type Email
19              
20             # Real To and From
21             To test@example.com
22             From root@example.com
23              
24             # Options
25             #Encoding base64
26              
27             # Headers
28            
29             X-Foo foo
30             X-Bar bar
31            
32              
33             # SMTP options
34             # If there are requirements to the register of parameter
35             # names, use the Set directive, for example:
36             # By default will use section of general config file
37             Set host 192.168.0.1
38             Set port 25
39             #Set sasl_username TeStUser
40             #Set sasl_password MyPassword
41              
42            
43              
44             =head1 DESCRIPTION
45              
46             This module provides email method
47              
48             =head2 DIRECTIVES
49              
50             =over 4
51              
52             =item B
53              
54             Sender address or name
55              
56             =item B
57              
58             Sets SMTP options:
59              
60             Set host SMTPHOST
61              
62             SMTP option "host". Contains hostname or IP of remote SMTP server
63              
64             Default: localhost
65              
66             Set port PORT
67              
68             SMTP option "port". Contains port to connect to
69              
70             Defaults to 25 for non-SSL, 465 for 'ssl', 587 for 'starttls'
71              
72             Set timeout TIMEOUT
73              
74             Maximum time in secs to wait for server
75              
76             Default: 120
77              
78             Set helo HELOSTRING
79              
80             SMTP attribute. What to say when saying HELO. Optional
81              
82             No default
83              
84             Set sasl_username USERNAME
85              
86             This is sasl_username SMTP attribute. Optional
87              
88             Contains the username to use for auth
89              
90             Set sasl_password PASSWORD
91              
92             This is sasl_password SMTP attribute. Optional
93              
94             Set ssl 1
95              
96             This is ssl SMTP attribute: if 'starttls', use STARTTLS;
97             if 'ssl' (or 1), connect securely; otherwise, no security.
98              
99             Default: undef
100              
101             See L
102              
103             =item B
104              
105             Recipient address or name
106              
107             =item B
108              
109             Defines type of channel. MUST BE set to "Email" value
110              
111             =back
112              
113             About other options (base) see L
114              
115             =head2 METHODS
116              
117             =over 4
118              
119             =item B
120              
121             For internal use only!
122              
123             =back
124              
125             =head1 HISTORY
126              
127             See C file
128              
129             =head1 DEPENDENCIES
130              
131             L, L, L
132              
133             =head1 TO DO
134              
135             See C file
136              
137             =head1 SEE ALSO
138              
139             L, L, L, L,
140             L
141              
142             =head1 AUTHOR
143              
144             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
145              
146             =head1 COPYRIGHT
147              
148             Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
149              
150             =head1 LICENSE
151              
152             This program is free software; you can redistribute it and/or
153             modify it under the same terms as Perl itself.
154              
155             See C file and L
156              
157             =cut
158              
159 1     1   39 use vars qw/$VERSION/;
  1         1  
  1         38  
160             $VERSION = '1.01';
161              
162 1     1   5 use CTK::ConfGenUtil;
  1         2  
  1         53  
163 1     1   5 use CTK::TFVals qw/ :ALL /;
  1         2  
  1         159  
164 1     1   356 use Email::Sender::Simple qw//;
  1         99817  
  1         23  
165 1     1   380 use Email::Sender::Transport::SMTP;
  1         16874  
  1         28  
166 1     1   7 use Try::Tiny;
  1         3  
  1         61  
167              
168 1     1   6 use App::MonM::Util qw/ set2attr /;
  1         3  
  1         412  
169              
170             sub process {
171 0     0 1   my $self = shift;
172 0           my $type = $self->type;
173 0 0         return $self->maybe::next::method() unless $type eq 'email';
174 0           my $message = $self->message;
175 0 0         unless ($message) {
176 0           $self->error("Incorrect Email::MIME object");
177 0           return;
178             }
179             my $message_id = $self->genId(
180             $self->data->{id} || 0,
181             $self->data->{pubdate} || 0,
182 0   0       $self->data->{to} || "anonymous",
      0        
      0        
183             );
184              
185             # eXtra headers (extension headers)
186 0           $message->header_str_set("X-Id" => $message_id );
187 0           $message->header_str_set("X-Mailer" => sprintf("%s/%s", __PACKAGE__, $VERSION) );
188              
189             # Options
190 0           my $configobj = $self->{configobj};
191 0           my $options = set2attr($self->config);
192 0 0         my $sendmail_def = $configobj->conf("sendmail") if $configobj;
193              
194             # SMTP options
195 0           my %smtp_opts = %$options;
196 0 0         unless (%smtp_opts) {
197 0 0         $smtp_opts{host} = value($sendmail_def, "smtp") if value($sendmail_def, "smtp");
198 0 0         $smtp_opts{sasl_username} = value($sendmail_def, "smtpuser") if value($sendmail_def, "smtpuser");
199 0 0         $smtp_opts{sasl_password} = value($sendmail_def, "smtppass") if value($sendmail_def, "smtppass");
200             }
201              
202             # General
203 0 0         my $try_sendmail_first = $smtp_opts{host} ? 0 : 1;
204 0           my $sent_status = 1;
205 0           my $sent_error = "";
206              
207             # Try via sendmail
208 0 0         if ($try_sendmail_first) {
209             try {
210 0     0     Email::Sender::Simple->send($message);
211             } catch {
212 0     0     $sent_status = 0;
213 0   0       $sent_error = $_ || 'unknown error';
214 0           };
215 0 0         return $self->status($sent_status) if $sent_status;
216             }
217              
218             # Now send via SMTP
219 0           $sent_status = 1;
220 0           my $transport = Email::Sender::Transport::SMTP->new({%smtp_opts});
221             try {
222 0     0     Email::Sender::Simple->send($message, { transport => $transport });
223             } catch {
224 0     0     $sent_status = 0;
225 0   0       $sent_error = $_ || 'unknown error';
226 0           };
227 0 0         return $self->status($sent_status) if $sent_status;
228              
229             # Errors
230 0   0       $self->error(sprintf("Can't send message: %s", $sent_error // "unknown error"));
231 0           return;
232             }
233              
234             1;
235              
236             __END__