Bug 27926: (QA follow-up) Switch to using data-order
[koha.git] / Koha / Email.pm
1 package Koha::Email;
2
3 # Copyright 2014 Catalyst
4 #           2020 Theke Solutions
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
23 use Email::Valid;
24 use Email::MessageID;
25 use List::Util qw(pairs);
26
27 use Koha::Exceptions;
28
29 use C4::Context;
30
31 use base qw( Email::Stuffer );
32
33 =head1 NAME
34
35 Koha::Email - A wrapper around Email::Stuffer
36
37 =head1 API
38
39 =head2 Class methods
40
41 =head3 create
42
43     my $email = Koha::Email->create(
44         {
45           [ text_body   => $text_message,
46             html_body   => $html_message,
47             body_params => $body_params ]
48             from        => $from,
49             to          => $to,
50             cc          => $cc,
51             bcc         => $bcc,
52             reply_to    => $reply_to,
53             sender      => $sender,
54             subject     => $subject,
55         }
56     );
57
58 This method creates a new Email::Stuffer object taking Koha specific configurations
59 into account.
60
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.
63
64 Parameters:
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
69
70 Both I<text_body> and I<html_body> can be set later. I<body_params> will be passed if present
71 to the constructor.
72
73 =cut
74
75 sub create {
76     my ( $self, $params ) = @_;
77
78     my $args = {};
79     $args->{from} = $params->{from} || C4::Context->preference('KohaAdminEmailAddress');
80     Koha::Exceptions::BadParameter->throw("Invalid 'from' parameter: ".$args->{from})
81         unless Email::Valid->address( -address => $args->{from}, -fqdn => 0 ); # from is mandatory
82
83     $args->{subject} = $params->{subject} // '';
84
85     if ( C4::Context->preference('SendAllEmailsTo') ) {
86         $args->{to} = C4::Context->preference('SendAllEmailsTo');
87     }
88     else {
89         $args->{to} = $params->{to};
90     }
91
92     Koha::Exceptions::BadParameter->throw("Invalid 'to' parameter: ".$args->{to})
93         unless Email::Valid->address( -address => $args->{to}, -fqdn => 0 ); # to is mandatory
94
95     my $addresses = {};
96     $addresses->{reply_to} = $params->{reply_to};
97     $addresses->{reply_to} ||= C4::Context->preference('ReplytoDefault')
98         if C4::Context->preference('ReplytoDefault');
99
100     $addresses->{sender} = $params->{sender};
101     $addresses->{sender} ||= C4::Context->preference('ReturnpathDefault')
102         if C4::Context->preference('ReturnpathDefault');
103
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};
109     }
110
111     foreach my $address ( keys %{$addresses} ) {
112         Koha::Exceptions::BadParameter->throw(
113             "Invalid '$address' parameter: " . $addresses->{$address} )
114           if $addresses->{$address} and !Email::Valid->address(
115             -address => $addresses->{$address},
116             -fqdn    => 0
117           );
118     }
119
120     $args->{cc} = $addresses->{cc}
121         if $addresses->{cc};
122     $args->{bcc} = $addresses->{bcc}
123         if $addresses->{bcc};
124
125     my $email = $self->SUPER::new( $args );
126
127     $email->header( 'Reply-To', $addresses->{reply_to} )
128         if $addresses->{reply_to};
129
130     $email->header( 'Sender'       => $addresses->{sender} ) if $addresses->{sender};
131     $email->header( 'Content-Type' => $params->{contenttype} ) if $params->{contenttype};
132     $email->header( 'X-Mailer'     => "Koha" );
133     $email->header( 'Message-ID'   => Email::MessageID->new->in_brackets );
134
135     if ( $params->{text_body} ) {
136         $email->text_body( $params->{text_body}, %{ $params->{body_params} } );
137     }
138     elsif ( $params->{html_body} ) {
139         $email->html_body( $params->{html_body}, %{ $params->{body_params} } );
140     }
141
142     return $email;
143 }
144
145 =head3 send_or_die
146
147     $email->send_or_die({ transport => $transport [, $args] });
148
149 Overloaded Email::Stuffer I<send_or_die> method, that takes care of Bcc handling.
150 Bcc is removed from the message headers, and included in the recipients list to be
151 passed to I<send_or_die>.
152
153 =cut
154
155 sub send_or_die {
156     my ( $self, $args ) = @_;
157
158     unless ( $args->{to} ) {    # don't do it if passed an explicit 'to' param
159
160         my @recipients;
161
162         my @headers = $self->email->header_str_pairs;
163         foreach my $pair ( pairs @headers ) {
164             my ( $header, $value ) = @$pair;
165             push @recipients, split (', ', $value)
166                 if grep { $_ eq $header } ('To', 'Cc', 'Bcc');
167         }
168
169         # Remove the Bcc header
170         $self->email->header_str_set('Bcc');
171
172         # Tweak $args
173         $args->{to} = \@recipients;
174     }
175
176     $self->SUPER::send_or_die($args);
177 }
178
179 1;