1 #=====================================================================
2 # SQL-Ledger Accounting
5 # Author: Dieter Simader
6 # Email: dsimader@sql-ledger.org
7 # Web: http://www.sql-ledger.org
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
27 #======================================================================
40 my ($self, $out) = @_;
43 $boundary = "SL-$self->{version}-$boundary";
44 my $domain = $self->{from};
45 $domain =~ s/(.*?\@|>)//g;
46 my $msgid = "$boundary\@$domain";
48 $self->{charset} = "ISO-8859-1" unless $self->{charset};
51 open(OUT, $out) or return "$out : $!";
53 open(OUT, ">-") or return "STDOUT : $!";
56 $self->{contenttype} = "text/plain" unless $self->{contenttype};
59 $cc = "Cc: $self->{cc}\n" if $self->{cc};
60 $bcc = "Bcc: $self->{bcc}\n" if $self->{bcc};
62 foreach my $item (qw(from to cc bcc)) {
63 $self->{$item} =~ s/\\_/_/g;
64 $self->{$item} =~ s/\</</g;
65 $self->{$item} =~ s/\$<\$/</g;
66 $self->{$item} =~ s/\>/>/g;
67 $self->{$item} =~ s/\$>\$/>/g;
70 print OUT qq|From: $self->{from}
72 ${cc}${bcc}Subject: $self->{subject}
74 X-Mailer: SQL-Ledger $self->{version}
79 if ($self->{attachments}) {
80 print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
83 if ($self->{message}) {
84 print OUT qq|--${boundary}
85 Content-Type: $self->{contenttype}; charset="$self->{charset}"
92 foreach my $attachment (@{ $self->{attachments} }) {
94 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
96 open(IN, $attachment);
99 return "$attachment : $!";
102 my $filename = $attachment;
104 $filename =~ s/(.*\/|$self->{fileid})//g;
106 print OUT qq|--${boundary}
107 Content-Type: $application/$self->{format}; name="$filename"; charset="$self->{charset}"
108 Content-Transfer-Encoding: BASE64
109 Content-Disposition: attachment; filename="$filename"\n\n|;
115 print OUT &encode_base64($msg);
120 print OUT qq|--${boundary}--\n|;
123 print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
136 sub encode_base64 ($;$) {
138 # this code is from the MIME-Base64-2.12 package
139 # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
143 $eol = "\n" unless defined $eol;
144 pos($_[0]) = 0; # ensure start at the beginning
146 $res = join '', map( pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
148 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
149 # fix padding at the end
150 my $padding = (3 - length($_[0]) % 3) % 3;
151 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
152 # break encoded string into lines of no more than 60 characters each
154 $res =~ s/(.{1,60})/$1$eol/g;