Beim Verschicken von Dokumenten per EMail die MIME-Header richtig setzen:
[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::MIME;
37 use SL::Template;
38
39 my $num_sent = 0;
40
41 sub new {
42   $main::lxdebug->enter_sub();
43
44   my ($type) = @_;
45   my $self = {};
46
47   $main::lxdebug->leave_sub();
48
49   bless $self, $type;
50 }
51
52 sub mime_quote_text {
53   $main::lxdebug->enter_sub();
54
55   my ($self, $text, $chars_left) = @_;
56
57   my $q_start = "=?$self->{charset}?Q?";
58   my $l_start = length($q_start);
59
60   my $new_text = "$q_start";
61   $chars_left -= $l_start if (defined $chars_left);
62
63   for (my $i = 0; $i < length($text); $i++) {
64     my $char = ord(substr($text, $i, 1));
65
66     if (($char < 32) || ($char > 127) || ($char == ord('?')) || ($char == ord('_'))) {
67       if ((defined $chars_left) && ($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 if (defined $chars_left);
74
75     } else {
76       $char = ord('_') if ($char == ord(' '));
77       if ((defined $chars_left) && ($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-- if (defined $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 bcc)) {
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       my $phrase = $addr_obj->phrase();
142       if ($phrase) {
143         $phrase =~ s/^\"//;
144         $phrase =~ s/\"$//;
145         $addr_obj->phrase($self->mime_quote_text($phrase));
146       }
147
148       $headers .= sprintf("%s: %s\n", ucfirst($item), $addr_obj->format());
149     }
150   }
151
152   $headers .= sprintf("Subject: %s\n", $self->mime_quote_text($self->{subject}, 60));
153
154   print OUT qq|${headers}Message-ID: <$msgid>
155 X-Mailer: Lx-Office $self->{version}
156 MIME-Version: 1.0
157 |;
158
159   if ($self->{attachments}) {
160     print OUT qq|Content-Type: multipart/mixed; boundary="$boundary"
161
162 |;
163     if ($self->{message}) {
164       print OUT qq|--${boundary}
165 Content-Type: $self->{contenttype}; charset="$self->{charset}"
166
167 $self->{message}
168
169 |;
170     }
171
172     foreach my $attachment (@{ $self->{attachments} }) {
173
174       my $filename;
175
176       if (ref($attachment) eq "HASH") {
177         $filename = $attachment->{"name"};
178         $attachment = $attachment->{"filename"};
179       } else {
180         $filename = $attachment;
181         # strip path
182         $filename =~ s/(.*\/|\Q$self->{fileid}\E)//g;
183       }
184
185       my $application    = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? "text" : "application";
186       my $content_type   = SL::MIME->mime_type_from_ext($filename);
187       $content_type      = "${application}/${self->{format}}" if (!$content_type && $self->{format});
188       $content_type    ||= 'application/octet-stream';
189
190       open(IN, $attachment);
191       if ($?) {
192         close(OUT);
193         $main::lxdebug->leave_sub();
194         return "$attachment : $!";
195       }
196
197       # only set charset for attachements of type text. every other type should not have this field
198       # refer to bug 883 for detailed information
199       my $attachment_charset;
200       if (lc $application eq 'text' && $self->{charset}) {
201         $attachment_charset = qq|; charset="$self->{charset}" |;
202       }
203
204       print OUT qq|--${boundary}
205 Content-Type: ${content_type}; name="$filename"$attachment_charset
206 Content-Transfer-Encoding: BASE64
207 Content-Disposition: attachment; filename="$filename"\n\n|;
208
209       my $msg = "";
210       while (<IN>) {
211         ;
212         $msg .= $_;
213       }
214       print OUT &encode_base64($msg);
215
216       close(IN);
217
218     }
219     print OUT qq|--${boundary}--\n|;
220
221   } else {
222     print OUT qq|Content-Type: $self->{contenttype}; charset="$self->{charset}"
223
224 $self->{message}
225 |;
226   }
227
228   close(OUT);
229
230   $main::lxdebug->leave_sub();
231
232   return "";
233 }
234
235 sub encode_base64 ($;$) {
236   $main::lxdebug->enter_sub();
237
238   # this code is from the MIME-Base64-2.12 package
239   # Copyright 1995-1999,2001 Gisle Aas <gisle@ActiveState.com>
240
241   my $res = "";
242   my $eol = $_[1];
243   $eol = "\n" unless defined $eol;
244   pos($_[0]) = 0;    # ensure start at the beginning
245
246   $res = join '', map(pack('u', $_) =~ /^.(\S*)/, ($_[0] =~ /(.{1,45})/gs));
247
248   $res =~ tr|` -_|AA-Za-z0-9+/|;    # `# help emacs
249                                     # fix padding at the end
250   my $padding = (3 - length($_[0]) % 3) % 3;
251   $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
252
253   # break encoded string into lines of no more than 60 characters each
254   if (length $eol) {
255     $res =~ s/(.{1,60})/$1$eol/g;
256   }
257
258   $main::lxdebug->leave_sub();
259
260   return $res;
261 }
262
263 1;
264