posaune
[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 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #======================================================================
22
23 package Mailer;
24
25 use Email::Address;
26 use Email::MIME::Creator;
27 use File::Slurp;
28
29 use SL::Common;
30 use SL::MIME;
31 use SL::Template;
32
33 use strict;
34
35 my $num_sent = 0;
36
37 sub new {
38   my ($type, %params) = @_;
39   my $self = { %params };
40
41   bless $self, $type;
42 }
43
44 sub _create_driver {
45   my ($self) = @_;
46
47   my %params = (
48     mailer   => $self,
49     form     => $::form,
50     myconfig => \%::myconfig,
51   );
52
53   my $module = ($::lx_office_conf{mail_delivery}->{method} || 'smtp') ne 'smtp' ? 'SL::Mailer::Sendmail' : 'SL::Mailer::SMTP';
54   eval "require $module" or return undef;
55
56   return $module->new(%params);
57 }
58
59 sub _cleanup_addresses {
60   my ($self) = @_;
61
62   foreach my $item (qw(to cc bcc)) {
63     next unless $self->{$item};
64
65     $self->{$item} =~ s/\&lt;/</g;
66     $self->{$item} =~ s/\$<\$/</g;
67     $self->{$item} =~ s/\&gt;/>/g;
68     $self->{$item} =~ s/\$>\$/>/g;
69   }
70 }
71
72 sub _create_message_id {
73   my ($self) = @_;
74
75   $num_sent  +=  1;
76   my $domain  =  $self->{from};
77   $domain     =~ s/.*\@//;
78   $domain     =~ s/>.*//;
79
80   return  "kivitendo-$self->{version}-" . time() . "-${$}-${num_sent}\@$domain";
81 }
82
83 sub _create_address_headers {
84   my ($self) = @_;
85
86   # $self->{addresses} collects the recipients for use in e.g. the
87   # SMTP 'RCPT TO:' envelope command. $self->{headers} collects the
88   # headers that make up the actual email. 'BCC' should not be
89   # included there for certain transportation methods (SMTP).
90
91   $self->{addresses} = {};
92
93   foreach my $item (qw(from to cc bcc)) {
94     $self->{addresses}->{$item} = [];
95     next if !$self->{$item};
96
97     my @header_addresses;
98
99     foreach my $addr_obj (Email::Address->parse($self->{$item})) {
100       push @{ $self->{addresses}->{$item} }, $addr_obj->address;
101       next if $self->{driver}->keep_from_header($item);
102
103       my $phrase = $addr_obj->phrase();
104       if ($phrase) {
105         $phrase =~ s/^\"//;
106         $phrase =~ s/\"$//;
107         $addr_obj->phrase($phrase);
108       }
109
110       push @header_addresses, $addr_obj->format;
111     }
112
113     push @{ $self->{headers} }, ( ucfirst($item) => join(', ', @header_addresses) ) if @header_addresses;
114   }
115 }
116
117 sub _create_attachment_part {
118   my ($self, $attachment) = @_;
119
120   my $source_file_name;
121
122   my %attributes = (
123     disposition  => 'attachment',
124     encoding     => 'base64',
125   );
126
127   if (ref($attachment) eq "HASH") {
128     $attributes{filename} = $attachment->{name};
129     $source_file_name     = $attachment->{filename};
130
131   } else {
132     # strip path
133     $attributes{filename} =  $attachment;
134     $attributes{filename} =~ s:.*\Q$self->{fileid}\E:: if $self->{fileid};
135     $attributes{filename} =~ s:.*/::g;
136     $source_file_name     =  $attachment;
137   }
138
139   my $attachment_content = eval { read_file($source_file_name) };
140   return undef if !defined $attachment_content;
141
142   my $application             = ($attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/) ? 'text' : 'application';
143   $attributes{content_type}   = SL::MIME->mime_type_from_ext($attributes{filename});
144   $attributes{content_type} ||= "${application}/$self->{format}" if $self->{format};
145   $attributes{content_type} ||= 'application/octet-stream';
146   $attributes{charset}        = $self->{charset} if lc $application eq 'text' && $self->{charset};
147
148   return Email::MIME->create(
149     attributes => \%attributes,
150     body       => $attachment_content,
151   );
152 }
153
154 sub _create_message {
155   my ($self) = @_;
156
157   my @parts;
158
159   if ($self->{message}) {
160     push @parts, Email::MIME->create(
161       attributes => {
162         content_type => $self->{contenttype},
163         charset      => $self->{charset},
164         encoding     => 'quoted-printable',
165       },
166       body_str => $self->{message},
167     );
168
169     push @{ $self->{headers} }, (
170       'Content-Type' => qq|$self->{contenttype}; charset="$self->{charset}"|,
171     );
172   }
173
174   push @parts, grep { $_ } map { $self->_create_attachment_part($_) } @{ $self->{attachments} || [] };
175
176   return Email::MIME->create(
177     header_str => $self->{headers},
178     parts      => \@parts,
179   );
180 }
181
182 sub send {
183   my ($self) = @_;
184
185   # Create driver for delivery method (sendmail/SMTP)
186   $self->{driver} = eval { $self->_create_driver };
187   if (!$self->{driver}) {
188     $::lxdebug->leave_sub();
189     return "send email : $@";
190   }
191
192   # Set defaults & headers
193   $self->{charset}       =  'UTF-8';
194   $self->{contenttype} ||=  "text/plain";
195   $self->{headers}       =  [
196     Subject              => $self->{subject},
197     'Message-ID'         => '<' . $self->_create_message_id . '>',
198     'X-Mailer'           => "kivitendo $self->{version}",
199   ];
200
201   # Clean up To/Cc/Bcc address fields
202   $self->_cleanup_addresses;
203   $self->_create_address_headers;
204
205   my $email = $self->_create_message;
206
207   # $::lxdebug->message(0, "message: " . $email->as_string);
208   # return "boom";
209
210   $self->{driver}->start_mail(from => $self->{from}, to => [ map { @{ $self->{addresses}->{$_} } } qw(to cc bcc) ]);
211   $self->{driver}->print($email->as_string);
212   $self->{driver}->send;
213
214   return '';
215 }
216
217 1;