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