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 #======================================================================
 
  36   $main::lxdebug->enter_sub();
 
  41   $main::lxdebug->leave_sub();
 
  47   $main::lxdebug->enter_sub();
 
  49   my ($self, $text, $chars_left) = @_;
 
  51   my $q_start = "=?$self->{charset}?Q?";
 
  52   my $l_start = length($q_start);
 
  54   my $new_text = "$q_start";
 
  55   $chars_left -= $l_start;
 
  57   for (my $i = 0; $i < length($text); $i++) {
 
  58     my $char = ord(substr($text, $i, 1));
 
  60     if (($char < 32) || ($char > 127) ||
 
  61         ($char == ord('?')) || ($char == ord('_'))) {
 
  62       if ($chars_left < 5) {
 
  63         $new_text .= "?=\n $q_start";
 
  64         $chars_left = 75 - $l_start;
 
  67       $new_text .= sprintf("=%02X", $char);
 
  71       $char = ord('_') if ($char == ord(' '));
 
  72       if ($chars_left < 5) {
 
  73         $new_text .= "?=\n $q_start";
 
  74         $chars_left = 75 - $l_start;
 
  77       $new_text .= chr($char);
 
  84   $main::lxdebug->leave_sub();
 
  90   $main::lxdebug->enter_sub();
 
  97   $boundary = "LxOffice-$self->{version}-$boundary";
 
  98   my $domain = $self->{from};
 
  99   $domain =~ s/(.*?\@|>)//g;
 
 100   my $msgid = "$boundary\@$domain";
 
 102   $self->{charset} = Common::DEFAULT_CHARSET unless $self->{charset};
 
 104   if (!open(OUT, $main::sendmail)) {
 
 105     $main::lxdebug->leave_sub();
 
 106     return "$main::sendmail : $!";
 
 109   $self->{contenttype} = "text/plain" unless $self->{contenttype};
 
 112   $cc  = "Cc: $self->{cc}\n"   if $self->{cc};
 
 113   $bcc = "Bcc: $self->{bcc}\n" if $self->{bcc};
 
 115   foreach my $item (qw(to cc bcc)) {
 
 116     $self->{$item} =~ s/\</</g;
 
 117     $self->{$item} =~ s/\$<\$/</g;
 
 118     $self->{$item} =~ s/\>/>/g;
 
 119     $self->{$item} =~ s/\$>\$/>/g;
 
 122   my $subject = $self->mime_quote_text($self->{subject}, 60);
 
 124   print OUT qq|From: $self->{from}
 
 126 ${cc}${bcc}Subject: $subject
 
 128 X-Mailer: Lx-Office $self->{version}
 
 132   if ($self->{attachments}) {
 
 133     print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
 
 136     if ($self->{message}) {
 
 137       print OUT qq|--${boundary}
 
 138 Content-Type: $self->{contenttype}; charset="$self->{charset}"
 
 145     foreach my $attachment (@{ $self->{attachments} }) {
 
 149       if (ref($attachment) eq "HASH") {
 
 150         $filename = $attachment->{"name"};
 
 151         $attachment = $attachment->{"filename"};
 
 153         $filename = $attachment;
 
 155         $filename =~ s/(.*\/|\Q$self->{fileid}\E)//g;
 
 159         ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/)
 
 163       open(IN, $attachment);
 
 166         $main::lxdebug->leave_sub();
 
 167         return "$attachment : $!";
 
 170       print OUT qq|--${boundary}
 
 171 Content-Type: $application/$self->{format}; name="$filename"; charset="$self->{charset}"
 
 172 Content-Transfer-Encoding: BASE64
 
 173 Content-Disposition: attachment; filename="$filename"\n\n|;
 
 180       print OUT &encode_base64($msg);
 
 185     print OUT qq|--${boundary}--\n|;
 
 188     print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
 
 196   $main::lxdebug->leave_sub();
 
 201 sub encode_base64 ($;$) {
 
 202   $main::lxdebug->enter_sub();
 
 204   # this code is from the MIME-Base64-2.12 package
 
 205   # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
 
 209   $eol = "\n" unless defined $eol;
 
 210   pos($_[0]) = 0;    # ensure start at the beginning
 
 212   $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
 
 214   $res =~ tr|` -_|AA-Za-z0-9+/|;    # `# help emacs
 
 215                                     # fix padding at the end
 
 216   my $padding = (3 - length($_[0]) % 3) % 3;
 
 217   $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
 
 219   # break encoded string into lines of no more than 60 characters each
 
 221     $res =~ s/(.{1,60})/$1$eol/g;
 
 224   $main::lxdebug->leave_sub();