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