Beim Verschicken von Emails müssen die Absender- und Empfängeradressen MIME-Encodiert...
[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 Email::Address;
34
35 use SL::Common;
36 use SL::Template;
37
38 my $num_sent = 0;
39
40 sub new {
41   $main::lxdebug->enter_sub();
42
43   my ($type) = @_;
44   my $self = {};
45
46   $main::lxdebug->leave_sub();
47
48   bless $self, $type;
49 }
50
51 sub mime_quote_text {
52   $main::lxdebug->enter_sub();
53
54   my ($self, $text, $chars_left) = @_;
55
56   my $q_start = "=?$self->{charset}?Q?";
57   my $l_start = length($q_start);
58
59   my $new_text = "$q_start";
60   $chars_left -= $l_start;
61
62   for (my $i = 0; $i < length($text); $i++) {
63     my $char = ord(substr($text, $i, 1));
64
65     if (($char < 32) || ($char > 127) ||
66         ($char == ord('?')) || ($char == ord('_'))) {
67       if ($chars_left < 5) {
68         $new_text .= "?=\n $q_start";
69         $chars_left = 75 - $l_start;
70       }
71
72       $new_text .= sprintf("=%02X", $char);
73       $chars_left -= 3;
74
75     } else {
76       $char = ord('_') if ($char == ord(' '));
77       if ($chars_left < 5) {
78         $new_text .= "?=\n $q_start";
79         $chars_left = 75 - $l_start;
80       }
81
82       $new_text .= chr($char);
83       $chars_left--;
84     }
85   }
86
87   $new_text .= "?=";
88
89   $main::lxdebug->leave_sub();
90
91   return $new_text;
92 }
93
94 sub send {
95   $main::lxdebug->enter_sub();
96
97   my ($self) = @_;
98
99   local (*IN, *OUT);
100
101   $num_sent++;
102   my $boundary    = time() . "-$$-${num_sent}";
103   $boundary       =  "LxOffice-$self->{version}-$boundary";
104   my $domain      =  $self->{from};
105   $domain         =~ s/(.*?\@|>)//g;
106   my $msgid       =  "$boundary\@$domain";
107
108   my $form        =  $main::form;
109   my $myconfig    =  \%main::myconfig;
110
111   my $email       =  $myconfig->{email};
112   $email          =~ s/[^\w\.\-\+=@]//ig;
113
114   my %temp_form   = ( %{ $form }, 'myconfig_email' => $email );
115   my $template    = PlainTextTemplate->new(undef, \%temp_form, $myconfig);
116   my $sendmail    = $template->parse_block($main::sendmail);
117
118   if (!open(OUT, $sendmail)) {
119     $main::lxdebug->leave_sub();
120     return "$sendmail : $!";
121   }
122
123   $self->{charset}     ||= Common::DEFAULT_CHARSET;
124   $self->{contenttype} ||= "text/plain";
125
126   foreach my $item (qw(to cc bcc)) {
127     next unless ($self->{$item});
128     $self->{$item} =~ s/\&lt;/</g;
129     $self->{$item} =~ s/\$<\$/</g;
130     $self->{$item} =~ s/\&gt;/>/g;
131     $self->{$item} =~ s/\$>\$/>/g;
132   }
133
134   my $headers = '';
135   foreach my $item (qw(from to cc)) {
136     next unless ($self->{$item});
137     my (@addr_objects) = Email::Address->parse($self->{$item});
138     next unless (scalar @addr_objects);
139
140     foreach my $addr_obj (@addr_objects) {
141       $addr_obj->phrase($self->mime_quote_text($addr_obj->phrase(), 60))   if ($addr_obj->phrase());
142       $addr_obj->comment($self->mime_quote_text($addr_obj->comment(), 60)) if ($addr_obj->comment());
143
144       $headers .= sprintf("%s: %s\n", ucfirst($item), $addr_obj->format());
145     }
146   }
147
148   $headers .= sprintf("Subject: %s\n", $self->mime_quote_text($self->{subject}, 60));
149
150   print OUT qq|${headers}Message-ID: <$msgid>
151 X-Mailer: Lx-Office $self->{version}
152 MIME-Version: 1.0
153 |;
154
155   if ($self->{attachments}) {
156     print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
157
158 |;
159     if ($self->{message}) {
160       print OUT qq|--${boundary}
161 Content-Type: $self->{contenttype}; charset="$self->{charset}"
162
163 $self->{message}
164
165 |;
166     }
167
168     foreach my $attachment (@{ $self->{attachments} }) {
169
170       my $filename;
171
172       if (ref($attachment) eq "HASH") {
173         $filename = $attachment->{"name"};
174         $attachment = $attachment->{"filename"};
175       } else {
176         $filename = $attachment;
177         # strip path
178         $filename =~ s/(.*\/|\Q$self->{fileid}\E)//g;
179       }
180
181       my $application =
182         ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/)
183         ? "text"
184         : "application";
185
186       open(IN, $attachment);
187       if ($?) {
188         close(OUT);
189         $main::lxdebug->leave_sub();
190         return "$attachment : $!";
191       }
192
193       print OUT qq|--${boundary}
194 Content-Type: $application/$self->{format}; name="$filename"; charset="$self->{charset}"
195 Content-Transfer-Encoding: BASE64
196 Content-Disposition: attachment; filename="$filename"\n\n|;
197
198       my $msg = "";
199       while (<IN>) {
200         ;
201         $msg .= $_;
202       }
203       print OUT &encode_base64($msg);
204
205       close(IN);
206
207     }
208     print OUT qq|--${boundary}--\n|;
209
210   } else {
211     print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
212
213 $self->{message}
214 |;
215   }
216
217   close(OUT);
218
219   $main::lxdebug->leave_sub();
220
221   return "";
222 }
223
224 sub encode_base64 ($;$) {
225   $main::lxdebug->enter_sub();
226
227   # this code is from the MIME-Base64-2.12 package
228   # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
229
230   my $res = "";
231   my $eol = $_[1];
232   $eol = "\n" unless defined $eol;
233   pos($_[0]) = 0;    # ensure start at the beginning
234
235   $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
236
237   $res =~ tr|` -_|AA-Za-z0-9+/|;    # `# help emacs
238                                     # fix padding at the end
239   my $padding = (3 - length($_[0]) % 3) % 3;
240   $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
241
242   # break encoded string into lines of no more than 60 characters each
243   if (length $eol) {
244     $res =~ s/(.{1,60})/$1$eol/g;
245   }
246
247   $main::lxdebug->leave_sub();
248
249   return $res;
250 }
251
252 1;
253