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>.
25 use List::Util qw( pairs );
31 use base qw( Email::Stuffer );
35 Koha::Email - A wrapper around Email::Stuffer
43 my $email = Koha::Email->create(
45 [ text_body => $text_message,
46 html_body => $html_message,
47 body_params => $body_params ]
52 reply_to => $reply_to,
58 This method creates a new Email::Stuffer object taking Koha specific configurations
61 The encoding defaults to utf-8. It can be set as part of the body_params hashref. See
62 I<Email::Stuffer> and I<Email::MIME> for more details on the available options.
65 - I<from> defaults to the value of the I<KohaAdminEmailAddress> system preference
66 - The I<SendAllEmailsTo> system preference overloads the I<to>, I<cc> and I<bcc> parameters
67 - I<reply_to> defaults to the value of the I<ReplytoDefault> system preference
68 - I<sender> defaults to the value of the I<ReturnpathDefault> system preference
70 Both I<text_body> and I<html_body> can be set later. I<body_params> will be passed if present
76 my ( $self, $params ) = @_;
79 $args->{from} = $params->{from} || C4::Context->preference('KohaAdminEmailAddress');
80 Koha::Exceptions::BadParameter->throw(
81 error => "Invalid 'from' parameter: " . $args->{from},
83 ) unless Koha::Email->is_valid( $args->{from} ); # from is mandatory
85 $args->{subject} = $params->{subject} // '';
87 if ( C4::Context->preference('SendAllEmailsTo') ) {
88 $args->{to} = C4::Context->preference('SendAllEmailsTo');
91 $args->{to} = $params->{to};
94 Koha::Exceptions::BadParameter->throw(
95 error => "Invalid 'to' parameter: " . $args->{to},
97 ) unless Koha::Email->is_valid( $args->{to} ); # to is mandatory
100 $addresses->{reply_to} = $params->{reply_to};
101 $addresses->{reply_to} ||= C4::Context->preference('ReplytoDefault')
102 if C4::Context->preference('ReplytoDefault');
104 $addresses->{sender} = $params->{sender};
105 $addresses->{sender} ||= C4::Context->preference('ReturnpathDefault')
106 if C4::Context->preference('ReturnpathDefault');
108 unless ( C4::Context->preference('SendAllEmailsTo') ) {
109 $addresses->{cc} = $params->{cc}
110 if exists $params->{cc};
111 $addresses->{bcc} = $params->{bcc}
112 if exists $params->{bcc};
115 foreach my $address ( keys %{$addresses} ) {
116 Koha::Exceptions::BadParameter->throw(
117 error => "Invalid '$address' parameter: " . $addresses->{$address},
118 parameter => $address
120 if $addresses->{$address}
121 and !Koha::Email->is_valid( $addresses->{$address} );
124 $args->{cc} = $addresses->{cc}
126 $args->{bcc} = $addresses->{bcc}
127 if $addresses->{bcc};
129 my $email = $self->SUPER::new( $args );
131 $email->header( 'Reply-To', $addresses->{reply_to} )
132 if $addresses->{reply_to};
134 $email->header( 'Sender' => $addresses->{sender} ) if $addresses->{sender};
135 $email->header( 'Content-Type' => $params->{contenttype} ) if $params->{contenttype};
136 $email->header( 'X-Mailer' => "Koha" );
137 $email->header( 'Message-ID' => Email::MessageID->new->in_brackets );
139 if ( $params->{text_body} ) {
140 $email->text_body( $params->{text_body}, %{ $params->{body_params} } );
142 elsif ( $params->{html_body} ) {
143 $email->html_body( $params->{html_body}, %{ $params->{body_params} } );
151 $email->send_or_die({ transport => $transport [, $args] });
153 Overloaded Email::Stuffer I<send_or_die> method, that takes care of Bcc handling.
154 Bcc is removed from the message headers, and included in the recipients list to be
155 passed to I<send_or_die>.
160 my ( $self, $args ) = @_;
162 unless ( $args->{to} ) { # don't do it if passed an explicit 'to' param
166 my @headers = $self->email->header_str_pairs;
167 foreach my $pair ( pairs @headers ) {
168 my ( $header, $value ) = @$pair;
169 push @recipients, split (', ', $value)
170 if grep { $_ eq $header } ('To', 'Cc', 'Bcc');
173 # Remove the Bcc header
174 $self->email->header_str_set('Bcc');
177 $args->{to} = \@recipients;
180 $self->SUPER::send_or_die($args);
185 my $is_valid = Koha::Email->is_valid($email_address);
187 Return true is the email address passed in parameter is valid following RFC 2822.
192 my ( $class, $email ) = @_;
193 my @addrs = Email::Address->parse($email);
194 return @addrs ? 1 : 0;