Änderungen zur Unterstützung von anderen Zeichensätzen als ISO-8859-1(5) implementier...
[kivitendo-erp.git] / SL / Mailer.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 # Contributors:
16 #
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.
21 #
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 #======================================================================
30
31 package Mailer;
32
33 use SL::Common;
34
35 sub new {
36   $main::lxdebug->enter_sub();
37
38   my ($type) = @_;
39   my $self = {};
40
41   $main::lxdebug->leave_sub();
42
43   bless $self, $type;
44 }
45
46 sub mime_quote_text {
47   $main::lxdebug->enter_sub();
48
49   my ($self, $text, $chars_left) = @_;
50
51   my $q_start = "=?$self->{charset}?Q?";
52   my $l_start = length($q_start);
53
54   my $new_text = "$q_start";
55   $chars_left -= $l_start;
56
57   for (my $i = 0; $i < length($text); $i++) {
58     my $char = ord(substr($text, $i, 1));
59
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;
65       }
66
67       $new_text .= sprintf("=%02X", $char);
68       $chars_left -= 3;
69
70     } else {
71       $char = ord('_') if ($char == ord(' '));
72       if ($chars_left < 5) {
73         $new_text .= "?=\n $q_start";
74         $chars_left = 75 - $l_start;
75       }
76
77       $new_text .= chr($char);
78       $chars_left--;
79     }
80   }
81
82   $new_text .= "?=";
83
84   $main::lxdebug->leave_sub();
85
86   return $new_text;
87 }
88
89 sub send {
90   $main::lxdebug->enter_sub();
91
92   my ($self, $out) = @_;
93
94   my $boundary = time;
95   $boundary = "LxOffice-$self->{version}-$boundary";
96   my $domain = $self->{from};
97   $domain =~ s/(.*?\@|>)//g;
98   my $msgid = "$boundary\@$domain";
99
100   $self->{charset} = Common::DEFAULT_CHARSET unless $self->{charset};
101
102   if ($out) {
103     if (!open(OUT, $out)) {
104       $main::lxdebug->leave_sub();
105       return "$out : $!";
106     }
107   } else {
108     if (!open(OUT, ">-")) {
109       $main::lxdebug->leave_sub();
110       return "STDOUT : $!";
111     }
112   }
113
114   $self->{contenttype} = "text/plain" unless $self->{contenttype};
115
116   my ($cc, $bcc);
117   $cc  = "Cc: $self->{cc}\n"   if $self->{cc};
118   $bcc = "Bcc: $self->{bcc}\n" if $self->{bcc};
119
120   foreach my $item (qw(to cc bcc)) {
121     $self->{$item} =~ s/\&lt;/</g;
122     $self->{$item} =~ s/\$<\$/</g;
123     $self->{$item} =~ s/\&gt;/>/g;
124     $self->{$item} =~ s/\$>\$/>/g;
125   }
126
127   my $subject = $self->mime_quote_text($self->{subject}, 60);
128
129   print OUT qq|From: $self->{from}
130 To: $self->{to}
131 ${cc}${bcc}Subject: $subject
132 Message-ID: <$msgid>
133 X-Mailer: Lx-Office $self->{version}
134 MIME-Version: 1.0
135 |;
136
137   if ($self->{attachments}) {
138     print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
139
140 |;
141     if ($self->{message}) {
142       print OUT qq|--${boundary}
143 Content-Type: $self->{contenttype}; charset="$self->{charset}"
144
145 $self->{message}
146
147 |;
148     }
149
150     foreach my $attachment (@{ $self->{attachments} }) {
151
152       my $filename;
153
154       if (ref($attachment) eq "HASH") {
155         $filename = $attachment->{"name"};
156         $attachment = $attachment->{"filename"};
157       } else {
158         $filename = $attachment;
159         # strip path
160         $filename =~ s/(.*\/|$self->{fileid})//g;
161       }
162
163       my $application =
164         ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/)
165         ? "text"
166         : "application";
167
168       open(IN, $attachment);
169       if ($?) {
170         close(OUT);
171         $main::lxdebug->leave_sub();
172         return "$attachment : $!";
173       }
174
175       print OUT qq|--${boundary}
176 Content-Type: $application/$self->{format}; name="$filename"; charset="$self->{charset}"
177 Content-Transfer-Encoding: BASE64
178 Content-Disposition: attachment; filename="$filename"\n\n|;
179
180       my $msg = "";
181       while (<IN>) {
182         ;
183         $msg .= $_;
184       }
185       print OUT &encode_base64($msg);
186
187       close(IN);
188
189     }
190     print OUT qq|--${boundary}--\n|;
191
192   } else {
193     print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
194
195 $self->{message}
196 |;
197   }
198
199   close(OUT);
200
201   $main::lxdebug->leave_sub();
202
203   return "";
204 }
205
206 sub encode_base64 ($;$) {
207   $main::lxdebug->enter_sub();
208
209   # this code is from the MIME-Base64-2.12 package
210   # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
211
212   my $res = "";
213   my $eol = $_[1];
214   $eol = "\n" unless defined $eol;
215   pos($_[0]) = 0;    # ensure start at the beginning
216
217   $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
218
219   $res =~ tr|` -_|AA-Za-z0-9+/|;    # `# help emacs
220                                     # fix padding at the end
221   my $padding = (3 - length($_[0]) % 3) % 3;
222   $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
223
224   # break encoded string into lines of no more than 60 characters each
225   if (length $eol) {
226     $res =~ s/(.{1,60})/$1$eol/g;
227   }
228
229   $main::lxdebug->leave_sub();
230
231   return $res;
232 }
233
234 1;
235