File Coverage

blib/lib/App/MonM/Channel/Email.pm
Criterion Covered Total %
statement 27 50 54.0
branch 0 10 0.0
condition 0 8 0.0
subroutine 9 14 64.2
pod 1 1 100.0
total 37 83 44.5


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