1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #======================================================================
27 use Email::MIME::Creator;
28 use File::MimeInfo::Magic;
30 use List::UtilsBy qw(bundle_by);
33 use SL::DB::EmailJournal;
34 use SL::DB::EmailJournalAttachment;
42 my %mail_delivery_modules = (
43 sendmail => 'SL::Mailer::Sendmail',
44 smtp => 'SL::Mailer::SMTP',
48 my ($type, %params) = @_;
49 my $self = { %params };
60 myconfig => \%::myconfig,
63 my $module = $mail_delivery_modules{ $::lx_office_conf{mail_delivery}->{method} };
64 eval "require $module" or return undef;
66 return $module->new(%params);
69 sub _cleanup_addresses {
72 foreach my $item (qw(to cc bcc)) {
73 next unless $self->{$item};
75 $self->{$item} =~ s/\</</g;
76 $self->{$item} =~ s/\$<\$/</g;
77 $self->{$item} =~ s/\>/>/g;
78 $self->{$item} =~ s/\$>\$/>/g;
82 sub _create_message_id {
86 my $domain = $self->{from};
90 return "kivitendo-$self->{version}-" . time() . "-${$}-${num_sent}\@$domain";
93 sub _create_address_headers {
96 # $self->{addresses} collects the recipients for use in e.g. the
97 # SMTP 'RCPT TO:' envelope command. $self->{headers} collects the
98 # headers that make up the actual email. 'BCC' should not be
99 # included there for certain transportation methods (SMTP).
101 $self->{addresses} = {};
103 foreach my $item (qw(from to cc bcc)) {
104 $self->{addresses}->{$item} = [];
105 next if !$self->{$item};
107 my @header_addresses;
109 foreach my $addr_obj (Email::Address->parse($self->{$item})) {
110 push @{ $self->{addresses}->{$item} }, $addr_obj->address;
111 next if $self->{driver}->keep_from_header($item);
113 my $phrase = $addr_obj->phrase();
117 $addr_obj->phrase($phrase);
120 push @header_addresses, $addr_obj->format;
123 push @{ $self->{headers} }, ( ucfirst($item) => join(', ', @header_addresses) ) if @header_addresses;
127 sub _create_attachment_part {
128 my ($self, $attachment) = @_;
131 disposition => 'attachment',
132 encoding => 'base64',
135 my $attachment_content;
137 my $email_journal = $::instance_conf->get_email_journal;
139 $::lxdebug->message(LXDebug->DEBUG2(), "mail5 att=" . $attachment . " email_journal=" . $email_journal . " id=" . $attachment->{id});
141 if (ref($attachment) eq "HASH") {
142 $attributes{Path} = $attachment->{path} || $attachment->{filename};
143 $attributes{filename} = $attachment->{name};
144 $file_id = $attachment->{id} || '0';
145 $attributes{content_type} = $attachment->{type} || 'application/pdf';
146 $attachment_content = $attachment->{content};
147 $attachment_content = eval { read_file($attachment->{path}) } if !$attachment_content;
151 $attributes{Path} = $attachment;
152 $attributes{filename} = $attachment;
153 $attributes{filename} =~ s:.*\Q$self->{fileid}\E:: if $self->{fileid};
154 $attributes{filename} =~ s:.*/::g;
156 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? 'text' : 'application';
157 $attributes{content_type} = File::MimeInfo::Magic::magic($attachment);
158 $attributes{content_type} ||= "${application}/$self->{format}" if $self->{format};
159 $attributes{content_type} ||= 'application/octet-stream';
160 $attachment_content = eval { read_file($attachment) };
163 return undef if $email_journal > 1 && !defined $attachment_content;
165 $attachment_content ||= ' ';
166 $attributes{charset} = $self->{charset} if $self->{charset};
168 $::lxdebug->message(LXDebug->DEBUG2(), "mail6 mtype=" . $attributes{Type} . " path=" . $attributes{Path} . " filename=" . $attributes{Filename});
171 if ( $attributes{content_type} eq 'message/rfc822' ) {
172 $ent = Email::MIME->new($attachment_content);
173 $ent->header_str_set('Content-disposition' => 'attachment; filename='.$attributes{filename});
175 $ent = Email::MIME->create(
176 attributes => \%attributes,
177 body => $attachment_content,
181 push @{ $self->{mail_attachments}} , SL::DB::EmailJournalAttachment->new(
182 name => $attributes{filename},
183 mime_type => $attributes{content_type},
184 content => ( $email_journal > 1 ? $attachment_content : ' '),
191 sub _create_message {
196 push @{ $self->{headers} }, (Type => "multipart/mixed");
198 if ($self->{message}) {
199 push @parts, Email::MIME->create(
201 content_type => $self->{contenttype},
202 charset => $self->{charset},
203 encoding => 'quoted-printable',
205 body_str => $self->{message},
208 push @{ $self->{headers} }, (
209 'Content-Type' => qq|$self->{contenttype}; charset="$self->{charset}"|,
213 push @parts, grep { $_ } map { $self->_create_attachment_part($_) } @{ $self->{attachments} || [] };
215 return Email::MIME->create(
216 header_str => $self->{headers},
224 # Create driver for delivery method (sendmail/SMTP)
225 $self->{driver} = eval { $self->_create_driver };
226 if (!$self->{driver}) {
227 $self->_store_in_journal('failed', 'driver could not be created; check your configuration');
228 return "send email : $@";
231 # Set defaults & headers
232 $self->{charset} = 'UTF-8';
233 $self->{contenttype} ||= "text/plain";
235 Subject => $self->{subject},
236 'Message-ID' => '<' . $self->_create_message_id . '>',
237 'X-Mailer' => "kivitendo $self->{version}",
239 $self->{mail_attachments} = [];
240 $self->{content_by_name} = $::instance_conf->get_email_journal == 1 && $::instance_conf->get_doc_files;
244 # Clean up To/Cc/Bcc address fields
245 $self->_cleanup_addresses;
246 $self->_create_address_headers;
248 my $email = $self->_create_message;
250 #$::lxdebug->message(0, "message: " . $email->as_string);
253 my $from_obj = (Email::Address->parse($self->{from}))[0];
255 $self->{driver}->start_mail(from => $from_obj->address, to => [ $self->_all_recipients ]);
256 $self->{driver}->print($email->as_string);
257 $self->{driver}->send;
264 $self->{journalentry} = $self->_store_in_journal;
266 return $ok ? '' : "send email: $error";
269 sub _all_recipients {
271 $self->{addresses} ||= {};
272 return map { @{ $self->{addresses}->{$_} || [] } } qw(to cc bcc);
275 sub _store_in_journal {
276 my ($self, $status, $extended_status) = @_;
278 my $journal_enable = $::instance_conf->get_email_journal;
280 return if $journal_enable == 0;
282 $status //= $self->{driver}->status if $self->{driver};
283 $status //= 'failed';
284 $extended_status //= $self->{driver}->extended_status if $self->{driver};
285 $extended_status //= 'unknown error';
287 my $headers = join "\r\n", (bundle_by { join(': ', @_) } 2, @{ $self->{headers} || [] });
289 my $jentry = SL::DB::EmailJournal->new(
290 sender => SL::DB::Manager::Employee->current,
291 from => $self->{from} // '',
292 recipients => join(', ', $self->_all_recipients),
293 subject => $self->{subject} // '',
295 body => $self->{message} // '',
296 sent_on => DateTime->now_local,
297 attachments => \@{ $self->{mail_attachments} },
299 extended_status => $extended_status,