| 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 $body = $args->{'body'}; | ||
| 30 | $body ||= $msg; | ||
| 31 | my $to = $args->{'to'}; | ||
| 32 | my $smtp_ip = $args->{'smtp_ip'}; | ||
| 33 | my $smtp_port = $args->{'smtp_port'}; | ||
| 34 | my $subject = $args->{'subject'}; | ||
| 35 | $smtp_ip ||= '127.0.0.1'; | ||
| 36 | $smtp_port ||= '25'; | ||
| 37 | my $force_text = $args->{'force_text'}; | ||
| 38 | $force_text ||= 0; | ||
| 39 | |||
| 40 | my $from = $args->{'from'}; | ||
| 41 | |||
| 42 | if (!$from) { | ||
| 43 | warn("Missing 'from' address in $args config " . Dumper($args)); | ||
| 44 | return 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | if (!$to) { | ||
| 48 | warn("Missing 'to' address in $args config " . Dumper($args)); | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | |||
| 52 | return $self->_send_email({to=>$to, from=>$from, subject=>$subject, alt_body=>$msg, body=>$body, force_text=>$force_text, 'smtp_ip'=>$smtp_ip, 'smtp_port'=>$smtp_port}); | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | sub _send_email { | ||
| 57 | |||
| 58 | my($self,$h) = @_; | ||
| 59 | |||
| 60 | # use Data::Dumper; | ||
| 61 | # warn "_mail_alt called: " . Dumper( @_ ); | ||
| 62 | use MIME::Entity; | ||
| 63 | use HTML::TokeParser; | ||
| 64 | use HTML::FormatText; | ||
| 65 | use Mail::SendGrid::SmtpApiHeader; | ||
| 66 | |||
| 67 | my $debug = 0; # this stops output from going to mail | ||
| 68 | my $sendmail = $^O eq "freebsd" ? '/usr/sbin/sendmail -t' : '/usr/lib/sendmail -t -i -Nfailure'; | ||
| 69 | my $mailer = $h->{mailer} ? $h->{mailer} : "SAS-1.0"; | ||
| 70 | my $success = 0; | ||
| 71 | my $host = $h->{'smtp_ip'}; | ||
| 72 | my $port = $h->{'smtp_port'}; | ||
| 73 | |||
| 74 | $h->{cc} ||= ''; | ||
| 75 | $h->{to} ||= ''; | ||
| 76 | |||
| 77 | $h->{to} =~ s/ +//g; | ||
| 78 | $h->{cc} =~ s/ +//g; | ||
| 79 | |||
| 80 | $h->{cid} ||= 0; | ||
| 81 | $h->{delivery_id} ||= 0; | ||
| 82 | $h->{body} ||= ''; | ||
| 83 | $h->{from} ||= ''; | ||
| 84 | $h->{to} ||= ''; | ||
| 85 | $h->{cc} ||= ''; | ||
| 86 | $h->{subject} ||= ''; | ||
| 87 | $h->{alt_body} ||= ''; | ||
| 88 | |||
| 89 | $h->{from} =~ s/[^\x00-\x7f]//g; | ||
| 90 | $h->{to} =~ s/[^\x00-\x7f]//g; | ||
| 91 | $h->{cc} =~ s/[^\x00-\x7f]//g; | ||
| 92 | $h->{bcc} =~ 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 | Charset => 'UTF-8', | ||
| 151 | 'X-NetSocial-CID' => $h->{cid}, | ||
| 152 | 'X-Delivery-ID' => $h->{delivery_id}, | ||
| 153 | 'X-Mailer' => $mailer, | ||
| 154 | 'X-SMTPAPI' => $sg_header->asJSON; | ||
| 155 | |||
| 156 | if ($type eq 'multipart/mixed') { | ||
| 157 | $content = $msg->attach( | ||
| 158 | Type => 'multipart/alternative' | ||
| 159 | ); | ||
| 160 | } else { | ||
| 161 | $content = $msg; | ||
| 162 | } | ||
| 163 | |||
| 164 | ### Alternative #1 is the plain text: | ||
| 165 | $content->attach( | ||
| 166 | Type => 'text/plain', | ||
| 167 | Data => $h->{alt_body}, | ||
| 168 | Encoding => 'quoted-printable' | ||
| 169 | ); | ||
| 170 | |||
| 171 | ### Alternative #2 is the HTML-with-content: | ||
| 172 | $content->attach( | ||
| 173 | Type => 'text/html', | ||
| 174 | Charset => 'UTF-8', | ||
| 175 | Data => $h->{body}, | ||
| 176 | Encoding => 'quoted-printable' | ||
| 177 | ); | ||
| 178 | |||
| 179 | if ($file_path) { | ||
| 180 | |||
| 181 | $msg->attach( | ||
| 182 | Filename => $h->{file_name}, | ||
| 183 | Path => $file_path, | ||
| 184 | Type => $h->{file_type}, | ||
| 185 | Disposition => "attachment", | ||
| 186 | Encoding => 'base64' | ||
| 187 | ); | ||
| 188 | |||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | } else { | ||
| 193 | |||
| 194 | $msg = build MIME::Entity | ||
| 195 | From => $h->{from}, | ||
| 196 | To => $h->{to}, | ||
| 197 | Cc => $h->{cc}, | ||
| 198 | Bcc => $h->{bcc}, | ||
| 199 | 'Reply-To' => $h->{replyto}, | ||
| 200 | Subject => $h->{subject}, | ||
| 201 | Type => $type, | ||
| 202 | Charset => 'UTF-8', | ||
| 203 | 'X-Geekwarriors-CID' => $h->{cid}, | ||
| 204 | 'X-Delivery-ID' => $h->{delivery_id}, | ||
| 205 | 'X-Mailer' => $mailer, | ||
| 206 | 'X-SMTPAPI' => $sg_header->asJSON, | ||
| 207 | Data => $h->{body}; | ||
| 208 | |||
| 209 | if ($file_path) { | ||
| 210 | |||
| 211 | $msg->attach( | ||
| 212 | Filename => $h->{file_name}, | ||
| 213 | Path => $file_path, | ||
| 214 | Type => $h->{file_type}, | ||
| 215 | Encoding => 'base64' | ||
| 216 | ); | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | } | ||
| 221 | |||
| 222 | # send it out | ||
| 223 | if( $h->{to} =~ m/(.*)\@(.*?)\..*/i && !$debug) { | ||
| 224 | use Net::SMTP; | ||
| 225 | my $smtp = Net::SMTP->new( "$host:$port" , Debug => 1); | ||
| 226 | if (!$smtp) { | ||
| 227 | warn("SMTP Failure: $@"); | ||
| 228 | return 0; | ||
| 229 | } | ||
| 230 | $smtp->mail( $h->{from} ); | ||
| 231 | $smtp->to( $h->{to} ); | ||
| 232 | $smtp->cc( $h->{cc} )unless !$h->{cc}; | ||
| 233 | $smtp->bcc( $h->{bcc} ) unless !$h->{bcc}; | ||
| 234 | $smtp->data(); | ||
| 235 | $smtp->datasend( $msg->stringify ); | ||
| 236 | $smtp->dataend(); | ||
| 237 | $success = 1; | ||
| 238 | # warn('queued mail to ' . $h->{to}); | ||
| 239 | # warn($msg->stringify); | ||
| 240 | } else { | ||
| 241 | warn('would send to ' . $h->{to} . "\n"); | ||
| 242 | warn($msg->stringify . "\n"); | ||
| 243 | warn("========\n"); | ||
| 244 | # pretend it went out | ||
| 245 | $success = 1; | ||
| 246 | } | ||
| 247 | |||
| 248 | return $success; | ||
| 249 | |||
| 250 | } | ||
| 251 | |||
| 252 | 1; |