From 744e9f456473c171fdfa48a485c896d29e7b7fb2 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 16 Sep 2024 10:24:11 +0100 Subject: [PATCH] Bug 37918: Add email_header template toolkit filter This patch adds a new email_header TT filter to properly encode strings according to RFC2047 as per the email specification. This allows developers to set default subjects in mailto links with the correct encoding. --- Koha/Template/Plugin/EmailHeader.pm | 59 +++++++++++++++++++++++++++++ cpanfile | 2 + t/Koha_Template_Email_Header.t | 54 ++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 Koha/Template/Plugin/EmailHeader.pm create mode 100644 t/Koha_Template_Email_Header.t diff --git a/Koha/Template/Plugin/EmailHeader.pm b/Koha/Template/Plugin/EmailHeader.pm new file mode 100644 index 00000000000..8552bd4f3e5 --- /dev/null +++ b/Koha/Template/Plugin/EmailHeader.pm @@ -0,0 +1,59 @@ +package Koha::Template::Plugin::EmailHeader; + +# Copyright PTFS Europe 2024 + +# 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 . + +=head1 NAME + +Koha::Template::Plugin::EmailHeader - Template Toolkit filter to encode email headers as per RFC2047 + +=head1 SYNOPSIS + + [% USE EmailHeader %] + [% header | email_header %] + +=head1 DESCRIPTION + +This plugin provides a Template Toolkit filter `email_header` that encodes +email headers according to RFC2047 using Encode::MIME::Header. + +=cut + +use Modern::Perl; +use Encode; +use Encode::MIME::Header; + +use base qw( Template::Plugin::Filter ); + +our $DYNAMIC = 1; + +=head1 FUNCTIONS + +=head2 filter + + [% subject | email_header %] + +Filters the given text as per RFC2047. + +=cut + +sub filter { + my ( $self, $text, $args, $config ) = @_; + return encode('MIME-Header', $text); +} + +1; diff --git a/cpanfile b/cpanfile index bf2db1d6523..fefd97029df 100644 --- a/cpanfile +++ b/cpanfile @@ -42,6 +42,7 @@ requires 'Email::Date', '1.103'; requires 'Email::MessageID', '1.406'; requires 'Email::Sender', '1.300030'; requires 'Email::Stuffer', '0.014'; +requires 'Encode::MIME::Header', '2.17'; requires 'Exception::Class', '1.38'; requires 'File::Slurp', '9999.13'; requires 'Font::TTF', '0.45'; @@ -113,6 +114,7 @@ requires 'Test', '1.25'; requires 'Test::Harness', '2.56'; requires 'Test::MockModule', '0.05'; requires 'Test::More', '1.302073'; +requires 'Test::More::UTF8', '0.05'; requires 'Text::Bidi', '0.03'; requires 'Text::CSV', '0.01'; requires 'Text::CSV::Encoded', '0.09'; diff --git a/t/Koha_Template_Email_Header.t b/t/Koha_Template_Email_Header.t new file mode 100644 index 00000000000..3f6e21a9558 --- /dev/null +++ b/t/Koha_Template_Email_Header.t @@ -0,0 +1,54 @@ +#!/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 . + +use utf8; +use Modern::Perl; + +use Test::More tests => 4; +use Test::More::UTF8; + +use Encode qw(encode); +use Encode::MIME::Header; + +use open qw( :std :encoding(UTF-8) ); + +use_ok( 'Koha::Template::Plugin::EmailHeader' ); + +my $filter = Koha::Template::Plugin::EmailHeader->new(); + +# Test cases with plain text and non-ASCII characters +my @tests = ( + { + input => 'Hello World', + expected => encode('MIME-Header', 'Hello World'), # ASCII text doesn't need encoding + }, + { + input => 'こんにちは', # Japanese text (Hello) + expected => encode('MIME-Header', 'こんにちは'), + }, + { + input => 'Café au lait', # Contains non-ASCII character (é) + expected => encode('MIME-Header', 'Café au lait'), + }, +); + +# Test each case +for my $test (@tests) { + is($filter->filter($test->{input}), $test->{expected}, "Correctly encoded: '$test->{input} -> $test->{expected}'"); +} + +done_testing(); -- 2.46.0