View | Details | Raw Unified | Return to bug 22343
Collapse All | Expand All

(-)a/Koha/Email.pm (-37 / +82 lines)
Lines 1-4 Link Here
1
package Koha::Email;
2
1
# Copyright 2014 Catalyst
3
# Copyright 2014 Catalyst
4
#           2020 Theke Solutions
2
#
5
#
3
# This file is part of Koha.
6
# This file is part of Koha.
4
#
7
#
Lines 15-78 Link Here
15
# You should have received a copy of the GNU General Public License
18
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
20
18
package Koha::Email;
19
20
use Modern::Perl;
21
use Modern::Perl;
22
21
use Email::Valid;
23
use Email::Valid;
22
use Email::MessageID;
24
use Email::MessageID;
23
25
24
use base qw(Class::Accessor);
25
use C4::Context;
26
use C4::Context;
26
27
27
__PACKAGE__->mk_accessors(qw( ));
28
use base qw( Email::Stuffer );
28
29
29
=head1 NAME
30
=head1 NAME
30
31
31
Koha::Email
32
Koha::Email - A wrapper around Email::Stuffer
33
34
=head1 API
35
36
=head2 Class methods
37
38
=head3 create
39
40
    my $email = Koha::Email->create(
41
        {
42
          [ text_body   => $text_message,
43
            html_body   => $html_message,
44
            body_params => $body_params ]
45
            from        => $from,
46
            to          => $to,
47
            cc          => $cc,
48
            bcc         => $bcc,
49
            replyto     => $replyto,
50
            sender      => $sender,
51
            subject     => $subject,
52
        }
53
    );
54
55
This method creates a new Email::Stuffer object taking Koha specific configurations
56
into account.
32
57
33
=head1 SYNOPSIS
58
The encoding defaults to utf-8. It can be set as part of the body_params hashref. See
59
I<Email::Stuffer> and I<Email::MIME> for more details on the available options.
34
60
35
  use Koha::Email;
61
Parameters:
36
  my $email = Koha::Email->new();
62
 - I<from> defaults to the value of the I<KohaAdminEmailAddress> system preference
37
  my %mail = $email->create_message_headers({ to => $to_address, from => $from_address,
63
 - The I<SendAllEmailsTo> system preference overloads the I<to>, I<cc> and I<bcc> parameters
38
                                             replyto => $replyto });
64
 - I<replyto> defaults to the value of the I<ReplytoDefault> system preference
65
 - I<sender> defaults to the value of the I<ReturnpathDefault> system preference
39
66
40
=head1 FUNCTIONS
67
Both I<text_body> and I<html_body> can be set later. I<body_params> will be passed if present
68
to the constructor.
41
69
42
=cut
70
=cut
43
71
44
sub create_message_headers {
72
sub create {
45
    my $self   = shift;
73
    my ( $self, $params ) = @_;
46
    my $params = shift;
74
47
    $params->{from} ||= C4::Context->preference('KohaAdminEmailAddress');
75
    my $from    = $params->{from}    // C4::Context->preference('KohaAdminEmailAddress');
48
    $params->{charset} ||= 'utf8';
76
    my $subject = $params->{subject} // '';
49
    my %mail = (
77
50
        To      => $params->{to},
78
    my $args = {
51
        From    => $params->{from},
79
        from    => $from,
52
        charset => $params->{charset}
80
        subject => $subject,
53
    );
81
    };
82
83
    $params->{replyto} ||= C4::Context->preference('ReplytoDefault')
84
        if C4::Context->preference('ReplytoDefault');
85
86
    $params->{sender} ||= C4::Context->preference('ReturnpathDefault')
87
        if C4::Context->preference('ReturnpathDefault') ;
88
54
89
55
    if (C4::Context->preference('SendAllEmailsTo') && Email::Valid->address(C4::Context->preference('SendAllEmailsTo'))) {
90
    if (   C4::Context->preference('SendAllEmailsTo')
56
        $mail{'To'} = C4::Context->preference('SendAllEmailsTo');
91
        && Email::Valid->address( C4::Context->preference('SendAllEmailsTo') ) )
92
    {
93
        $args->{to} = C4::Context->preference('SendAllEmailsTo');
57
    }
94
    }
58
    else {
95
    else {
59
        $mail{'Cc'}  = $params->{cc}  if exists $params->{cc};
96
        $args->{to}  = $params->{to};
60
        $mail{'Bcc'} = $params->{bcc} if exists $params->{bcc};
97
        $args->{cc}  = $params->{cc}
98
            if exists $params->{cc};
99
        $args->{bcc} = $params->{bcc}
100
            if exists $params->{bcc};
61
    }
101
    }
62
102
63
    if ( C4::Context->preference('ReplytoDefault') ) {
103
    my $email = $self->SUPER::new( $args );
64
        $params->{replyto} ||= C4::Context->preference('ReplytoDefault');
104
105
    $email->header( 'ReplyTo', $params->{replyto} )
106
        if $params->{replyto};
107
108
    $email->header( 'Sender'       => $params->{sender} );
109
    $email->header( 'Content-Type' => $params->{contenttype} ) if $params->{contenttype};
110
    $email->header( 'X-Mailer'     => "Koha" );
111
    $email->header( 'Message-ID'   => Email::MessageID->new->in_brackets );
112
113
    if ( $params->{text_body} ) {
114
        $email->text_body( $params->{text_body}, %{ $params->{body_params} } );
65
    }
115
    }
66
    if ( C4::Context->preference('ReturnpathDefault') ) {
116
    elsif ( $params->{html_body} ) {
67
        $params->{sender} ||= C4::Context->preference('ReturnpathDefault');
117
        $email->html_body( $params->{html_body}, %{ $params->{body_params} } );
68
    }
118
    }
69
    $mail{'Reply-to'}     = $params->{replyto}     if $params->{replyto};
119
70
    $mail{'Sender'}       = $params->{sender}      if $params->{sender};
120
    return $email;
71
    $mail{'Message'}      = $params->{message}     if $params->{message};
72
    $mail{'Subject'}      = $params->{subject}     if $params->{subject};
73
    $mail{'Content-Type'} = $params->{contenttype} if $params->{contenttype};
74
    $mail{'X-Mailer'}     = "Koha";
75
    $mail{'Message-ID'}   = Email::MessageID->new->in_brackets;
76
    return %mail;
77
}
121
}
122
78
1;
123
1;
(-)a/cpanfile (+1 lines)
Lines 37-42 requires 'Digest::SHA', '5.43'; Link Here
37
requires 'Email::Date', '1.103';
37
requires 'Email::Date', '1.103';
38
requires 'Email::MessageID', '1.406';
38
requires 'Email::MessageID', '1.406';
39
requires 'Email::Sender', '1.300030';
39
requires 'Email::Sender', '1.300030';
40
requires 'Email::Stuffer', '0.014';
40
requires 'Email::Valid', '0.190';
41
requires 'Email::Valid', '0.190';
41
requires 'Exception::Class', '1.38';
42
requires 'Exception::Class', '1.38';
42
requires 'File::Slurp', '9999.13';
43
requires 'File::Slurp', '9999.13';
(-)a/t/Koha/Email.t (+86 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
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.
9
#
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.
14
#
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>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
22
use t::lib::Mocks;
23
24
use_ok('Koha::Email');
25
26
subtest 'create() tests' => sub {
27
28
    plan tests => 23;
29
30
    t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
31
32
    my $html_body = '<h1>Title</h1><p>Message</p>';
33
    my $text_body = "#Title: Message";
34
35
    my $email = Koha::Email->create(
36
        {
37
            from        => 'from@example.com',
38
            to          => 'to@example.com',
39
            cc          => 'cc@example.com',
40
            bcc         => 'bcc@example.com',
41
            replyto     => 'replyto@example.com',
42
            sender      => 'sender@example.com',
43
            subject     => 'Some subject',
44
            html_body   => $html_body,
45
            body_params => { charset => 'iso-8859-1' },
46
        }
47
    );
48
49
    is( $email->email->header('From'), 'from@example.com', 'Value set correctly' );
50
    is( $email->email->header('To'), 'to@example.com', 'Value set correctly' );
51
    is( $email->email->header('Cc'), 'cc@example.com', 'Value set correctly' );
52
    is( $email->email->header('Bcc'), 'bcc@example.com', 'Value set correctly' );
53
    is( $email->email->header('ReplyTo'), 'replyto@example.com', 'Value set correctly' );
54
    is( $email->email->header('Sender'), 'sender@example.com', 'Value set correctly' );
55
    is( $email->email->header('Subject'), 'Some subject', 'Value set correctly' );
56
    is( $email->email->header('X-Mailer'), 'Koha', 'Value set correctly' );
57
    is( $email->email->body, $html_body, "Body set correctly" );
58
    like( $email->email->content_type, qr|text/html|, "Content type set correctly");
59
    like( $email->email->content_type, qr|charset="iso-8859-1"|, "Charset set correctly");
60
    like( $email->email->header('Message-ID'), qr/\<.*@.*\>/, 'Value set correctly' );
61
62
    t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'catchall@example.com' );
63
    t::lib::Mocks::mock_preference( 'ReplytoDefault', 'replytodefault@example.com' );
64
    t::lib::Mocks::mock_preference( 'ReturnpathDefault', 'returnpathdefault@example.com' );
65
    t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'kohaadminemailaddress@example.com' );
66
67
    $email = Koha::Email->create(
68
        {
69
            to        => 'to@example.com',
70
            cc        => 'cc@example.com',
71
            bcc       => 'bcc@example.com',
72
            text_body => $text_body,
73
        }
74
    );
75
76
    is( $email->email->header('From'), 'kohaadminemailaddress@example.com', 'KohaAdminEmailAddress is picked when no from passed' );
77
    is( $email->email->header('To'), 'catchall@example.com', 'SendAllEmailsTo overloads any address' );
78
    is( $email->email->header('Cc'), undef, 'SendAllEmailsTo overloads any address' );
79
    is( $email->email->header('Bcc'), undef, 'SendAllEmailsTo overloads any address' );
80
    is( $email->email->header('ReplyTo'), 'replytodefault@example.com', 'ReplytoDefault picked when replyto not passed' );
81
    is( $email->email->header('Sender'), 'returnpathdefault@example.com', 'ReturnpathDefault picked when sender not passed' );
82
    is( $email->email->body, $text_body, "Body set correctly" );
83
    like( $email->email->content_type, qr|text/plain|, "Content type set correctly");
84
    like( $email->email->content_type, qr|charset="utf-8"|, "Charset set correctly");
85
};
86
(-)a/t/Koha_Email.t (-17 lines)
Lines 1-16 Link Here
1
use Modern::Perl;
2
3
use t::lib::Mocks;
4
use Test::More tests => 4;                      # last test to print
5
6
use_ok('Koha::Email');
7
8
my $from = 'chrisc@catalyst.net.nz';
9
t::lib::Mocks::mock_preference('ReplytoDefault', $from);
10
t::lib::Mocks::mock_preference('ReturnpathDefault', $from);
11
12
13
14
ok( my $email = Koha::Email->new(), 'Create a Koha::Email Object');
15
ok( my %mail = $email->create_message_headers({from => $from}),'Set headers');
16
is ($mail{'From'}, $from, 'Set correctly');
17
- 

Return to bug 22343