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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #======================================================================
26 use Email::MIME::Creator;
38 my ($type, %params) = @_;
39 my $self = { %params };
50 myconfig => \%::myconfig,
53 my $module = ($::lx_office_conf{mail_delivery}->{method} || 'smtp') ne 'smtp' ? 'SL::Mailer::Sendmail' : 'SL::Mailer::SMTP';
54 eval "require $module" or return undef;
56 return $module->new(%params);
59 sub _cleanup_addresses {
62 foreach my $item (qw(to cc bcc)) {
63 next unless $self->{$item};
65 $self->{$item} =~ s/\</</g;
66 $self->{$item} =~ s/\$<\$/</g;
67 $self->{$item} =~ s/\>/>/g;
68 $self->{$item} =~ s/\$>\$/>/g;
72 sub _create_message_id {
76 my $domain = $self->{from};
80 return "kivitendo-$self->{version}-" . time() . "-${$}-${num_sent}\@$domain";
83 sub _create_address_headers {
86 # $self->{addresses} collects the recipients for use in e.g. the
87 # SMTP 'RCPT TO:' envelope command. $self->{headers} collects the
88 # headers that make up the actual email. 'BCC' should not be
89 # included there for certain transportation methods (SMTP).
91 $self->{addresses} = {};
93 foreach my $item (qw(from to cc bcc)) {
94 $self->{addresses}->{$item} = [];
95 next if !$self->{$item};
99 foreach my $addr_obj (Email::Address->parse($self->{$item})) {
100 push @{ $self->{addresses}->{$item} }, $addr_obj->address;
101 next if $self->{driver}->keep_from_header($item);
103 my $phrase = $addr_obj->phrase();
107 $addr_obj->phrase($phrase);
110 push @header_addresses, $addr_obj->format;
113 push @{ $self->{headers} }, ( ucfirst($item) => join(', ', @header_addresses) ) if @header_addresses;
117 sub _create_attachment_part {
118 my ($self, $attachment) = @_;
120 my $source_file_name;
123 disposition => 'attachment',
124 encoding => 'base64',
127 if (ref($attachment) eq "HASH") {
128 $attributes{filename} = $attachment->{name};
129 $source_file_name = $attachment->{filename};
133 $attributes{filename} = $attachment;
134 $attributes{filename} =~ s:.*\Q$self->{fileid}\E:: if $self->{fileid};
135 $attributes{filename} =~ s:.*/::g;
136 $source_file_name = $attachment;
139 my $attachment_content = eval { read_file($source_file_name) };
140 return undef if !defined $attachment_content;
142 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? 'text' : 'application';
143 $attributes{content_type} = SL::MIME->mime_type_from_ext($attributes{filename});
144 $attributes{content_type} ||= "${application}/$self->{format}" if $self->{format};
145 $attributes{content_type} ||= 'application/octet-stream';
146 $attributes{charset} = $self->{charset} if lc $application eq 'text' && $self->{charset};
148 return Email::MIME->create(
149 attributes => \%attributes,
150 body => $attachment_content,
154 sub _create_message {
159 if ($self->{message}) {
160 push @parts, Email::MIME->create(
162 content_type => $self->{contenttype},
163 charset => $self->{charset},
164 encoding => 'quoted-printable',
166 body_str => $self->{message},
169 push @{ $self->{headers} }, (
170 'Content-Type' => qq|$self->{contenttype}; charset="$self->{charset}"|,
174 push @parts, grep { $_ } map { $self->_create_attachment_part($_) } @{ $self->{attachments} || [] };
176 return Email::MIME->create(
177 header_str => $self->{headers},
185 # Create driver for delivery method (sendmail/SMTP)
186 $self->{driver} = eval { $self->_create_driver };
187 if (!$self->{driver}) {
188 $::lxdebug->leave_sub();
189 return "send email : $@";
192 # Set defaults & headers
193 $self->{charset} = 'UTF-8';
194 $self->{contenttype} ||= "text/plain";
196 Subject => $self->{subject},
197 'Message-ID' => '<' . $self->_create_message_id . '>',
198 'X-Mailer' => "kivitendo $self->{version}",
201 # Clean up To/Cc/Bcc address fields
202 $self->_cleanup_addresses;
203 $self->_create_address_headers;
205 my $email = $self->_create_message;
207 # $::lxdebug->message(0, "message: " . $email->as_string);
210 $self->{driver}->start_mail(from => $self->{from}, to => [ map { @{ $self->{addresses}->{$_} } } qw(to cc bcc) ]);
211 $self->{driver}->print($email->as_string);
212 $self->{driver}->send;