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 #======================================================================
45 $main::lxdebug->enter_sub();
50 $main::lxdebug->leave_sub();
56 $main::lxdebug->enter_sub();
58 my ($self, $text, $chars_left) = @_;
60 my $q_start = "=?$self->{charset}?Q?";
61 my $l_start = length($q_start);
63 my $new_text = "$q_start";
64 $chars_left -= $l_start if (defined $chars_left);
66 for (my $i = 0; $i < length($text); $i++) {
67 my $char = ord(substr($text, $i, 1));
69 if (($char < 32) || ($char > 127) || ($char == ord('?')) || ($char == ord('_'))) {
70 if ((defined $chars_left) && ($chars_left < 5)) {
71 $new_text .= "?=\n $q_start";
72 $chars_left = 75 - $l_start;
75 $new_text .= sprintf("=%02X", $char);
76 $chars_left -= 3 if (defined $chars_left);
79 $char = ord('_') if ($char == ord(' '));
80 if ((defined $chars_left) && ($chars_left < 5)) {
81 $new_text .= "?=\n $q_start";
82 $chars_left = 75 - $l_start;
85 $new_text .= chr($char);
86 $chars_left-- if (defined $chars_left);
92 $main::lxdebug->leave_sub();
98 $main::lxdebug->enter_sub();
105 my $boundary = time() . "-$$-${num_sent}";
106 $boundary = "LxOffice-$self->{version}-$boundary";
107 my $domain = $self->recode($self->{from});
108 $domain =~ s/(.*?\@|>)//g;
109 my $msgid = "$boundary\@$domain";
111 my $form = $main::form;
112 my $myconfig = \%main::myconfig;
114 my $email = $self->recode($myconfig->{email});
115 $email =~ s/[^\w\.\-\+=@]//ig;
117 my %temp_form = ( %{ $form }, 'myconfig_email' => $email );
118 my $template = SL::Template::create(type => 'PlainText', form => \%temp_form);
119 my $sendmail = $template->parse_block($::lx_office_conf{applications}->{sendmail});
121 if (!open(OUT, "|$sendmail")) {
122 $main::lxdebug->leave_sub();
123 return "$sendmail : $!";
126 $self->{charset} ||= Common::DEFAULT_CHARSET;
127 $self->{contenttype} ||= "text/plain";
129 foreach my $item (qw(to cc bcc)) {
130 next unless ($self->{$item});
131 $self->{$item} = $self->recode($self->{$item});
132 $self->{$item} =~ s/\</</g;
133 $self->{$item} =~ s/\$<\$/</g;
134 $self->{$item} =~ s/\>/>/g;
135 $self->{$item} =~ s/\$>\$/>/g;
138 $self->{from} = $self->recode($self->{from});
141 foreach my $item (qw(from to cc bcc)) {
142 next unless ($self->{$item});
143 my (@addr_objects) = Email::Address->parse($self->{$item});
144 next unless (scalar @addr_objects);
146 foreach my $addr_obj (@addr_objects) {
147 my $phrase = $addr_obj->phrase();
151 $addr_obj->phrase($self->mime_quote_text($phrase));
154 $headers .= sprintf("%s: %s\n", ucfirst($item), $addr_obj->format());
158 $headers .= sprintf("Subject: %s\n", $self->mime_quote_text($self->recode($self->{subject}), 60));
160 print OUT qq|${headers}Message-ID: <$msgid>
161 X-Mailer: Lx-Office $self->{version}
165 if ($self->{attachments}) {
166 print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
169 if ($self->{message}) {
170 print OUT qq|--${boundary}
171 Content-Type: $self->{contenttype}; charset="$self->{charset}"
173 | . $self->recode($self->{message}) . qq|
178 foreach my $attachment (@{ $self->{attachments} }) {
182 if (ref($attachment) eq "HASH") {
183 $filename = $attachment->{"name"};
184 $attachment = $attachment->{"filename"};
186 $filename = $attachment;
188 $filename =~ s/(.*\/|\Q$self->{fileid}\E)//g;
191 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
192 my $content_type = SL::MIME->mime_type_from_ext($filename);
193 $content_type = "${application}/$self->{format}" if (!$content_type && $self->{format});
194 $content_type ||= 'application/octet-stream';
196 open(IN, $attachment);
199 $main::lxdebug->leave_sub();
200 return "$attachment : $!";
203 # only set charset for attachements of type text. every other type should not have this field
204 # refer to bug 883 for detailed information
205 my $attachment_charset;
206 if (lc $application eq 'text' && $self->{charset}) {
207 $attachment_charset = qq|; charset="$self->{charset}" |;
210 print OUT qq|--${boundary}
211 Content-Type: ${content_type}; name="$filename"$attachment_charset
212 Content-Transfer-Encoding: BASE64
213 Content-Disposition: attachment; filename="$filename"\n\n|;
220 print OUT &encode_base64($msg);
225 print OUT qq|--${boundary}--\n|;
228 print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
230 | . $self->recode($self->{message}) . qq|
236 $main::lxdebug->leave_sub();
241 sub encode_base64 ($;$) {
242 $main::lxdebug->enter_sub();
244 # this code is from the MIME-Base64-2.12 package
245 # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
249 $eol = "\n" unless defined $eol;
250 pos($_[0]) = 0; # ensure start at the beginning
252 $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
254 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
255 # fix padding at the end
256 my $padding = (3 - length($_[0]) % 3) % 3;
257 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
259 # break encoded string into lines of no more than 60 characters each
261 $res =~ s/(.{1,60})/$1$eol/g;
264 $main::lxdebug->leave_sub();
273 return $::locale->is_utf8 ? Encode::encode('utf-8-strict', $text) : $text;