3 # Copyright 2014 Catalyst
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26 use List::Util qw( pairs );
27 use Scalar::Util qw( blessed );
33 use base qw( Email::Stuffer );
37 Koha::Email - A wrapper around Email::Stuffer
43 =head3 new_from_string
45 my $email = Koha::Email->new_from_string( $email_string );
47 Constructor for the Koha::Email class. The I<$email_string> (mandatory)
48 parameter will be parsed with I<Email::MIME>.
50 Note: I<$email_string> can be the produced by the I<as_string> method from
51 B<Koha::Email> or B<Email::MIME>.
56 my ( $class, $email_string ) = @_;
58 Koha::Exceptions::MissingParameter->throw("Mandatory string parameter missing.")
61 my $self = $class->SUPER::new();
62 my $mime = Email::MIME->new( $email_string );
63 $self->{email} = $mime;
70 my $email = Koha::Email->create(
72 [ text_body => $text_message,
73 html_body => $html_message,
74 body_params => $body_params ]
79 reply_to => $reply_to,
85 This method creates a new Email::Stuffer object taking Koha specific configurations
88 The encoding defaults to utf-8. It can be set as part of the body_params hashref. See
89 I<Email::Stuffer> and I<Email::MIME> for more details on the available options.
92 - I<from> defaults to the value of the I<KohaAdminEmailAddress> system preference
93 - The I<SendAllEmailsTo> system preference overloads the I<to>, I<cc> and I<bcc> parameters
94 - I<reply_to> defaults to the value of the I<ReplytoDefault> system preference
95 - I<sender> defaults to the value of the I<ReturnpathDefault> system preference
97 Both I<text_body> and I<html_body> can be set later. I<body_params> will be passed if present
103 my ( $self, $params ) = @_;
106 $args->{from} = $params->{from} || C4::Context->preference('KohaAdminEmailAddress');
107 Koha::Exceptions::BadParameter->throw(
108 error => "Invalid 'from' parameter: " . $args->{from},
110 ) unless Koha::Email->is_valid( $args->{from} ); # from is mandatory
112 $args->{subject} = $params->{subject} // '';
114 if ( C4::Context->preference('SendAllEmailsTo') ) {
115 $args->{to} = C4::Context->preference('SendAllEmailsTo');
118 $args->{to} = $params->{to};
121 my @emails = split(',', $args->{to});
122 foreach my $email (@emails) {
124 Koha::Exceptions::BadParameter->throw(
125 error => "Invalid 'to' parameter: ".$email,
127 ) unless Koha::Email->is_valid($email);
131 $addresses->{reply_to} = $params->{reply_to};
132 $addresses->{reply_to} ||= C4::Context->preference('ReplytoDefault')
133 if C4::Context->preference('ReplytoDefault');
135 $addresses->{sender} = $params->{sender};
136 $addresses->{sender} ||= C4::Context->preference('ReturnpathDefault')
137 if C4::Context->preference('ReturnpathDefault');
139 unless ( C4::Context->preference('SendAllEmailsTo') ) {
140 $addresses->{cc} = $params->{cc}
141 if exists $params->{cc};
142 $addresses->{bcc} = $params->{bcc}
143 if exists $params->{bcc};
146 foreach my $address ( keys %{$addresses} ) {
147 Koha::Exceptions::BadParameter->throw(
148 error => "Invalid '$address' parameter: " . $addresses->{$address},
149 parameter => $address
151 if $addresses->{$address}
152 and !Koha::Email->is_valid( $addresses->{$address} );
155 $args->{cc} = $addresses->{cc}
157 $args->{bcc} = $addresses->{bcc}
158 if $addresses->{bcc};
161 # FIXME: This is ugly, but aids backportability
162 # TODO: Remove this and move address and default headers handling
163 # to separate subs to be (re)used
164 if ( blessed($self) ) {
166 $email->to( $args->{to} ) if $args->{to};
167 $email->from( $args->{from} ) if $args->{from};
168 $email->cc( $args->{cc} ) if $args->{cc};
169 $email->bcc( $args->{bcc} ) if $args->{bcc};
170 $email->reply_to( $args->{reply_to} ) if $args->{reply_to};
171 $email->subject( $args->{subject} ) if $args->{subject};
174 $email = $self->SUPER::new( $args );
177 $email->header( 'Reply-To', $addresses->{reply_to} )
178 if $addresses->{reply_to};
180 $email->header( 'Sender' => $addresses->{sender} ) if $addresses->{sender};
181 $email->header( 'Content-Type' => $params->{contenttype} ) if $params->{contenttype};
182 $email->header( 'X-Mailer' => "Koha" );
183 $email->header( 'Message-ID' => Email::MessageID->new->in_brackets );
185 if ( $params->{text_body} ) {
186 $email->text_body( $params->{text_body}, %{ $params->{body_params} } );
188 elsif ( $params->{html_body} ) {
189 $email->html_body( $params->{html_body}, %{ $params->{body_params} } );
197 $email->send_or_die({ transport => $transport [, $args] });
199 Overloaded Email::Stuffer I<send_or_die> method, that takes care of Bcc and Return-path
202 Bcc is removed from the message headers, and included in the recipients list to be
203 passed to I<send_or_die>.
205 Return-path, 'MAIL FROM', is set to the 'Sender' email header unless an explicit 'from'
206 parameter is passed to send_or_die. 'Return-path' headers are actually set by the MTA,
207 usually using the 'MAIL FROM' information set at mail server connection time.
212 my ( $self, $args ) = @_;
214 unless ( $args->{to} ) { # don't do it if passed an explicit 'to' param
218 my @headers = $self->email->header_str_pairs;
219 foreach my $pair ( pairs @headers ) {
220 my ( $header, $value ) = @$pair;
221 push @recipients, split (', ', $value)
222 if grep { $_ eq $header } ('To', 'Cc', 'Bcc');
225 # Remove the Bcc header
226 $self->email->header_str_set('Bcc');
229 $args->{to} = \@recipients;
232 unless ( $args->{from} ) { # don't do it if passed an explicit 'from' param
233 $args->{from} = $self->email->header_str('Sender');
234 $self->email->header_str_set('Sender'); # remove Sender header
237 $self->SUPER::send_or_die($args);
242 my $is_valid = Koha::Email->is_valid($email_address);
244 Return true is the email address passed in parameter is valid following RFC 2822.
249 my ( $class, $email ) = @_;
250 my @addrs = Email::Address->parse($email);
251 return @addrs ? 1 : 0;