* [pmg-devel] [PATCH pmg-api v2 0/2] fix #4211: convert quarantine link mail to template
@ 2024-10-07 10:32 Christoph Heiss
2024-10-07 10:32 ` [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
2024-10-07 10:32 ` [pmg-devel] [PATCH pmg-api v2 2/2] fix #4211: convert quarantine link mail to template Christoph Heiss
0 siblings, 2 replies; 5+ messages in thread
From: Christoph Heiss @ 2024-10-07 10:32 UTC (permalink / raw)
To: pmg-devel
Fixes #4211 [0] by converting the currently hardcoded text for the
quarantine link mail to a proper template, enabling users to write their
own versions.
This series also adds the possibility to specify both a HTML and
plaintext template for PMG::Utils::finalize_report() (or just HTML, for
backwards compatibility) - as suggested in [1].
[0] https://bugzilla.proxmox.com/show_bug.cgi?id=4211
[1] https://lore.proxmox.com/pmg-devel/20241003172625.49655705@rosa.proxmox.com/
Christoph Heiss (2):
utils: allow specifying plain and/or html for finalize_report()
fix #4211: convert quarantine link mail to template
src/Makefile | 2 ++
src/PMG/API2/Quarantine.pm | 21 ++++++++++-----------
src/PMG/Backup.pm | 2 +-
src/PMG/CLI/pmgqm.pm | 5 ++++-
src/PMG/CLI/pmgreport.pm | 5 ++++-
src/PMG/Utils.pm | 26 +++++++++++++++++++++-----
src/templates/quarantine-link.html.tt | 13 +++++++++++++
src/templates/quarantine-link.plain.tt | 5 +++++
8 files changed, 60 insertions(+), 19 deletions(-)
create mode 100644 src/templates/quarantine-link.html.tt
create mode 100644 src/templates/quarantine-link.plain.tt
--
2.46.0
_______________________________________________
pmg-devel mailing list
pmg-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain and/or html for finalize_report()
2024-10-07 10:32 [pmg-devel] [PATCH pmg-api v2 0/2] fix #4211: convert quarantine link mail to template Christoph Heiss
@ 2024-10-07 10:32 ` Christoph Heiss
2024-10-07 16:11 ` Stoiko Ivanov
2024-10-07 10:32 ` [pmg-devel] [PATCH pmg-api v2 2/2] fix #4211: convert quarantine link mail to template Christoph Heiss
1 sibling, 1 reply; 5+ messages in thread
From: Christoph Heiss @ 2024-10-07 10:32 UTC (permalink / raw)
To: pmg-devel
To support both plain-text and HTML emails when sending reports,
PMG::Utils::finalize_report() first needs a small adaption to allow
specifying either only an HTML template or both.
Suggested-by: Stoiko Ivanov <s.ivanov@proxmox.com>
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
src/PMG/Backup.pm | 2 +-
src/PMG/CLI/pmgqm.pm | 5 ++++-
src/PMG/CLI/pmgreport.pm | 5 ++++-
src/PMG/Utils.pm | 26 +++++++++++++++++++++-----
4 files changed, 30 insertions(+), 8 deletions(-)
diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm
index ab7e628..e9c116b 100644
--- a/src/PMG/Backup.pm
+++ b/src/PMG/Backup.pm
@@ -418,7 +418,7 @@ sub send_backup_notification {
my $tt = PMG::Config::get_template_toolkit();
my $mailfrom = "Proxmox Mail Gateway <postmaster>";
- PMG::Utils::finalize_report($tt, 'backup-notification.tt', $vars, $mailfrom, $email);
+ PMG::Utils::finalize_report($tt, $vars, $mailfrom, $email, html => 'backup-notification.tt');
}
diff --git a/src/PMG/CLI/pmgqm.pm b/src/PMG/CLI/pmgqm.pm
index 987ddc9..4bde279 100755
--- a/src/PMG/CLI/pmgqm.pm
+++ b/src/PMG/CLI/pmgqm.pm
@@ -297,7 +297,10 @@ __PACKAGE__->register_method ({
if (!$extern) {
$data->{mailcount} = $mailcount;
my $sendto = $redirect ? $redirect : $creceiver;
- PMG::Utils::finalize_report($tt, $template, $data, $mailfrom, $sendto, $param->{debug});
+ PMG::Utils::finalize_report(
+ $tt, $data, $mailfrom, $sendto,
+ html => $template, debug => $param->{debug}
+ );
}
};
diff --git a/src/PMG/CLI/pmgreport.pm b/src/PMG/CLI/pmgreport.pm
index 3403e44..eb5b0a9 100644
--- a/src/PMG/CLI/pmgreport.pm
+++ b/src/PMG/CLI/pmgreport.pm
@@ -359,7 +359,10 @@ __PACKAGE__->register_method ({
}
my $mailfrom = "Proxmox Mail Gateway <postmaster>";
- PMG::Utils::finalize_report($tt, 'pmgreport.tt', $vars, $mailfrom, $email, $param->{debug});
+ PMG::Utils::finalize_report(
+ $tt, $vars, $mailfrom, $email,
+ html => 'pmgreport.tt', debug => $param->{debug}
+ );
return undef;
}});
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 5d9ded4..32ee3b1 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -1249,11 +1249,11 @@ sub format_uptime {
}
sub finalize_report {
- my ($tt, $template, $data, $mailfrom, $receiver, $debug) = @_;
+ my ($tt, $data, $mailfrom, $receiver, %params) = @_;
my $html = '';
- $tt->process($template, $data, \$html) ||
+ $tt->process($params{html}, $data, \$html) ||
die $tt->error() . "\n";
my $title;
@@ -1263,21 +1263,37 @@ sub finalize_report {
die "unable to extract template title\n";
}
+ $data->{title} //= $title;
+
+ my $plain;
+ if (defined($params{plain})) {
+ $tt->process($params{plain}, $data , \$plain)
+ || die $tt->error() . "\n";
+ }
+
my $top = MIME::Entity->build(
- Type => "multipart/related",
+ Type => defined($plain) ? 'multipart/alternative' : 'multipart/related',
To => $data->{pmail_raw},
From => $mailfrom,
Subject => bencode_header(decode_entities($title)));
+ if (defined($plain)) {
+ $top->attach(
+ Data => $plain,
+ Type => 'text/plain; charset=utf-8',
+ Encoding => '8bit');
+ }
+
$top->attach(
Data => $html,
Type => "text/html",
- Encoding => $debug ? 'binary' : 'quoted-printable');
+ Encoding => $params{debug} ? 'binary' : 'quoted-printable');
- if ($debug) {
+ if ($params{debug}) {
$top->print();
return;
}
+
# we use an empty envelope sender (we don't want to receive NDRs)
PMG::Utils::reinject_local_mail ($top, '', [$receiver], undef, $data->{fqdn});
}
--
2.46.0
_______________________________________________
pmg-devel mailing list
pmg-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* [pmg-devel] [PATCH pmg-api v2 2/2] fix #4211: convert quarantine link mail to template
2024-10-07 10:32 [pmg-devel] [PATCH pmg-api v2 0/2] fix #4211: convert quarantine link mail to template Christoph Heiss
2024-10-07 10:32 ` [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
@ 2024-10-07 10:32 ` Christoph Heiss
1 sibling, 0 replies; 5+ messages in thread
From: Christoph Heiss @ 2024-10-07 10:32 UTC (permalink / raw)
To: pmg-devel
Fixes #4211 [0] by converting the currently hardcoded text for the
quarantine link mail to a proper template, enabling users to write their
own versions.
Pretty straight-forward change. The overall content/wording is kept
pretty much for the plain-text version, the HTML variant is adapted from
that as needed.
[0] https://bugzilla.proxmox.com/show_bug.cgi?id=4211
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
src/Makefile | 2 ++
src/PMG/API2/Quarantine.pm | 21 ++++++++++-----------
src/templates/quarantine-link.html.tt | 13 +++++++++++++
src/templates/quarantine-link.plain.tt | 5 +++++
4 files changed, 30 insertions(+), 11 deletions(-)
create mode 100644 src/templates/quarantine-link.html.tt
create mode 100644 src/templates/quarantine-link.plain.tt
diff --git a/src/Makefile b/src/Makefile
index 8e49a10..c602378 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -44,6 +44,8 @@ TEMPLATES = \
postgresql.conf.in \
pg_hba.conf.in \
backup-notification.tt \
+ quarantine-link.html.tt \
+ quarantine-link.plain.tt \
TEMPLATES_FILES = $(addprefix templates/, ${TEMPLATES})
diff --git a/src/PMG/API2/Quarantine.pm b/src/PMG/API2/Quarantine.pm
index 9301da9..9fd8cc8 100644
--- a/src/PMG/API2/Quarantine.pm
+++ b/src/PMG/API2/Quarantine.pm
@@ -1235,18 +1235,17 @@ my sub send_link_mail {
my $esc_ticket = uri_escape($ticket);
my $link = "$protocol_fqdn_port/quarantine?ticket=${esc_ticket}";
- my $text = "Here is your Link for the Spam Quarantine on $fqdn:\n\n$link\n";
-
- my $mail = MIME::Entity->build(
- Type => "text/plain",
- To => $receiver,
- From => $mailfrom,
- Subject => "Proxmox Mail Gateway - Quarantine Link",
- Data => $text,
+ my $tt = PMG::Config::get_template_toolkit();
+ my $vars = {
+ fqdn => $fqdn,
+ link => $link,
+ };
+
+ PMG::Utils::finalize_report(
+ $tt, $vars, $mailfrom, $receiver,
+ html => 'quarantine-link.html.tt',
+ plain => 'quarantine-link.plain.tt'
);
-
- # we use an empty envelope sender (we don't want to receive NDRs)
- PMG::Utils::reinject_local_mail ($mail, '', [$receiver], undef, $fqdn);
}
__PACKAGE__->register_method ({
diff --git a/src/templates/quarantine-link.html.tt b/src/templates/quarantine-link.html.tt
new file mode 100644
index 0000000..d6fd17e
--- /dev/null
+++ b/src/templates/quarantine-link.html.tt
@@ -0,0 +1,13 @@
+[%- SET title = "Proxmox Mail Gateway - Quarantine Link" -%]
+<html>
+ <head>
+ <title>[% title %]</title>
+ </head>
+ <body>
+ <p>
+ Here is your link for the spam quarantine on <code>[% fqdn %]</code>: <a href='[% link | url %]'>Spam quarantine</a>
+ </p>
+
+ <p>Powered by <a target=_blank href='http://www.proxmox.com'>Proxmox</a>.</p>
+ </body>
+</html>
diff --git a/src/templates/quarantine-link.plain.tt b/src/templates/quarantine-link.plain.tt
new file mode 100644
index 0000000..98517a3
--- /dev/null
+++ b/src/templates/quarantine-link.plain.tt
@@ -0,0 +1,5 @@
+Here is your link for the spam quarantine on [% fqdn %]:
+
+[% link %]
+
+Powered by http://www.proxmox.com
--
2.46.0
_______________________________________________
pmg-devel mailing list
pmg-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain and/or html for finalize_report()
2024-10-07 10:32 ` [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
@ 2024-10-07 16:11 ` Stoiko Ivanov
2024-10-08 9:34 ` Christoph Heiss
0 siblings, 1 reply; 5+ messages in thread
From: Stoiko Ivanov @ 2024-10-07 16:11 UTC (permalink / raw)
To: Christoph Heiss; +Cc: pmg-devel
Thanks for the quick turn-around on this!
on a quick glance - I think I like the approach (and that you converted
all call-sites of finalize_report directly) - however one
pitfall/suggestion inline:
On Mon, 7 Oct 2024 12:32:11 +0200
Christoph Heiss <c.heiss@proxmox.com> wrote:
> To support both plain-text and HTML emails when sending reports,
> PMG::Utils::finalize_report() first needs a small adaption to allow
> specifying either only an HTML template or both.
>
> Suggested-by: Stoiko Ivanov <s.ivanov@proxmox.com>
> Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
> ---
> src/PMG/Backup.pm | 2 +-
> src/PMG/CLI/pmgqm.pm | 5 ++++-
> src/PMG/CLI/pmgreport.pm | 5 ++++-
> src/PMG/Utils.pm | 26 +++++++++++++++++++++-----
> 4 files changed, 30 insertions(+), 8 deletions(-)
>
> diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm
> index ab7e628..e9c116b 100644
> --- a/src/PMG/Backup.pm
> +++ b/src/PMG/Backup.pm
> @@ -418,7 +418,7 @@ sub send_backup_notification {
> my $tt = PMG::Config::get_template_toolkit();
>
> my $mailfrom = "Proxmox Mail Gateway <postmaster>";
> - PMG::Utils::finalize_report($tt, 'backup-notification.tt', $vars, $mailfrom, $email);
> + PMG::Utils::finalize_report($tt, $vars, $mailfrom, $email, html => 'backup-notification.tt');
I think using a hashref here would help to cause less confusion in the
future:
PMG::Utils::finalize_report($tt, $vars, $mailfrom, $email, { html =>
'backup-notification.tt'} ); (but looking through our source am not 100%
sure if we have a strong rule for this)
your line above is syntactically equivalent to:
PMG::Utils::finalize_report($tt, $vars, $mailfrom, $email, "html",
'backup-notification.tt'); see:
https://perldoc.perl.org/perlop#Comma-Operator which I find confusing
(especially if I touch this code in the future)
since finalize_report is called from quite a few places - I think having
those parameters in a hashref would be more robust, but maybe I'm
overlooking something?
_______________________________________________
pmg-devel mailing list
pmg-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain and/or html for finalize_report()
2024-10-07 16:11 ` Stoiko Ivanov
@ 2024-10-08 9:34 ` Christoph Heiss
0 siblings, 0 replies; 5+ messages in thread
From: Christoph Heiss @ 2024-10-08 9:34 UTC (permalink / raw)
To: Stoiko Ivanov; +Cc: pmg-devel
Thanks for the review!
On Mon, Oct 07, 2024 at 06:11:28PM GMT, Stoiko Ivanov wrote:
> Thanks for the quick turn-around on this!
>
> on a quick glance - I think I like the approach (and that you converted
> all call-sites of finalize_report directly) - however one
> pitfall/suggestion inline:
>
> On Mon, 7 Oct 2024 12:32:11 +0200
> Christoph Heiss <c.heiss@proxmox.com> wrote:
[..]
> > --- a/src/PMG/Backup.pm
> > +++ b/src/PMG/Backup.pm
> > @@ -418,7 +418,7 @@ sub send_backup_notification {
> > my $tt = PMG::Config::get_template_toolkit();
> >
> > my $mailfrom = "Proxmox Mail Gateway <postmaster>";
> > - PMG::Utils::finalize_report($tt, 'backup-notification.tt', $vars, $mailfrom, $email);
> > + PMG::Utils::finalize_report($tt, $vars, $mailfrom, $email, html => 'backup-notification.tt');
> I think using a hashref here would help to cause less confusion in the
> future:
> PMG::Utils::finalize_report($tt, $vars, $mailfrom, $email, { html =>
> 'backup-notification.tt'} ); (but looking through our source am not 100%
> sure if we have a strong rule for this)
Yeah, I think we use both variants pretty equally all around ..
>
> your line above is syntactically equivalent to:
> PMG::Utils::finalize_report($tt, $vars, $mailfrom, $email, "html",
> 'backup-notification.tt'); see:
> https://perldoc.perl.org/perlop#Comma-Operator which I find confusing
> (especially if I touch this code in the future)
>
> since finalize_report is called from quite a few places - I think having
> those parameters in a hashref would be more robust, but maybe I'm
> overlooking something?
>
No, using a hashref would work too I guess! I also don't have a
particular strong opinion on that, so it's fine. As you say, it's a lot
clearer in any case.
I'll work through it and send a v3 soon.
_______________________________________________
pmg-devel mailing list
pmg-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-10-08 9:34 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-07 10:32 [pmg-devel] [PATCH pmg-api v2 0/2] fix #4211: convert quarantine link mail to template Christoph Heiss
2024-10-07 10:32 ` [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
2024-10-07 16:11 ` Stoiko Ivanov
2024-10-08 9:34 ` Christoph Heiss
2024-10-07 10:32 ` [pmg-devel] [PATCH pmg-api v2 2/2] fix #4211: convert quarantine link mail to template Christoph Heiss
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox