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();
 
  47   my ($type, %params) = @_;
 
  48   my $self = { %params };
 
  50   $main::lxdebug->leave_sub();
 
  61     myconfig => \%::myconfig,
 
  64   my $cfg = $::lx_office_conf{mail_delivery};
 
  65   if (($cfg->{method} || 'smtp') ne 'smtp') {
 
  66     require SL::Mailer::Sendmail;
 
  67     return SL::Mailer::Sendmail->new(%params);
 
  69     require SL::Mailer::SMTP;
 
  70     return SL::Mailer::SMTP->new(%params);
 
  75   $main::lxdebug->enter_sub();
 
  77   my ($self, $text, $chars_left) = @_;
 
  79   my $q_start = "=?$self->{charset}?Q?";
 
  80   my $l_start = length($q_start);
 
  82   my $new_text = "$q_start";
 
  83   $chars_left -= $l_start if (defined $chars_left);
 
  85   for (my $i = 0; $i < length($text); $i++) {
 
  86     my $char = ord(substr($text, $i, 1));
 
  88     if (($char < 32) || ($char > 127) || ($char == ord('?')) || ($char == ord('_'))) {
 
  89       if ((defined $chars_left) && ($chars_left < 5)) {
 
  90         $new_text .= "?=\n $q_start";
 
  91         $chars_left = 75 - $l_start;
 
  94       $new_text .= sprintf("=%02X", $char);
 
  95       $chars_left -= 3 if (defined $chars_left);
 
  98       $char = ord('_') if ($char == ord(' '));
 
  99       if ((defined $chars_left) && ($chars_left < 5)) {
 
 100         $new_text .= "?=\n $q_start";
 
 101         $chars_left = 75 - $l_start;
 
 104       $new_text .= chr($char);
 
 105       $chars_left-- if (defined $chars_left);
 
 111   $main::lxdebug->leave_sub();
 
 117   $main::lxdebug->enter_sub();
 
 124   my $boundary    = time() . "-$$-${num_sent}";
 
 125   $boundary       =  "LxOffice-$self->{version}-$boundary";
 
 126   my $domain      =  $self->recode($self->{from});
 
 127   $domain         =~ s/(.*?\@|>)//g;
 
 128   my $msgid       =  "$boundary\@$domain";
 
 130   my $form        =  $main::form;
 
 131   my $myconfig    =  \%main::myconfig;
 
 133   my $driver = eval { $self->_create_driver };
 
 135     $main::lxdebug->leave_sub();
 
 136     return "send email : $@";
 
 139   $self->{charset}     ||= Common::DEFAULT_CHARSET;
 
 140   $self->{contenttype} ||= "text/plain";
 
 142   foreach my $item (qw(to cc bcc)) {
 
 143     next unless ($self->{$item});
 
 144     $self->{$item} =  $self->recode($self->{$item});
 
 145     $self->{$item} =~ s/\</</g;
 
 146     $self->{$item} =~ s/\$<\$/</g;
 
 147     $self->{$item} =~ s/\>/>/g;
 
 148     $self->{$item} =~ s/\$>\$/>/g;
 
 151   $self->{from} = $self->recode($self->{from});
 
 155   foreach my $item (qw(from to cc bcc)) {
 
 156     $addresses{$item} = [];
 
 157     next unless ($self->{$item});
 
 159     my (@addr_objects) = Email::Address->parse($self->{$item});
 
 160     next unless (scalar @addr_objects);
 
 162     foreach my $addr_obj (@addr_objects) {
 
 163       push @{ $addresses{$item} }, $addr_obj->address;
 
 164       my $phrase = $addr_obj->phrase();
 
 168         $addr_obj->phrase($self->mime_quote_text($phrase));
 
 171       $headers .= sprintf("%s: %s\n", ucfirst($item), $addr_obj->format()) unless $driver->keep_from_header($item);
 
 175   $headers .= sprintf("Subject: %s\n", $self->mime_quote_text($self->recode($self->{subject}), 60));
 
 177   $driver->start_mail(from => $self->{from}, to => [ map { @{ $addresses{$_} } } qw(to cc bcc) ]);
 
 179   $driver->print(qq|${headers}Message-ID: <$msgid>
 
 180 X-Mailer: Lx-Office $self->{version}
 
 184   if ($self->{attachments}) {
 
 185     $driver->print(qq|Content-Type: multipart/mixed; boundary="$boundary"\n\n|);
 
 186     if ($self->{message}) {
 
 187       $driver->print(qq|--${boundary}
 
 188 Content-Type: $self->{contenttype}; charset="$self->{charset}"
 
 190 | . $self->recode($self->{message}) . qq|
 
 195     foreach my $attachment (@{ $self->{attachments} }) {
 
 199       if (ref($attachment) eq "HASH") {
 
 200         $filename = $attachment->{"name"};
 
 201         $attachment = $attachment->{"filename"};
 
 203         $filename = $attachment;
 
 205         $filename =~ s/(.*\/|\Q$self->{fileid}\E)//g;
 
 208       my $application    = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
 
 209       my $content_type   = SL::MIME->mime_type_from_ext($filename);
 
 210       $content_type      = "${application}/$self->{format}" if (!$content_type && $self->{format});
 
 211       $content_type    ||= 'application/octet-stream';
 
 213       open(IN, $attachment);
 
 215         $main::lxdebug->leave_sub();
 
 216         return "$attachment : $!";
 
 219       # only set charset for attachements of type text. every other type should not have this field
 
 220       # refer to bug 883 for detailed information
 
 221       my $attachment_charset;
 
 222       if (lc $application eq 'text' && $self->{charset}) {
 
 223         $attachment_charset = qq|; charset="$self->{charset}" |;
 
 226       $driver->print(qq|--${boundary}
 
 227 Content-Type: ${content_type}; name="$filename"$attachment_charset
 
 228 Content-Transfer-Encoding: BASE64
 
 229 Content-Disposition: attachment; filename="$filename"\n\n|);
 
 236       $driver->print(encode_base64($msg));
 
 241     $driver->print(qq|--${boundary}--\n|);
 
 244     $driver->print(qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
 
 246 | . $self->recode($self->{message}) . qq|
 
 252   $main::lxdebug->leave_sub();
 
 257 sub encode_base64 ($;$) {
 
 258   $main::lxdebug->enter_sub();
 
 260   # this code is from the MIME-Base64-2.12 package
 
 261   # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
 
 265   $eol = "\n" unless defined $eol;
 
 266   pos($_[0]) = 0;    # ensure start at the beginning
 
 268   $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
 
 270   $res =~ tr|` -_|AA-Za-z0-9+/|;    # `# help emacs
 
 271                                     # fix padding at the end
 
 272   my $padding = (3 - length($_[0]) % 3) % 3;
 
 273   $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
 
 275   # break encoded string into lines of no more than 60 characters each
 
 277     $res =~ s/(.{1,60})/$1$eol/g;
 
 280   $main::lxdebug->leave_sub();
 
 289   return $::locale->is_utf8 ? Encode::encode('utf-8-strict', $text) : $text;