better error message for missing tables
[freeside.git] / sql-ledger / SL / Mailer.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2001
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 # Contributors: 
10 #
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.
15 #
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 #======================================================================
24
25 package Mailer;
26
27 sub new {
28   my ($type) = @_;
29   my $self = {};
30
31   bless $self, $type;
32 }
33
34
35 sub send {
36   my ($self, $out) = @_;
37
38   my $boundary = time;
39   $boundary = "SL-$self->{version}-$boundary";
40   my $domain = $self->{from};
41   $domain =~ s/(.*?\@|>)//g;
42   my $msgid = "$boundary\@$domain";
43   
44   $self->{charset} = "ISO-8859-1" unless $self->{charset};
45   
46   if ($out) {
47     open(OUT, $out) or return "$out : $!";
48   } else {
49     open(OUT, ">-") or return "STDOUT : $!";
50   }
51
52   $self->{contenttype} = "text/plain" unless $self->{contenttype};
53   
54   my ($cc, $bcc);
55   $cc = "Cc: $self->{cc}\n" if $self->{cc};
56   $bcc = "Bcc: $self->{bcc}\n" if $self->{bcc};
57   
58   print OUT qq|From: $self->{from}
59 To: $self->{to}
60 ${cc}${bcc}Subject: $self->{subject}
61 Message-ID: <$msgid>
62 X-Mailer: SQL-Ledger $self->{version}
63 MIME-Version: 1.0
64 |;
65
66
67   if ($self->{attachments}) {
68     print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
69
70 --${boundary}
71 Content-Type: $self->{contenttype}; charset="$self->{charset}"
72
73 $self->{message}
74
75 |;
76
77     foreach my $attachment (@{ $self->{attachments} }) {
78       
79       my $application = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
80       
81       open(IN, $attachment);
82       if ($?) {
83         close(OUT);
84         return "$attachment : $!";
85       }
86       
87       my $filename = $attachment;
88       # strip path
89       $filename =~ s/(.*\/|$self->{fileid})//g;
90       
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|;
95
96       my $msg = "";
97       while (<IN>) {;
98         $msg .= $_;
99       }
100       print OUT &encode_base64($msg);
101
102       close(IN);
103       
104     }
105     print OUT qq|--${boundary}--\n|;
106
107   } else {
108     print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
109
110 $self->{message}
111 |;
112   }
113
114   close(OUT);
115
116   return "";
117   
118 }
119
120
121 sub encode_base64 ($;$) {
122
123   # this code is from the MIME-Base64-2.12 package
124   # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
125
126   my $res = "";
127   my $eol = $_[1];
128   $eol = "\n" unless defined $eol;
129   pos($_[0]) = 0;                          # ensure start at the beginning
130
131   $res = join '', map( pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
132
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
138   if (length $eol) {
139     $res =~ s/(.{1,60})/$1$eol/g;
140   }
141   return $res;
142   
143 }
144
145
146 1;
147