1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
46 $main::lxdebug->enter_sub();
48 my ($type, %params) = @_;
49 my $self = { %params };
51 $main::lxdebug->leave_sub();
62 myconfig => \%::myconfig,
65 my $cfg = $::lx_office_conf{mail_delivery};
66 if (($cfg->{method} || 'smtp') ne 'smtp') {
67 require SL::Mailer::Sendmail;
68 return SL::Mailer::Sendmail->new(%params);
70 require SL::Mailer::SMTP;
71 return SL::Mailer::SMTP->new(%params);
76 $main::lxdebug->enter_sub();
78 my ($self, $text, $chars_left) = @_;
80 my $q_start = "=?$self->{charset}?Q?";
81 my $l_start = length($q_start);
83 my $new_text = "$q_start";
84 $chars_left -= $l_start if (defined $chars_left);
86 for (my $i = 0; $i < length($text); $i++) {
87 my $char = ord(substr($text, $i, 1));
89 if (($char < 32) || ($char > 127) || ($char == ord('?')) || ($char == ord('_'))) {
90 if ((defined $chars_left) && ($chars_left < 5)) {
91 $new_text .= "?=\n $q_start";
92 $chars_left = 75 - $l_start;
95 $new_text .= sprintf("=%02X", $char);
96 $chars_left -= 3 if (defined $chars_left);
99 $char = ord('_') if ($char == ord(' '));
100 if ((defined $chars_left) && ($chars_left < 5)) {
101 $new_text .= "?=\n $q_start";
102 $chars_left = 75 - $l_start;
105 $new_text .= chr($char);
106 $chars_left-- if (defined $chars_left);
112 $main::lxdebug->leave_sub();
118 $main::lxdebug->enter_sub();
125 my $boundary = time() . "-$$-${num_sent}";
126 $boundary = "kivitendo-$self->{version}-$boundary";
127 my $domain = $self->recode($self->{from});
128 $domain =~ s/(.*?\@|>)//g;
129 my $msgid = "$boundary\@$domain";
131 my $form = $main::form;
132 my $myconfig = \%main::myconfig;
134 my $driver = eval { $self->_create_driver };
136 $main::lxdebug->leave_sub();
137 return "send email : $@";
140 $self->{charset} ||= Common::DEFAULT_CHARSET;
141 $self->{contenttype} ||= "text/plain";
143 foreach my $item (qw(to cc bcc)) {
144 next unless ($self->{$item});
145 $self->{$item} = $self->recode($self->{$item});
146 $self->{$item} =~ s/\</</g;
147 $self->{$item} =~ s/\$<\$/</g;
148 $self->{$item} =~ s/\>/>/g;
149 $self->{$item} =~ s/\$>\$/>/g;
152 $self->{from} = $self->recode($self->{from});
156 foreach my $item (qw(from to cc bcc)) {
157 $addresses{$item} = [];
158 next unless ($self->{$item});
160 my (@addr_objects) = Email::Address->parse($self->{$item});
161 next unless (scalar @addr_objects);
163 foreach my $addr_obj (@addr_objects) {
164 push @{ $addresses{$item} }, $addr_obj->address;
165 my $phrase = $addr_obj->phrase();
169 $addr_obj->phrase($self->mime_quote_text($phrase));
172 $headers .= sprintf("%s: %s\n", ucfirst($item), $addr_obj->format()) unless $driver->keep_from_header($item);
176 $headers .= sprintf("Subject: %s\n", $self->mime_quote_text($self->recode($self->{subject}), 60));
178 $driver->start_mail(from => $self->{from}, to => [ map { @{ $addresses{$_} } } qw(to cc bcc) ]);
180 $driver->print(qq|${headers}Message-ID: <$msgid>
181 X-Mailer: kivitendo $self->{version}
185 if ($self->{attachments}) {
186 $driver->print(qq|Content-Type: multipart/mixed; boundary="$boundary"\n\n|);
187 if ($self->{message}) {
188 $driver->print(qq|--${boundary}
189 Content-Type: $self->{contenttype}; charset="$self->{charset}"
191 | . $self->recode($self->{message}) . qq|
196 foreach my $attachment (@{ $self->{attachments} }) {
200 if (ref($attachment) eq "HASH") {
201 $filename = $attachment->{"name"};
202 $attachment = $attachment->{"filename"};
204 $filename = $attachment;
206 $filename =~ s/(.*\/|\Q$self->{fileid}\E)//g;
209 my $attachment_content = eval { read_file($attachment) };
210 if (!defined $attachment_content) {
211 $main::lxdebug->leave_sub();
212 return "$attachment : $!";
215 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
216 my $content_type = SL::MIME->mime_type_from_ext($filename);
217 $content_type = "${application}/$self->{format}" if (!$content_type && $self->{format});
218 $content_type ||= 'application/octet-stream';
220 # only set charset for attachements of type text. every other type should not have this field
221 # refer to bug 883 for detailed information
222 my $attachment_charset;
223 if (lc $application eq 'text' && $self->{charset}) {
224 $attachment_charset = qq|; charset="$self->{charset}" |;
227 $driver->print(qq|--${boundary}
228 Content-Type: ${content_type}; name="$filename"$attachment_charset
229 Content-Transfer-Encoding: BASE64
230 Content-Disposition: attachment; filename="$filename"\n\n|);
232 $driver->print(encode_base64($attachment_content));
234 $driver->print(qq|--${boundary}--\n|);
237 $driver->print(qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
239 | . $self->recode($self->{message}) . qq|
245 $main::lxdebug->leave_sub();
250 sub encode_base64 ($;$) {
251 $main::lxdebug->enter_sub();
253 # this code is from the MIME-Base64-2.12 package
254 # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
258 $eol = "\n" unless defined $eol;
259 pos($_[0]) = 0; # ensure start at the beginning
261 $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
263 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
264 # fix padding at the end
265 my $padding = (3 - length($_[0]) % 3) % 3;
266 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
268 # break encoded string into lines of no more than 60 characters each
270 $res =~ s/(.{1,60})/$1$eol/g;
273 $main::lxdebug->leave_sub();
282 return $::locale->is_utf8 ? Encode::encode('utf-8-strict', $text) : $text;