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("Invalid 'from' parameter: ".$args->{from})
81 unless Email::Valid->address($args->{from}); # from is mandatory
83 $args->{subject} = $params->{subject} // '';
85 if ( C4::Context->preference('SendAllEmailsTo') ) {
86 $args->{to} = C4::Context->preference('SendAllEmailsTo');
89 $args->{to} = $params->{to};
92 Koha::Exceptions::BadParameter->throw("Invalid 'to' parameter: ".$args->{to})
93 unless Email::Valid->address($args->{to}); # to is mandatory
96 $addresses->{reply_to} = $params->{reply_to};
97 $addresses->{reply_to} ||= C4::Context->preference('ReplytoDefault')
98 if C4::Context->preference('ReplytoDefault');
100 $addresses->{sender} = $params->{sender};
101 $addresses->{sender} ||= C4::Context->preference('ReturnpathDefault')
102 if C4::Context->preference('ReturnpathDefault');
104 unless ( C4::Context->preference('SendAllEmailsTo') ) {
105 $addresses->{cc} = $params->{cc}
106 if exists $params->{cc};
107 $addresses->{bcc} = $params->{bcc}
108 if exists $params->{bcc};
111 foreach my $address ( keys %{ $addresses } ) {
112 Koha::Exceptions::BadParameter->throw("Invalid '$address' parameter: ".$addresses->{$address})
113 if $addresses->{$address} and !Email::Valid->address($addresses->{$address});
116 $args->{cc} = $addresses->{cc}
118 $args->{bcc} = $addresses->{bcc}
119 if $addresses->{bcc};
121 my $email = $self->SUPER::new( $args );
123 $email->header( 'Reply-To', $addresses->{reply_to} )
124 if $addresses->{reply_to};
126 $email->header( 'Sender' => $addresses->{sender} ) if $addresses->{sender};
127 $email->header( 'Content-Type' => $params->{contenttype} ) if $params->{contenttype};
128 $email->header( 'X-Mailer' => "Koha" );
129 $email->header( 'Message-ID' => Email::MessageID->new->in_brackets );
131 if ( $params->{text_body} ) {
132 $email->text_body( $params->{text_body}, %{ $params->{body_params} } );
134 elsif ( $params->{html_body} ) {
135 $email->html_body( $params->{html_body}, %{ $params->{body_params} } );
143 $email->send_or_die({ transport => $transport [, $args] });
145 Overloaded Email::Stuffer I<send_or_die> method, that takes care of Bcc handling.
146 Bcc is removed from the message headers, and included in the recipients list to be
147 passed to I<send_or_die>.
152 my ( $self, $args ) = @_;
154 unless ( $args->{to} ) { # don't do it if passed an explicit 'to' param
158 my @headers = $self->email->header_str_pairs;
159 foreach my $pair ( pairs @headers ) {
160 my ( $header, $value ) = @$pair;
161 push @recipients, split (', ', $value)
162 if grep { $_ eq $header } ('To', 'Cc', 'Bcc');
165 # Remove the Bcc header
166 $self->email->header_str_set('Bcc');
169 $args->{to} = \@recipients;
172 $self->SUPER::send_or_die($args);