24 |
24 |
package Mailer;
|
25 |
25 |
|
26 |
26 |
use Email::Address;
|
27 |
|
use MIME::Entity;
|
28 |
|
use MIME::Parser;
|
|
27 |
use Email::MIME::Creator;
|
29 |
28 |
use File::MimeInfo::Magic;
|
30 |
29 |
use File::Slurp;
|
31 |
30 |
use List::UtilsBy qw(bundle_by);
|
... | ... | |
40 |
39 |
use Encode;
|
41 |
40 |
|
42 |
41 |
my $num_sent = 0;
|
43 |
|
my $parser;
|
44 |
42 |
|
45 |
43 |
my %mail_delivery_modules = (
|
46 |
44 |
sendmail => 'SL::Mailer::Sendmail',
|
... | ... | |
50 |
48 |
sub new {
|
51 |
49 |
my ($type, %params) = @_;
|
52 |
50 |
my $self = { %params };
|
53 |
|
$parser = MIME::Parser->new;
|
54 |
|
$parser->output_under("users");
|
55 |
51 |
|
56 |
52 |
bless $self, $type;
|
57 |
53 |
}
|
... | ... | |
133 |
129 |
my ($self, $attachment) = @_;
|
134 |
130 |
|
135 |
131 |
my %attributes = (
|
136 |
|
Disposition => 'attachment',
|
137 |
|
Encoding => 'base64',
|
|
132 |
disposition => 'attachment',
|
|
133 |
encoding => 'base64',
|
138 |
134 |
);
|
139 |
135 |
|
140 |
136 |
my $attachment_content;
|
... | ... | |
144 |
140 |
$::lxdebug->message(LXDebug->DEBUG2(), "mail5 att=" . $attachment . " email_journal=" . $email_journal . " id=" . $attachment->{id});
|
145 |
141 |
|
146 |
142 |
if (ref($attachment) eq "HASH") {
|
147 |
|
$attributes{Path} = $attachment->{path} || $attachment->{filename};
|
148 |
|
$attributes{Filename} = $attachment->{name};
|
149 |
|
$file_id = $attachment->{id} || '0';
|
150 |
|
$attributes{Type} = $attachment->{type} || 'application/pdf';
|
151 |
|
$attachment_content = eval { read_file($attachment->{path}) } if $email_journal > 1;
|
|
143 |
$attributes{Path} = $attachment->{path} || $attachment->{filename};
|
|
144 |
$attributes{filename} = $attachment->{name};
|
|
145 |
$file_id = $attachment->{id} || '0';
|
|
146 |
$attributes{content_type} = $attachment->{type} || 'application/pdf';
|
|
147 |
$attachment_content = $attachment->{content};
|
|
148 |
$attachment_content = eval { read_file($attachment->{path}) } if !$attachment_content;
|
152 |
149 |
|
153 |
150 |
} else {
|
154 |
151 |
# strip path
|
155 |
152 |
$attributes{Path} = $attachment;
|
156 |
|
$attributes{Filename} = $attachment;
|
157 |
|
$attributes{Filename} =~ s:.*\Q$self->{fileid}\E:: if $self->{fileid};
|
158 |
|
$attributes{Filename} =~ s:.*/::g;
|
159 |
|
|
160 |
|
my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? 'text' : 'application';
|
161 |
|
$attributes{Type} = File::MimeInfo::Magic::magic($attachment);
|
162 |
|
$attributes{Type} ||= "${application}/$self->{format}" if $self->{format};
|
163 |
|
$attributes{Type} ||= 'application/octet-stream';
|
164 |
|
$attachment_content = eval { read_file($attachment) } if $email_journal > 1;
|
|
153 |
$attributes{filename} = $attachment;
|
|
154 |
$attributes{filename} =~ s:.*\Q$self->{fileid}\E:: if $self->{fileid};
|
|
155 |
$attributes{filename} =~ s:.*/::g;
|
|
156 |
|
|
157 |
my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? 'text' : 'application';
|
|
158 |
$attributes{content_type} = File::MimeInfo::Magic::magic($attachment);
|
|
159 |
$attributes{content_type} ||= "${application}/$self->{format}" if $self->{format};
|
|
160 |
$attributes{content_type} ||= 'application/octet-stream';
|
|
161 |
$attachment_content = eval { read_file($attachment) };
|
165 |
162 |
}
|
166 |
163 |
|
167 |
164 |
return undef if $email_journal > 1 && !defined $attachment_content;
|
168 |
165 |
|
169 |
166 |
$attachment_content ||= ' ';
|
170 |
|
$attributes{Charset} = $self->{charset} if $self->{charset};
|
|
167 |
$attributes{charset} = $self->{charset} if $self->{charset};
|
171 |
168 |
|
172 |
169 |
$::lxdebug->message(LXDebug->DEBUG2(), "mail6 mtype=" . $attributes{Type} . " path=" . $attributes{Path} . " filename=" . $attributes{Filename});
|
173 |
170 |
|
174 |
171 |
my $ent;
|
175 |
|
if ( $attributes{Type} eq 'message/rfc822' ) {
|
176 |
|
my $fh = IO::File->new($attributes{Path}, "r") or return undef;
|
177 |
|
$ent = $parser->parse($fh);
|
178 |
|
|
179 |
|
$ent->head->replace('Content-disposition','attachment; filename='.$attributes{Filename});
|
180 |
|
|
|
172 |
if ( $attributes{content_type} eq 'message/rfc822' ) {
|
|
173 |
$ent = Email::MIME->new($attachment_content);
|
|
174 |
$ent->header_str_set('Content-disposition' => 'attachment; filename='.$attributes{filename});
|
181 |
175 |
} else {
|
182 |
|
$ent = MIME::Entity->build(%attributes);
|
|
176 |
$ent = Email::MIME->create(
|
|
177 |
attributes => \%attributes,
|
|
178 |
body => $attachment_content,
|
|
179 |
);
|
183 |
180 |
}
|
184 |
181 |
|
185 |
182 |
push @{ $self->{mail_attachments}} , SL::DB::EmailJournalAttachment->new(
|
186 |
|
name => $attributes{Filename},
|
187 |
|
mime_type => $attributes{Type},
|
|
183 |
name => $attributes{filename},
|
|
184 |
mime_type => $attributes{content_type},
|
188 |
185 |
content => ( $email_journal > 1 ? $attachment_content : ' '),
|
189 |
186 |
file_id => $file_id,
|
190 |
187 |
);
|
... | ... | |
195 |
192 |
sub _create_message {
|
196 |
193 |
my ($self) = @_;
|
197 |
194 |
|
198 |
|
push @{ $self->{headers} }, (Type => "multipart/mixed");
|
|
195 |
my @parts;
|
199 |
196 |
|
200 |
|
my $top = MIME::Entity->build(@{$self->{headers}});
|
|
197 |
push @{ $self->{headers} }, (Type => "multipart/mixed");
|
201 |
198 |
|
202 |
199 |
if ($self->{message}) {
|
203 |
|
$top->attach(
|
204 |
|
Data => encode($self->{charset},$self->{message}),
|
205 |
|
Charset => $self->{charset},
|
206 |
|
Type => $self->{contenttype},
|
207 |
|
Encoding => 'quoted-printable',
|
|
200 |
push @parts, Email::MIME->create(
|
|
201 |
attributes => {
|
|
202 |
content_type => $self->{contenttype},
|
|
203 |
charset => $self->{charset},
|
|
204 |
encoding => 'quoted-printable',
|
|
205 |
},
|
|
206 |
body_str => $self->{message},
|
|
207 |
);
|
|
208 |
|
|
209 |
push @{ $self->{headers} }, (
|
|
210 |
'Content-Type' => qq|$self->{contenttype}; charset="$self->{charset}"|,
|
208 |
211 |
);
|
209 |
212 |
}
|
210 |
213 |
|
211 |
|
$top->add_part($self->_create_attachment_part($_)) for @{ $self->{attachments} || [] };
|
|
214 |
push @parts, grep { $_ } map { $self->_create_attachment_part($_) } @{ $self->{attachments} || [] };
|
212 |
215 |
|
213 |
|
return $top;
|
|
216 |
return Email::MIME->create(
|
|
217 |
header_str => $self->{headers},
|
|
218 |
parts => \@parts,
|
|
219 |
);
|
214 |
220 |
}
|
215 |
221 |
|
216 |
222 |
sub send {
|
... | ... | |
257 |
263 |
$error = $@ if !$ok;
|
258 |
264 |
|
259 |
265 |
$self->{journalentry} = $self->_store_in_journal;
|
260 |
|
$parser->filer->purge;
|
261 |
266 |
|
262 |
267 |
return $ok ? '' : "send email: $error";
|
263 |
268 |
}
|
E-Mail versenden wieder mit Email::MIME
Einschränkung: *.eml werden zwar mitverschickt,
aber tauchen beim Empfänger nicht als *eml auf sondern als einzelne Anhänge