| 1 | package Exception::SendMail; | ||
| 2 | $Exception::SendMail::VERSION = '0.007'; | ||
| 3 | use base qw(Exception); | ||
| 4 | |||
| 5 | package Exception::SendMail::BadAddress; | ||
| 6 | $Exception::SendMail::BadAddress::VERSION = '0.007'; | ||
| 7 | use base qw(Exception::SendMail); | ||
| 8 | |||
| 9 | package QBit::Application::Model::SendMail; | ||
| 10 | $QBit::Application::Model::SendMail::VERSION = '0.007'; | ||
| 11 | use qbit; | ||
| 12 | use base qw(QBit::Application::Model); | ||
| 13 | use MIME::Base64 qw(encode_base64); | ||
| 14 | use MIME::Lite; | ||
| 15 | |||
| 16 | our %MESSAGE_STRUCT = ( | ||
| 17 | from => {type => 'email', max => 1, required => 1}, | ||
| 18 | to => {type => 'email', required => 1}, | ||
| 19 | cc => {type => 'email'}, | ||
| 20 | bcc => {type => 'email'}, | ||
| 21 | reply_to => {type => 'email'}, | ||
| 22 | subject => {type => 'str', required => 1}, | ||
| 23 | content_type => {type => 'contenttype', default => 'text/plain'}, | ||
| 24 | body => {type => 'str', conv_raw => 1, required => 1}, | ||
| 25 | attachments => { | ||
| 26 | type => 'struct', | ||
| 27 | struct => { | ||
| 28 | data => {type => 'str', conv_raw => 1}, | ||
| 29 | content_type => {type => 'contenttype', default => 'application/download'}, | ||
| 30 | filename => {type => 'str'}, | ||
| 31 | content_id => {type => 'str', conv_raw => 1}, | ||
| 32 | } | ||
| 33 | }, | ||
| 34 | source_spot => {type => 'str', conv_raw => 1}, | ||
| 35 | ); | ||
| 36 | |||
| 37 | our %TEMPLATERS = ( | ||
| 38 | TT2 => sub { | ||
| 39 | my $d = shift; | ||
| 40 | use Template; | ||
| 41 | my $template = Template->new({}); | ||
| 42 | my $out = ''; | ||
| 43 | $template->process($d->{template}, $d->{vars}, \$out) | ||
| 44 | || throw Exception::SendMail gettext('Template process error [%s]', $template->error()); | ||
| 45 | return $out; | ||
| 46 | }, | ||
| 47 | ); | ||
| 48 | |||
| 49 | our %FIELD_TYPE; | ||
| 50 | |||
| 51 | %FIELD_TYPE = ( | ||
| 52 | email => { | ||
| 53 | in => { | ||
| 54 | hash => [ | ||
| 55 | sub { | ||
| 56 | # in: '', { <email> => <name> }, [{}] | ||
| 57 | # out: [ { name => '', email => '' }, ... ] | ||
| 58 | my ($self, $data, $opt) = @_; | ||
| 59 | my $out = []; | ||
| 60 | |||
| 61 | if (ref($data) eq 'HASH') { | ||
| 62 | while (my ($email, $name) = each(%$data)) { | ||
| 63 | push(@$out, {name => $name, email => $email}); | ||
| 64 | } | ||
| 65 | } elsif (!ref($data) && $data) { | ||
| 66 | push(@$out, {name => '', email => $data}); | ||
| 67 | } elsif (ref($data) eq 'ARRAY') { | ||
| 68 | |||
| 69 | foreach my $d (@$data) { | ||
| 70 | foreach my $sub (@{$FIELD_TYPE{email}->{in}->{hash}}) { | ||
| 71 | my $res = $sub->($self, $d, $opt) if ref($sub) eq 'CODE' && defined($d); | ||
| 72 | if (defined $res) { | ||
| 73 | push(@$out, @$res); | ||
| 74 | last; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | } | ||
| 80 | |||
| 81 | foreach my $elem (@$out) { | ||
| 82 | $elem->{name} =~ s/(^\s*|\s*$)//g; | ||
| 83 | throw Exception::SendMail::BadAddress gettext('Bad email [%s]', $elem->{email}) | ||
| 84 | if grep {!check_email($_)} split(/,\s*/, $elem->{email}); | ||
| 85 | } | ||
| 86 | return $out; | ||
| 87 | }, | ||
| 88 | ] | ||
| 89 | }, | ||
| 90 | out => { | ||
| 91 | mime => [ | ||
| 92 | sub { | ||
| 93 | # out: '' | ||
| 94 | my ($self, $data, $opt) = @_; | ||
| 95 | return join( | ||
| 96 | ', ', | ||
| 97 | map({ | ||
| 98 | utf8::encode($_->{'name'}); | ||
| 99 | $_->{'name'} | ||
| 100 | ? '=?UTF-8?B?' . encode_base64($_->{'name'}, '') . '?= <' . $_->{'email'} . '>' | ||
| 101 | : $_->{'email'} | ||
| 102 | } @$data) | ||
| 103 | ); | ||
| 104 | }, | ||
| 105 | ], | ||
| 106 | }, | ||
| 107 | }, | ||
| 108 | |||
| 109 | contenttype => { | ||
| 110 | in => { | ||
| 111 | hash => [ | ||
| 112 | sub { | ||
| 113 | # in: '' | ||
| 114 | # out: '' | ||
| 115 | my ($self, $data, $opt, $secret) = @_; | ||
| 116 | my $out = lc($data); | ||
| 117 | throw Exception::SendMail gettext('Unknown content type [%s]', $data) | ||
| 118 | if ( | ||
| 119 | defined($out) | ||
| 120 | && !in_array( | ||
| 121 | $out, | ||
| 122 | [ | ||
| 123 | qw(text/plain text/html application/download image/jpeg image/png image/gif application/pdf application/xhtml+xml application/xml-dtd application/zip application/x-gzip) | ||
| 124 | ] | ||
| 125 | ) | ||
| 126 | ); | ||
| 127 | return $out; | ||
| 128 | }, | ||
| 129 | ], | ||
| 130 | }, | ||
| 131 | out => { | ||
| 132 | mime => [ | ||
| 133 | sub { | ||
| 134 | # out: '' | ||
| 135 | return $_[1]; | ||
| 136 | }, | ||
| 137 | ], | ||
| 138 | }, | ||
| 139 | }, | ||
| 140 | |||
| 141 | struct => { | ||
| 142 | in => { | ||
| 143 | hash => [ | ||
| 144 | sub { | ||
| 145 | # in: {} | ||
| 146 | # out: {} | ||
| 147 | # opt: struct | ||
| 148 | my ($self, $data, $opt) = @_; | ||
| 149 | return [ | ||
| 150 | map($self->_message_struct_in($_, 'hash', $opt->{struct}), | ||
| 151 | ref($data) eq 'ARRAY' ? @$data : ($data)) | ||
| 152 | ]; | ||
| 153 | }, | ||
| 154 | ], | ||
| 155 | }, | ||
| 156 | out => { | ||
| 157 | mime => [ | ||
| 158 | sub { | ||
| 159 | # out: {} | ||
| 160 | my ($self, $data, $opt) = @_; | ||
| 161 | return [map($self->_message_struct_out($_, 'mime', $opt->{struct}), @$data)]; | ||
| 162 | }, | ||
| 163 | ], | ||
| 164 | }, | ||
| 165 | }, | ||
| 166 | |||
| 167 | str => { | ||
| 168 | in => { | ||
| 169 | hash => [ | ||
| 170 | sub { | ||
| 171 | # in: '', \'', {} | ||
| 172 | # out: '' | ||
| 173 | my ($self, $data, $opt) = @_; | ||
| 174 | my $out; | ||
| 175 | if (!ref($data)) { | ||
| 176 | $out = $data; | ||
| 177 | } elsif (ref($data) eq 'SCALAR') { | ||
| 178 | $out = $$data; | ||
| 179 | } elsif (ref($data) eq 'HASH' && $TEMPLATERS{$data->{'type'}}) { | ||
| 180 | $out = $TEMPLATERS{$data->{'type'}}->($data); | ||
| 181 | } | ||
| 182 | return $out; | ||
| 183 | }, | ||
| 184 | ], | ||
| 185 | }, | ||
| 186 | out => { | ||
| 187 | mime => [ | ||
| 188 | sub { | ||
| 189 | # out: '' | ||
| 190 | # opt: conv_raw => 1' | ||
| 191 | my ($self, $data, $opt) = @_; | ||
| 192 | if ($opt->{conv_raw}) { | ||
| 193 | return $data; | ||
| 194 | } else { | ||
| 195 | utf8::encode($data); | ||
| 196 | return '=?UTF-8?B?' . encode_base64($data, '') . '?='; | ||
| 197 | } | ||
| 198 | }, | ||
| 199 | ], | ||
| 200 | }, | ||
| 201 | }, | ||
| 202 | ); | ||
| 203 | |||
| 204 | sub _message_struct_out { | ||
| 205 | my ($self, $data, $media, $struct) = @_; | ||
| 206 | my $out; | ||
| 207 | $struct //= \%MESSAGE_STRUCT; | ||
| 208 | while (my ($name, $opt) = each(%$struct)) { | ||
| 209 | # call export | ||
| 210 | my $sub = $FIELD_TYPE{$opt->{type}}->{out}->{$media}->[0] | ||
| 211 | if ($FIELD_TYPE{$opt->{type}}->{out}->{$media} | ||
| 212 | && ref($FIELD_TYPE{$opt->{type}}->{out}->{$media}->[0]) eq 'CODE'); | ||
| 213 | my $d = $data ? $data->{$name} : $self->{ucfirst($name)}; | ||
| 214 | next if !defined($d); | ||
| 215 | $out->{$name} = $sub ? $sub->($self, $d, $opt) : undef; | ||
| 216 | } | ||
| 217 | return $out; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub _message_struct_in { | ||
| 221 | my ($self, $data, $media, $struct) = @_; | ||
| 222 | my $level = $struct ? 1 : 0; | ||
| 223 | $struct ||= \%MESSAGE_STRUCT; | ||
| 224 | $media ||= 'hash'; | ||
| 225 | my $out; | ||
| 226 | while (my ($name, $opt) = each(%$struct)) { | ||
| 227 | # call import | ||
| 228 | my $res; | ||
| 229 | foreach my $sub (@{$FIELD_TYPE{$opt->{type}}->{in}->{$media}}, @{$FIELD_TYPE{$opt->{type}}->{in}->{''}}) { | ||
| 230 | # call field convertor | ||
| 231 | $res = $sub->($self, $data->{$name}, $opt, {in => $data, out => $out}) | ||
| 232 | if ref($sub) eq 'CODE' && defined($data->{$name}); | ||
| 233 | last if defined($res); | ||
| 234 | } | ||
| 235 | $res = $opt->{default} if (!defined($res)); # move 'defatult' behavior into convertor? | ||
| 236 | # check limits | ||
| 237 | throw Exception::SendMail gettext('No mandatory field [%s]', $name) | ||
| 238 | if ($opt->{required} && (!defined($res) || (ref($res) eq 'ARRAY' && @$res == 0))); | ||
| 239 | throw Exception::SendMail gettext('Too many fields [%s]', $name) | ||
| 240 | if ($opt->{max} && (ref($res) eq 'ARRAY' && @$res > $opt->{max})); | ||
| 241 | # result | ||
| 242 | $out->{$name} = $res; | ||
| 243 | } | ||
| 244 | return $out; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub _mail_create { | ||
| 248 | my ($self, $hash) = @_; | ||
| 249 | my $mail; | ||
| 250 | my $data; | ||
| 251 | |||
| 252 | # create message data | ||
| 253 | $data = $self->_message_struct_out($self->_message_struct_in($hash, 'hash'), 'mime'); | ||
| 254 | my $msg; | ||
| 255 | $msg->{headers} = { | ||
| 256 | hash_transform( | ||
| 257 | $data, | ||
| 258 | [], | ||
| 259 | { | ||
| 260 | from => 'From', | ||
| 261 | to => 'To', | ||
| 262 | subject => 'Subject', | ||
| 263 | cc => 'Cc', | ||
| 264 | bcc => 'Bcc', | ||
| 265 | source_spot => 'X-Source-Spot', | ||
| 266 | reply_to => 'Reply-To', | ||
| 267 | } | ||
| 268 | ) | ||
| 269 | }; | ||
| 270 | $msg->{headers}->{'Message-ID'} = '<' | ||
| 271 | . (time() . '_' . sprintf("%09d", rand(999999999))) . '.' | ||
| 272 | . $self->get_option('message_id', 'framework@qbit.ru') . '>'; | ||
| 273 | |||
| 274 | if ($data->{attachments}) { | ||
| 275 | $msg->{content_type} = 'multipart/related'; | ||
| 276 | $msg->{attachments} = [ | ||
| 277 | { | ||
| 278 | data => $data->{body}, | ||
| 279 | content_type => $data->{content_type}, | ||
| 280 | }, | ||
| 281 | @{$data->{attachments}}, | ||
| 282 | ]; | ||
| 283 | delete($msg->{body}); | ||
| 284 | } else { | ||
| 285 | $msg->{content_type} = $data->{content_type}; | ||
| 286 | $msg->{attachments} = []; | ||
| 287 | $msg->{body} = $data->{body}; | ||
| 288 | } | ||
| 289 | |||
| 290 | # create mail message | ||
| 291 | if ($msg->{body}) { | ||
| 292 | utf8::encode($msg->{body}) if (utf8::is_utf8($msg->{body})); | ||
| 293 | $mail = new MIME::Lite( | ||
| 294 | %{$msg->{headers}}, | ||
| 295 | Type => $msg->{content_type}, | ||
| 296 | 'Encoding' => 'base64', | ||
| 297 | 'Data' => $msg->{body}, | ||
| 298 | ); | ||
| 299 | } else { | ||
| 300 | $mail = new MIME::Lite(%{$msg->{headers}}, Type => $msg->{content_type},); | ||
| 301 | } | ||
| 302 | $mail->attr("content-type.charset" => "UTF-8"); | ||
| 303 | |||
| 304 | foreach my $attach (@{$msg->{'attachments'}}) { | ||
| 305 | utf8::encode($attach->{data}) if (utf8::is_utf8($attach->{data})); | ||
| 306 | my $att = $mail->attach( | ||
| 307 | Type => $attach->{content_type}, | ||
| 308 | Encoding => 'base64', | ||
| 309 | Data => $attach->{data}, | ||
| 310 | Filename => $attach->{filename}, | ||
| 311 | Id => $attach->{content_id}, | ||
| 312 | ); | ||
| 313 | $att->attr("content-type.charset" => "UTF-8"); | ||
| 314 | } | ||
| 315 | |||
| 316 | return $mail; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub send { | ||
| 320 | my ($self, %hash) = @_; | ||
| 321 | |||
| 322 | my $mail = $self->_mail_create(\%hash); | ||
| 323 | |||
| 324 | $self->_before_send($mail); | ||
| 325 | |||
| 326 | if ($self->get_option('via', 'sendmail') eq 'sendmail') { | ||
| 327 | $mail->send_by_sendmail(%{$self->get_option('sendmail')}) | ||
| 328 | || throw Exception::SendMail gettext("Can't send message"); | ||
| 329 | } elsif ($self->get_option('via') eq 'smtp') { | ||
| 330 | $mail->send_by_smtp(%{$self->get_option('smtp')}); | ||
| 331 | } elsif ($self->get_option('via') eq 'testfile') { | ||
| 332 | $mail->send_by_testfile($self->get_option('testfile')); | ||
| 333 | } | ||
| 334 | |||
| 335 | $self->_after_send($mail); | ||
| 336 | } | ||
| 337 | |||
| 338 | sub _before_send { } | ||
| 339 | sub _after_send { } | ||
| 340 | |||
| 341 | TRUE; | ||
| 342 | |||
| 343 | __END__ |