Engine/Utility/Email.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 43 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 245 CC 23 SCAR 54.6
CC
23
Cov
0%
_send_email
420.0
send
12.0
new
6.0
CRAP 235.3
1 package Service::Engine::Utility::Email;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6 use Data::Dumper;
7 use Service::Engine;
8  
9 sub new {
10 my ($class,$options) = @_;
11
12 # set some defaults
13 my $attributes = {'methods'=>{}};
14
15 # load options
16 if (ref($options) eq 'HASH') {
17 foreach my $option (keys %{$options}) {
18 $attributes->{$option} = $options->{$option};
19 }
20 }
21 my $self = bless $attributes, $class;
22 }
23  
24 sub send {
25  
26 my ($self,$args) = @_;
27
28 my $msg = $args->{'msg'};
29 my $to = $args->{'to'};
30 my $smtp_ip = $args->{'smtp_ip'};
31 my $smtp_port = $args->{'smtp_port'};
32 my $subject = $args->{'subject'};
33 $smtp_ip ||= '127.0.0.1';
34 $smtp_port ||= '25';
35 my $force_text = $args->{'force_text'};
36 $force_text ||= 0;
37
38 my $from = $args->{'from'};
39
40 if (!$from) {
41 warn("Missing 'from' address in $args config " . Dumper($args));
42 return 0;
43 }
44
45 if (!$to) {
46 warn("Missing 'to' address in $args config " . Dumper($args));
47 return 0;
48 }
49
50 return $self->_send_email({to=>$to, from=>$from, subject=>$subject, alt_body=>$msg, force_text=>$force_text, 'smtp_ip'=>$smtp_ip, 'smtp_port'=>$smtp_port});
51  
52 }
53  
54 sub _send_email {
55  
56 my($self,$h) = @_;
57
58 # use Data::Dumper;
59 # warn "_mail_alt called: " . Dumper( @_ );
60 use MIME::Entity;
61 use HTML::TokeParser;
62 use HTML::FormatText;
63 use Mail::SendGrid::SmtpApiHeader;
64  
65 my $debug = 0; # this stops output from going to mail
66 my $sendmail = $^O eq "freebsd" ? '/usr/sbin/sendmail -t' : '/usr/lib/sendmail -t -i -Nfailure';
67 my $mailer = $h->{mailer} ? $h->{mailer} : "SAS-1.0";
68 my $success = 0;
69 my $host = $h->{'smtp_ip'};
70 my $port = $h->{'smtp_port'};
71  
72 $h->{cc} ||= '';
73 $h->{to} ||= '';
74  
75 $h->{to} =~ s/ +//g;
76 $h->{cc} =~ s/ +//g;
77
78 $h->{cid} ||= 0;
79 $h->{delivery_id} ||= 0;
80 $h->{body} ||= '';
81 $h->{from} ||= '';
82 $h->{to} ||= '';
83 $h->{cc} ||= '';
84 $h->{subject} ||= '';
85 $h->{alt_body} ||= '';
86
87 $h->{body} =~ s/[^\x00-\x7f]//g;
88 $h->{from} =~ s/[^\x00-\x7f]//g;
89 $h->{to} =~ s/[^\x00-\x7f]//g;
90 $h->{cc} =~ s/[^\x00-\x7f]//g;
91 $h->{subject} =~ s/[^\x00-\x7f]//g;
92 $h->{alt_body} =~ s/[^\x00-\x7f]//g;
93  
94 # create sendgrid header
95 my $sg_header = Mail::SendGrid::SmtpApiHeader->new();
96 $sg_header->addFilterSetting( bypass_list_management => 'enable', 1 );
97 $sg_header->setUniqueArgs({delivery_id => "$h->{delivery_id}"}) unless !$h->{delivery_id};
98
99 # Determine what type of mail we are going to send
100 my $force_text = $h->{force_text} ? 1 : 0;
101 my $tokenized_body = HTML::TokeParser->new( \$h->{body} ) || warn "that didn't work!";
102 my $tag = $tokenized_body->get_tag();
103
104 my $add_head = ($tag && uc @$tag[0] eq 'HTML' ) ? 0 : 1;
105
106 if ($add_head && $tag) {
107 $h->{body} = "<HTML><BODY>" . $h->{body} . "</BODY></HTML>";
108 }
109
110 my $type = $tag ? 'multipart/alternative' : 'text/plain';
111
112 my $file_path = '';
113 if ($h->{file_path} && -e $h->{file_path}) {
114 $type = 'multipart/mixed';
115 $file_path = $h->{file_path};
116 }
117
118 if ($force_text) {
119 $type = 'text/plain';
120 $tag = '';
121 $h->{body} = $h->{alt_body} if $h->{alt_body};
122 }
123
124 $tokenized_body->unget_token();
125
126 # if there is no alt_body and it is HTML - then build an alt
127 if ($tag && !$h->{alt_body}) {
128
129 # build alt content
130 require HTML::TreeBuilder;
131 my $tree = HTML::TreeBuilder->new->parse($h->{body});
132 my $formated_text = HTML::FormatText->new();
133 $h->{alt_body}=$formated_text->format($tree);
134
135 }
136
137 my $msg='';
138 my $content = '';
139
140 if ($type ne 'text/plain') {
141
142 $msg = build MIME::Entity
143 From => $h->{from},
144 To => $h->{to},
145 Cc => $h->{cc},
146 Bcc => $h->{bcc},
147 'Reply-To' => $h->{replyto},
148 Subject => $h->{subject},
149 Type => $type,
150 'X-NetSocial-CID' => $h->{cid},
151 'X-Delivery-ID' => $h->{delivery_id},
152 'X-Mailer' => $mailer,
153 'X-SMTPAPI' => $sg_header->asJSON;
154
155 if ($type eq 'multipart/mixed') {
156 $content = $msg->attach(
157 Type => 'multipart/alternative'
158 );
159 } else {
160 $content = $msg;
161 }
162
163 ### Alternative #1 is the plain text:
164 $content->attach(
165 Type => 'text/plain',
166 Data => $h->{alt_body},
167 Encoding => 'quoted-printable'
168 );
169
170 ### Alternative #2 is the HTML-with-content:
171 $content->attach(
172 Type => 'text/html',
173 Data => $h->{body},
174 Encoding => 'quoted-printable'
175 );
176
177 if ($file_path) {
178
179 $msg->attach(
180 Filename => $h->{file_name},
181 Path => $file_path,
182 Type => $h->{file_type},
183 Disposition => "attachment",
184 Encoding => 'base64'
185 );
186
187
188 }
189
190 } else {
191
192 $msg = build MIME::Entity
193 From => $h->{from},
194 To => $h->{to},
195 Cc => $h->{cc},
196 Bcc => $h->{bcc},
197 'Reply-To' => $h->{replyto},
198 Subject => $h->{subject},
199 Type => $type,
200 'X-Geekwarriors-CID' => $h->{cid},
201 'X-Delivery-ID' => $h->{delivery_id},
202 'X-Mailer' => $mailer,
203 'X-SMTPAPI' => $sg_header->asJSON,
204 Data => $h->{body};
205
206 if ($file_path) {
207
208 $msg->attach(
209 Filename => $h->{file_name},
210 Path => $file_path,
211 Type => $h->{file_type},
212 Encoding => 'base64'
213 );
214
215 }
216
217 }
218
219 # send it out
220 if( $h->{to} =~ m/(.*)\@(.*?)\..*/i && !$debug) {
221 use Net::SMTP;
222 my $smtp = Net::SMTP->new( "$host:$port" , Debug => 1);
223 if (!$smtp) {
224 warn("SMTP Failure: $@");
225 return 0;
226 }
227 $smtp->mail( $h->{from} );
228 $smtp->to( $h->{to} );
229 $smtp->cc( $h->{cc} )unless !$h->{cc};
230 $smtp->bcc( $h->{bcc} ) unless !$h->{bcc};
231 $smtp->data();
232 $smtp->datasend( $msg->stringify );
233 $smtp->dataend();
234 $success = 1;
235 # warn('queued mail to ' . $h->{to});
236 # warn($msg->stringify);
237 } else {
238 warn('would send to ' . $h->{to} . "\n");
239 warn($msg->stringify . "\n");
240 warn("========\n");
241 # pretend it went out
242 $success = 1;
243 }
244
245 return $success;
246  
247 }
248  
249 1;