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} = {};
88 foreach my $item (qw(from to cc bcc)) {
89 $self->{addresses}->{$item} = [];
90 next if !$self->{$item} || $self->{driver}->keep_from_header($item);
94 foreach my $addr_obj (Email::Address->parse($self->{$item})) {
95 push @{ $self->{addresses}->{$item} }, $addr_obj->address;
96 my $phrase = $addr_obj->phrase();
100 $addr_obj->phrase($phrase);
103 push @header_addresses, $addr_obj->format;
106 push @{ $self->{headers} }, ( ucfirst($item) => join(', ', @header_addresses) ) if @header_addresses;
110 sub _create_attachment_part {
111 my ($self, $attachment) = @_;
113 my $source_file_name;
116 disposition => 'attachment',
117 encoding => 'base64',
120 if (ref($attachment) eq "HASH") {
121 $attributes{filename} = $attachment->{name};
122 $source_file_name = $attachment->{filename};
126 $attributes{filename} = $attachment;
127 $attributes{filename} =~ s:.*\Q$self->{fileid}\E:: if $self->{fileid};
128 $attributes{filename} =~ s:.*/::g;
129 $source_file_name = $attachment;
132 my $attachment_content = eval { read_file($source_file_name) };
133 return undef if !defined $attachment_content;
135 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? 'text' : 'application';
136 $attributes{content_type} = SL::MIME->mime_type_from_ext($attributes{filename});
137 $attributes{content_type} ||= "${application}/$self->{format}" if $self->{format};
138 $attributes{content_type} ||= 'application/octet-stream';
139 $attributes{charset} = $self->{charset} if lc $application eq 'text' && $self->{charset};
141 return Email::MIME->create(
142 attributes => \%attributes,
143 body => $attachment_content,
147 sub _create_message {
152 if ($self->{message}) {
153 push @parts, Email::MIME->create(
155 content_type => $self->{contenttype},
156 charset => $self->{charset},
157 encoding => 'quoted-printable',
159 body_str => $self->{message},
162 push @{ $self->{headers} }, (
163 'Content-Type' => qq|$self->{contenttype}; charset="$self->{charset}"|,
167 push @parts, grep { $_ } map { $self->_create_attachment_part($_) } @{ $self->{attachments} || [] };
169 return Email::MIME->create(
170 header_str => $self->{headers},
178 # Create driver for delivery method (sendmail/SMTP)
179 $self->{driver} = eval { $self->_create_driver };
180 if (!$self->{driver}) {
181 $::lxdebug->leave_sub();
182 return "send email : $@";
185 # Set defaults & headers
186 $self->{charset} ||= Common::DEFAULT_CHARSET;
187 $self->{contenttype} ||= "text/plain";
189 Subject => $self->{subject},
190 'Message-ID' => $self->_create_message_id,
191 'X-Mailer' => "kivitendo $self->{version}",
194 # Clean up To/Cc/Bcc address fields
195 $self->_cleanup_addresses;
196 $self->_create_address_headers;
198 my $email = $self->_create_message;
200 # $::lxdebug->message(0, "message: " . $email->as_string);
203 $self->{driver}->start_mail(from => $self->{from}, to => [ map { @{ $self->{addresses}->{$_} } } qw(to cc bcc) ]);
204 $self->{driver}->print($email->as_string);
205 $self->{driver}->send;