Bugzilla – Attachment 108649 Details for
Bug 22343
Add configuration options for SMTP servers
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 22343: Revamped Koha::Email class
Bug-22343-Revamped-KohaEmail-class.patch (text/plain), 11.29 KB, created by
Kyle M Hall (khall)
on 2020-08-19 15:16:41 UTC
(
hide
)
Description:
Bug 22343: Revamped Koha::Email class
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2020-08-19 15:16:41 UTC
Size:
11.29 KB
patch
obsolete
>From 53708406decd02806b4b9fe4e08aa641ef2919d6 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Thu, 6 Aug 2020 12:38:45 -0300 >Subject: [PATCH] Bug 22343: Revamped Koha::Email class > >This patch completely rewrites the Koha::Email class, inheriting from >Email::Stuffer. The latter suits well the use by Email::Sender, which is >to replace Mail::Sendmail on this patchset. > >To test: >1. Apply this patch >2. Run: > $ kshell > k$ prove t/Koha/Email.t >=> SUCCESS: Tests pass! >3. Verify all conditional codepaths are covered >4. Sign off :-D > >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> > >Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> >--- > Koha/Email.pm | 119 ++++++++++++++++++++++++++++++++++--------------- > cpanfile | 1 + > t/Koha/Email.t | 85 +++++++++++++++++++++++++++++++++++ > t/Koha_Email.t | 16 ------- > 4 files changed, 168 insertions(+), 53 deletions(-) > create mode 100644 t/Koha/Email.t > delete mode 100755 t/Koha_Email.t > >diff --git a/Koha/Email.pm b/Koha/Email.pm >index f1e825e526..037b783bc3 100644 >--- a/Koha/Email.pm >+++ b/Koha/Email.pm >@@ -1,4 +1,7 @@ >+package Koha::Email; >+ > # Copyright 2014 Catalyst >+# 2020 Theke Solutions > # > # This file is part of Koha. > # >@@ -15,64 +18,106 @@ > # You should have received a copy of the GNU General Public License > # along with Koha; if not, see <http://www.gnu.org/licenses>. > >-package Koha::Email; >- > use Modern::Perl; >+ > use Email::Valid; > use Email::MessageID; > >-use base qw(Class::Accessor); > use C4::Context; > >-__PACKAGE__->mk_accessors(qw( )); >+use base qw( Email::Stuffer ); > > =head1 NAME > >-Koha::Email >+Koha::Email - A wrapper around Email::Stuffer >+ >+=head1 API >+ >+=head2 Class methods >+ >+=head3 create >+ >+ my $email = Koha::Email->create( >+ { >+ [ text_body => $text_message, >+ html_body => $html_message, >+ body_params => $body_params ] >+ from => $from, >+ to => $to, >+ cc => $cc, >+ bcc => $bcc, >+ replyto => $replyto, >+ sender => $sender, >+ subject => $subject, >+ } >+ ); >+ >+This method creates a new Email::Stuffer object taking Koha specific configurations >+into account. > >-=head1 SYNOPSIS >+The encoding defaults to utf-8. It can be set as part of the body_params hashref. See >+I<Email::Stuffer> and I<Email::MIME> for more details on the available options. > >- use Koha::Email; >- my $email = Koha::Email->new(); >- my %mail = $email->create_message_headers({ to => $to_address, from => $from_address, >- replyto => $replyto }); >+Parameters: >+ - I<from> defaults to the value of the I<KohaAdminEmailAddress> system preference >+ - The I<SendAllEmailsTo> system preference overloads the I<to>, I<cc> and I<bcc> parameters >+ - I<replyto> defaults to the value of the I<ReplytoDefault> system preference >+ - I<sender> defaults to the value of the I<ReturnpathDefault> system preference > >-=head1 FUNCTIONS >+Both I<text_body> and I<html_body> can be set later. I<body_params> will be passed if present >+to the constructor. > > =cut > >-sub create_message_headers { >- my $self = shift; >- my $params = shift; >- $params->{from} ||= C4::Context->preference('KohaAdminEmailAddress'); >- $params->{charset} ||= 'utf8'; >- my %mail = ( >- To => $params->{to}, >- From => $params->{from}, >- charset => $params->{charset} >- ); >+sub create { >+ my ( $self, $params ) = @_; >+ >+ my $from = $params->{from} // C4::Context->preference('KohaAdminEmailAddress'); >+ my $subject = $params->{subject} // ''; >+ >+ my $args = { >+ from => $from, >+ subject => $subject, >+ }; >+ >+ $params->{replyto} ||= C4::Context->preference('ReplytoDefault') >+ if C4::Context->preference('ReplytoDefault'); >+ >+ $params->{sender} ||= C4::Context->preference('ReturnpathDefault') >+ if C4::Context->preference('ReturnpathDefault') ; >+ > >- if (C4::Context->preference('SendAllEmailsTo') && Email::Valid->address(C4::Context->preference('SendAllEmailsTo'))) { >- $mail{'To'} = C4::Context->preference('SendAllEmailsTo'); >+ if ( C4::Context->preference('SendAllEmailsTo') >+ && Email::Valid->address( C4::Context->preference('SendAllEmailsTo') ) ) >+ { >+ $args->{to} = C4::Context->preference('SendAllEmailsTo'); > } > else { >- $mail{'Cc'} = $params->{cc} if exists $params->{cc}; >- $mail{'Bcc'} = $params->{bcc} if exists $params->{bcc}; >+ $args->{to} = $params->{to}; >+ $args->{cc} = $params->{cc} >+ if exists $params->{cc}; >+ $args->{bcc} = $params->{bcc} >+ if exists $params->{bcc}; > } > >- if ( C4::Context->preference('ReplytoDefault') ) { >- $params->{replyto} ||= C4::Context->preference('ReplytoDefault'); >+ my $email = $self->SUPER::new( $args ); >+ >+ $email->header( 'ReplyTo', $params->{replyto} ) >+ if $params->{replyto}; >+ >+ $email->header( 'Sender' => $params->{sender} ); >+ $email->header( 'Content-Type' => $params->{contenttype} ) if $params->{contenttype}; >+ $email->header( 'X-Mailer' => "Koha" ); >+ $email->header( 'Message-ID' => Email::MessageID->new->in_brackets ); >+ >+ if ( $params->{text_body} ) { >+ $email->text_body( $params->{text_body}, %{ $params->{body_params} } ); > } >- if ( C4::Context->preference('ReturnpathDefault') ) { >- $params->{sender} ||= C4::Context->preference('ReturnpathDefault'); >+ elsif ( $params->{html_body} ) { >+ $email->html_body( $params->{html_body}, %{ $params->{body_params} } ); > } >- $mail{'Reply-to'} = $params->{replyto} if $params->{replyto}; >- $mail{'Sender'} = $params->{sender} if $params->{sender}; >- $mail{'Message'} = $params->{message} if $params->{message}; >- $mail{'Subject'} = $params->{subject} if $params->{subject}; >- $mail{'Content-Type'} = $params->{contenttype} if $params->{contenttype}; >- $mail{'X-Mailer'} = "Koha"; >- $mail{'Message-ID'} = Email::MessageID->new->in_brackets; >- return %mail; >+ >+ return $email; > } >+ > 1; >diff --git a/cpanfile b/cpanfile >index 05ae2aa495..d5958efa33 100644 >--- a/cpanfile >+++ b/cpanfile >@@ -37,6 +37,7 @@ requires 'Digest::SHA', '5.43'; > requires 'Email::Date', '1.103'; > requires 'Email::MessageID', '1.406'; > requires 'Email::Sender', '1.300030'; >+requires 'Email::Stuffer', '0.014'; > requires 'Email::Valid', '0.190'; > requires 'Exception::Class', '1.38'; > requires 'File::Slurp', '9999.13'; >diff --git a/t/Koha/Email.t b/t/Koha/Email.t >new file mode 100644 >index 0000000000..d7dfef1b6f >--- /dev/null >+++ b/t/Koha/Email.t >@@ -0,0 +1,85 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Test::More tests => 2; >+ >+use t::lib::Mocks; >+ >+use_ok('Koha::Email'); >+ >+subtest 'create() tests' => sub { >+ >+ plan tests => 23; >+ >+ t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef ); >+ >+ my $html_body = '<h1>Title</h1><p>Message</p>'; >+ my $text_body = "#Title: Message"; >+ >+ my $email = Koha::Email->create( >+ { >+ from => 'from@example.com', >+ to => 'to@example.com', >+ cc => 'cc@example.com', >+ bcc => 'bcc@example.com', >+ replyto => 'replyto@example.com', >+ sender => 'sender@example.com', >+ subject => 'Some subject', >+ html_body => $html_body, >+ body_params => { charset => 'iso-8859-1' }, >+ } >+ ); >+ >+ is( $email->email->header('From'), 'from@example.com', 'Value set correctly' ); >+ is( $email->email->header('To'), 'to@example.com', 'Value set correctly' ); >+ is( $email->email->header('Cc'), 'cc@example.com', 'Value set correctly' ); >+ is( $email->email->header('Bcc'), 'bcc@example.com', 'Value set correctly' ); >+ is( $email->email->header('ReplyTo'), 'replyto@example.com', 'Value set correctly' ); >+ is( $email->email->header('Sender'), 'sender@example.com', 'Value set correctly' ); >+ is( $email->email->header('Subject'), 'Some subject', 'Value set correctly' ); >+ is( $email->email->header('X-Mailer'), 'Koha', 'Value set correctly' ); >+ is( $email->email->body, $html_body, "Body set correctly" ); >+ like( $email->email->content_type, qr|text/html|, "Content type set correctly"); >+ like( $email->email->content_type, qr|charset="iso-8859-1"|, "Charset set correctly"); >+ like( $email->email->header('Message-ID'), qr/\<.*@.*\>/, 'Value set correctly' ); >+ >+ t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'catchall@example.com' ); >+ t::lib::Mocks::mock_preference( 'ReplytoDefault', 'replytodefault@example.com' ); >+ t::lib::Mocks::mock_preference( 'ReturnpathDefault', 'returnpathdefault@example.com' ); >+ t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'kohaadminemailaddress@example.com' ); >+ >+ $email = Koha::Email->create( >+ { >+ to => 'to@example.com', >+ cc => 'cc@example.com', >+ bcc => 'bcc@example.com', >+ text_body => $text_body, >+ } >+ ); >+ >+ is( $email->email->header('From'), 'kohaadminemailaddress@example.com', 'KohaAdminEmailAddress is picked when no from passed' ); >+ is( $email->email->header('To'), 'catchall@example.com', 'SendAllEmailsTo overloads any address' ); >+ is( $email->email->header('Cc'), undef, 'SendAllEmailsTo overloads any address' ); >+ is( $email->email->header('Bcc'), undef, 'SendAllEmailsTo overloads any address' ); >+ is( $email->email->header('ReplyTo'), 'replytodefault@example.com', 'ReplytoDefault picked when replyto not passed' ); >+ is( $email->email->header('Sender'), 'returnpathdefault@example.com', 'ReturnpathDefault picked when sender not passed' ); >+ is( $email->email->body, $text_body, "Body set correctly" ); >+ like( $email->email->content_type, qr|text/plain|, "Content type set correctly"); >+ like( $email->email->content_type, qr|charset="utf-8"|, "Charset set correctly"); >+}; >diff --git a/t/Koha_Email.t b/t/Koha_Email.t >deleted file mode 100755 >index 93dd55bd6d..0000000000 >--- a/t/Koha_Email.t >+++ /dev/null >@@ -1,16 +0,0 @@ >-use Modern::Perl; >- >-use t::lib::Mocks; >-use Test::More tests => 4; # last test to print >- >-use_ok('Koha::Email'); >- >-my $from = 'chrisc@catalyst.net.nz'; >-t::lib::Mocks::mock_preference('ReplytoDefault', $from); >-t::lib::Mocks::mock_preference('ReturnpathDefault', $from); >- >- >- >-ok( my $email = Koha::Email->new(), 'Create a Koha::Email Object'); >-ok( my %mail = $email->create_message_headers({from => $from}),'Set headers'); >-is ($mail{'From'}, $from, 'Set correctly'); >-- >2.24.1 (Apple Git-126)
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 22343
:
107598
|
107599
|
107600
|
107601
|
107602
|
107603
|
107604
|
107605
|
107987
|
107988
|
107989
|
107990
|
107991
|
107992
|
107993
|
107994
|
107995
|
107996
|
107997
|
107998
|
107999
|
108009
|
108107
|
108108
|
108109
|
108110
|
108111
|
108112
|
108113
|
108114
|
108115
|
108116
|
108117
|
108118
|
108119
|
108131
|
108132
|
108133
|
108134
|
108135
|
108136
|
108137
|
108138
|
108139
|
108140
|
108141
|
108142
|
108143
|
108529
|
108537
|
108538
|
108539
|
108540
|
108541
|
108542
|
108543
|
108544
|
108545
|
108546
|
108547
|
108548
|
108549
|
108609
|
108638
|
108642
|
108643
|
108644
|
108645
|
108646
|
108647
|
108648
|
108649
|
108650
|
108651
|
108652
|
108653
|
108654
|
108655
|
108656
|
108715
|
108716
|
109003
|
109004
|
109005
|
109006
|
109007
|
109008
|
109009
|
109010
|
109011
|
109012
|
109013
|
109014
|
109015
|
109016
|
109017
|
109018
|
109155
|
109156
|
109157
|
109158
|
109159
|
109160
|
109161
|
109162
|
109163
|
109164
|
109165
|
109166
|
109167
|
109168
|
109169
|
109170
|
109601
|
109602
|
109603
|
109604
|
109605
|
109606
|
109607
|
109608
|
109609
|
109610
|
109611
|
109612
|
109613
|
109614
|
109615
|
109616
|
109723
|
109724
|
109725
|
109726
|
109727
|
109728
|
109729
|
109730
|
109731
|
109732
|
109733
|
109734
|
109735
|
109736
|
109737
|
109738
|
109739
|
109740
|
109787
|
109788
|
111048
|
111087
|
111088
|
111089
|
111090
|
111154
|
111165
|
111166
|
111175
|
111176
|
111213
|
111215
|
111216
|
111442
|
111482
|
113240
|
113827
|
113847