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 #======================================================================
36 my ($self, $out) = @_;
39 $boundary = "SL-$self->{version}-$boundary";
40 my $domain = $self->{from};
41 $domain =~ s/(.*?\@|>)//g;
42 my $msgid = "$boundary\@$domain";
44 $self->{charset} = "ISO-8859-1" unless $self->{charset};
47 open(OUT, $out) or return "$out : $!";
49 open(OUT, ">-") or return "STDOUT : $!";
52 $self->{contenttype} = "text/plain" unless $self->{contenttype};
55 $cc = "Cc: $self->{cc}\n" if $self->{cc};
56 $bcc = "Bcc: $self->{bcc}\n" if $self->{bcc};
58 print OUT qq|From: $self->{from}
60 ${cc}${bcc}Subject: $self->{subject}
62 X-Mailer: SQL-Ledger $self->{version}
67 if ($self->{attachments}) {
68 print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
71 Content-Type: $self->{contenttype}; charset="$self->{charset}"
77 foreach my $attachment (@{ $self->{attachments} }) {
79 my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
81 open(IN, $attachment);
84 return "$attachment : $!";
87 my $filename = $attachment;
89 $filename =~ s/(.*\/|$self->{fileid})//g;
91 print OUT qq|--${boundary}
92 Content-Type: $application/$self->{format}; name="$filename"; charset="$self->{charset}"
93 Content-Transfer-Encoding: BASE64
94 Content-Disposition: attachment; filename="$filename"\n\n|;
100 print OUT &encode_base64($msg);
105 print OUT qq|--${boundary}--\n|;
108 print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
121 sub encode_base64 ($;$) {
123 # this code is from the MIME-Base64-2.12 package
124 # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
128 $eol = "\n" unless defined $eol;
129 pos($_[0]) = 0; # ensure start at the beginning
131 $res = join '', map( pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
133 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
134 # fix padding at the end
135 my $padding = (3 - length($_[0]) % 3) % 3;
136 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
137 # break encoded string into lines of no more than 60 characters each
139 $res =~ s/(.{1,60})/$1$eol/g;