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->{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} =~ s/\</</g;
146 $self->{$item} =~ s/\$<\$/</g;
147 $self->{$item} =~ s/\>/>/g;
148 $self->{$item} =~ s/\$>\$/>/g;
153 foreach my $item (qw(from to cc bcc)) {
154 $addresses{$item} = [];
155 next unless ($self->{$item});
157 my (@addr_objects) = Email::Address->parse($self->{$item});
158 next unless (scalar @addr_objects);
160 foreach my $addr_obj (@addr_objects) {
161 push @{ $addresses{$item} }, $addr_obj->address;
162 my $phrase = $addr_obj->phrase();
166 $addr_obj->phrase($self->mime_quote_text($phrase));
169 $headers .= sprintf("%s: %s\n", ucfirst($item), $addr_obj->format()) unless $driver->keep_from_header($item);
173 $headers .= sprintf("Subject: %s\n", $self->mime_quote_text($self->{subject}, 60));
175 $driver->start_mail(from => $self->{from}, to => [ map { @{ $addresses{$_} } } qw(to cc bcc) ]);
177 $driver->print(qq|${headers}Message-ID: <$msgid>
178 X-Mailer: kivitendo $self->{version}
182 if ($self->{attachments}) {
183 $driver->print(qq|Content-Type: multipart/mixed; boundary="$boundary"\n\n|);
184 if ($self->{message}) {
185 $driver->print(qq|--${boundary}
186 Content-Type: $self->{contenttype}; charset="$self->{charset}"
193 foreach my $attachment (@{ $self->{attachments} }) {
197 if (ref($attachment) eq "HASH") {
198 $filename = $attachment->{"name"};
199 $attachment = $attachment->{"filename"};
201 $filename = $attachment;
203 $filename =~ s/(.*\/|\Q$self->{fileid}\E)//g;
206 my $attachment_content = eval { read_file($attachment) };
207 if (!defined $attachment_content) {
208 $main::lxdebug->leave_sub();
209 return "$attachment : $!";
212 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
213 my $content_type = SL::MIME->mime_type_from_ext($filename);
214 $content_type = "${application}/$self->{format}" if (!$content_type && $self->{format});
215 $content_type ||= 'application/octet-stream';
217 # only set charset for attachements of type text. every other type should not have this field
218 # refer to bug 883 for detailed information
219 my $attachment_charset;
220 if (lc $application eq 'text' && $self->{charset}) {
221 $attachment_charset = qq|; charset="$self->{charset}" |;
224 $driver->print(qq|--${boundary}
225 Content-Type: ${content_type}; name="$filename"$attachment_charset
226 Content-Transfer-Encoding: BASE64
227 Content-Disposition: attachment; filename="$filename"\n\n|);
229 $driver->print(encode_base64($attachment_content));
231 $driver->print(qq|--${boundary}--\n|);
234 $driver->print(qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
242 $main::lxdebug->leave_sub();
247 sub encode_base64 ($;$) {
248 $main::lxdebug->enter_sub();
250 # this code is from the MIME-Base64-2.12 package
251 # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
255 $eol = "\n" unless defined $eol;
256 pos($_[0]) = 0; # ensure start at the beginning
258 $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
260 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
261 # fix padding at the end
262 my $padding = (3 - length($_[0]) % 3) % 3;
263 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
265 # break encoded string into lines of no more than 60 characters each
267 $res =~ s/(.{1,60})/$1$eol/g;
270 $main::lxdebug->leave_sub();