3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 4;
27 use_ok('Koha::Email');
29 subtest 'create() tests' => sub {
33 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
35 my $html_body = '<h1>Title</h1><p>Message</p>';
36 my $text_body = "#Title: Message";
38 my $email = Koha::Email->create(
40 from => 'Fróm <from@example.com>',
41 to => 'Tö <to@example.com>',
42 cc => 'cc@example.com',
43 bcc => 'bcc@example.com',
44 reply_to => 'reply_to@example.com',
45 sender => 'sender@example.com',
46 subject => 'Some subject',
47 html_body => $html_body,
48 body_params => { charset => 'iso-8859-1' },
52 is( $email->email->header('From'), 'Fróm <from@example.com>', 'Value set correctly' );
53 is( $email->email->header('To'), 'Tö <to@example.com>', 'Value set correctly' );
54 is( $email->email->header('Cc'), 'cc@example.com', 'Value set correctly' );
55 is( $email->email->header('Bcc'), 'bcc@example.com', 'Value set correctly' );
56 is( $email->email->header('Reply-To'), 'reply_to@example.com', 'Value set correctly' );
57 is( $email->email->header('Sender'), 'sender@example.com', 'Value set correctly' );
58 is( $email->email->header('Subject'), 'Some subject', 'Value set correctly' );
59 is( $email->email->header('X-Mailer'), 'Koha', 'Value set correctly' );
60 is( $email->email->body, $html_body, "Body set correctly" );
61 like( $email->email->content_type, qr|text/html|, "Content type set correctly");
62 like( $email->email->content_type, qr|charset="?iso-8859-1"?|, "Charset set correctly");
63 like( $email->email->header('Message-ID'), qr/\<.*@.*\>/, 'Value set correctly' );
65 $email = Koha::Email->create(
67 from => 'from@8.8.8.8',
68 to => 'to@example.com',
69 bcc => 'root@localhost',
73 is( $email->email->header('Bcc'), 'root@localhost', 'Non-FQDN (@localhost) supported' );
74 is( $email->email->header('From'), 'from@8.8.8.8', 'IPs supported' );
76 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'catchall@example.com' );
77 t::lib::Mocks::mock_preference( 'ReplytoDefault', 'replytodefault@example.com' );
78 t::lib::Mocks::mock_preference( 'ReturnpathDefault', 'returnpathdefault@example.com' );
79 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'kohaadminemailaddress@example.com' );
81 $email = Koha::Email->create(
83 to => 'to@example.com',
84 cc => 'cc@example.com',
85 bcc => 'bcc@example.com',
86 text_body => $text_body,
90 is( $email->email->header('From'), 'kohaadminemailaddress@example.com', 'KohaAdminEmailAddress is picked when no from passed' );
91 is( $email->email->header('To'), 'catchall@example.com', 'SendAllEmailsTo overloads any address' );
92 is( $email->email->header('Cc'), undef, 'SendAllEmailsTo overloads any address' );
93 is( $email->email->header('Bcc'), undef, 'SendAllEmailsTo overloads any address' );
94 is( $email->email->header('Reply-To'), 'replytodefault@example.com', 'ReplytoDefault picked when replyto not passed' );
95 is( $email->email->header('Sender'), 'returnpathdefault@example.com', 'ReturnpathDefault picked when sender not passed' );
96 is( $email->email->header('Subject'), '', 'No subject passed, empty string' );
97 is( $email->email->body, $text_body, "Body set correctly" );
98 like( $email->email->content_type, qr|text/plain|, "Content type set correctly");
99 like( $email->email->content_type, qr|charset="?utf-8"?|, "Charset set correctly");
101 subtest 'exception cases' => sub {
106 { Koha::Email->create({ from => 'not_an_email' }); }
107 'Koha::Exceptions::BadParameter',
108 'Exception thrown correctly';
110 is( "$@", q{Invalid 'from' parameter: not_an_email}, 'Exception message correct' );
112 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'not_an_email' );
115 { Koha::Email->create({ }); }
116 'Koha::Exceptions::BadParameter',
117 'Exception thrown correctly';
119 is( "$@", q{Invalid 'from' parameter: not_an_email}, 'Exception message correct' );
121 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'tomasito@mail.com' );
122 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
125 { Koha::Email->create({ to => 'not_an_email' }); }
126 'Koha::Exceptions::BadParameter',
127 'Exception thrown correctly';
129 is( "$@", q{Invalid 'to' parameter: not_an_email}, 'Exception message correct' );
131 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'not_an_email' );
134 { Koha::Email->create({ }); }
135 'Koha::Exceptions::BadParameter',
136 'Exception thrown correctly';
138 is( "$@", q{Invalid 'to' parameter: not_an_email}, 'Exception message correct' );
140 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
143 { Koha::Email->create(
145 to => 'tomasito@mail.com',
146 reply_to => 'not_an_email'
149 'Koha::Exceptions::BadParameter',
150 'Exception thrown correctly';
152 is( "$@", q{Invalid 'reply_to' parameter: not_an_email}, 'Exception message correct' );
155 { Koha::Email->create(
157 to => 'tomasito@mail.com',
158 sender => 'not_an_email'
161 'Koha::Exceptions::BadParameter',
162 'Exception thrown correctly';
164 is( "$@", q{Invalid 'sender' parameter: not_an_email}, 'Exception message correct' );
167 { Koha::Email->create(
169 to => 'tomasito@mail.com',
173 'Koha::Exceptions::BadParameter',
174 'Exception thrown correctly';
176 is( "$@", q{Invalid 'cc' parameter: not_an_email}, 'Exception message correct' );
179 { Koha::Email->create(
181 to => 'tomasito@mail.com',
182 bcc => 'not_an_email'
185 'Koha::Exceptions::BadParameter',
186 'Exception thrown correctly';
188 is( "$@", q{Invalid 'bcc' parameter: not_an_email}, 'Exception message correct' );
192 subtest 'send_or_die() tests' => sub {
199 my $transport = "Hi there!";
201 my $mocked_email_simple = Test::MockModule->new('Email::Sender::Simple');
202 $mocked_email_simple->mock(
212 my $html_body = '<h1>Title</h1><p>Message</p>';
213 my $THE_email = Koha::Email->create(
215 from => 'from@example.com',
216 to => 'to@example.com',
217 cc => 'cc@example.com',
218 reply_to => 'reply_to@example.com',
219 sender => 'sender@example.com',
220 html_body => $html_body
224 my @bcc = ( 'bcc_1@example.com', 'bcc_2@example.com' );
226 $THE_email->bcc(@bcc);
229 $THE_email->email->header_str('Bcc'),
231 'Bcc header set correctly'
234 $THE_email->send_or_die(
235 { transport => $transport, to => ['tomasito@mail.com'] } );
236 is_deeply( $args->{to}, ['tomasito@mail.com'],
237 'If explicitly passed, "to" is preserved' );
239 $THE_email->send_or_die( { transport => $transport } );
240 my @to = sort @{ $args->{to} };
244 'bcc_1@example.com', 'bcc_2@example.com',
245 'cc@example.com', 'to@example.com',
247 'If "to" is not explicitly passed, extract recipients from headers'
249 is( $email->header_str('Bcc'), undef, 'The Bcc header is unset' );
252 subtest 'is_valid' => sub {
255 is(Koha::Email->is_valid('Fróm <from@example.com>'), 1);
256 is(Koha::Email->is_valid('from@example.com'), 1);
257 is(Koha::Email->is_valid('<from@example.com>'), 1);
258 is(Koha::Email->is_valid('root@localhost'), 1); # See bug 28017
260 is(Koha::Email->is_valid('<from@fróm.com>'), 0); # "In accordance with RFC 822 and its descendants, this module demands that email addresses be ASCII only"
261 isnt(Koha::Email->is_valid('@example.com'), 1);
262 isnt(Koha::Email->is_valid('example.com'), 1);
263 isnt(Koha::Email->is_valid('from'), 1);