* [pmg-devel] [PATCH pmg-api v4 1/2] utils: allow specifying plain and/or html for finalize_report()
2024-10-14 18:05 [pmg-devel] [PATCH pmg-api/docs v4] fix #4211: convert quarantine link mail to template Stoiko Ivanov
@ 2024-10-14 18:05 ` Stoiko Ivanov
2024-10-14 18:05 ` [pmg-devel] [PATCH pmg-api v4 2/2] fix #4211: convert quarantine link mail to template Stoiko Ivanov
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Stoiko Ivanov @ 2024-10-14 18:05 UTC (permalink / raw)
To: pmg-devel
To support both plain-text and HTML emails when sending reports,
PMG::Utils::finalize_report() now checks if a template has a
plain-text equivalent - (`.plain.tt` instead of `.tt` as suffix).
if either template file is empty (or non-existant) the corresponding
part is simply left-out.
This should enable admins top optionally send plain-text only reports,
or multipart mails with both text/plain and text/html, while not
changing the current behavior
Co-developed-by: Christoph Heiss <c.heiss@proxmox.com>
Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
src/PMG/Backup.pm | 2 +-
src/PMG/CLI/pmgqm.pm | 6 ++--
src/PMG/CLI/pmgreport.pm | 2 +-
src/PMG/Utils.pm | 77 +++++++++++++++++++++++++++++++++-------
4 files changed, 70 insertions(+), 17 deletions(-)
diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm
index ab7e628..c820d6b 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, 'backup-notification', $vars, $mailfrom, $email);
}
diff --git a/src/PMG/CLI/pmgqm.pm b/src/PMG/CLI/pmgqm.pm
index 987ddc9..7016161 100755
--- a/src/PMG/CLI/pmgqm.pm
+++ b/src/PMG/CLI/pmgqm.pm
@@ -261,14 +261,14 @@ __PACKAGE__->register_method ({
my $domains = PVE::INotify::read_file('domains');
my $domainregex = PMG::Utils::domain_regex([keys %$domains]);
- my $template = "spamreport-${reportstyle}.tt";
+ my $template = "spamreport-${reportstyle}";
my $found = 0;
foreach my $path (@$PMG::Config::tt_include_path) {
- if (-f "$path/$template") { $found = 1; last; }
+ if (-f "$path/$template.tt") { $found = 1; last; }
}
if (!$found) {
warn "unable to find template '$template' - using default\n";
- $template = "spamreport-verbose.tt";
+ $template = "spamreport-verbose";
}
my $sth = $dbh->prepare(
diff --git a/src/PMG/CLI/pmgreport.pm b/src/PMG/CLI/pmgreport.pm
index 3403e44..99adfd2 100644
--- a/src/PMG/CLI/pmgreport.pm
+++ b/src/PMG/CLI/pmgreport.pm
@@ -359,7 +359,7 @@ __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, 'pmgreport', $vars, $mailfrom, $email, $param->{debug});
return undef;
}});
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 5d9ded4..9069f81 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -1248,32 +1248,85 @@ sub format_uptime {
}
}
+# Sends a report to the given receiver, building the body from templates.
+# if either html or plain template is empty that part is not added.
+# The subject of the mail is derived from the title tag of the HTML template, with a fallback to
+# the value after `subject:` on a line of its own in the plaintext template.
+#
+# The resulting mail is then reinjected with an empty envelope sender.
+#
+# Parameters:
+# * `tt` - The template toolkit instance to use
+# * `template_base` - base name of the template. suffixed with 'tt' yields the HTML template.
+# suffixed with 'plain.tt' yields the plain text template
+# * `data` - Template variables to pass to the template processor
+# * `mailfrom` - Sender address
+# * `receiver` - Recipient address
+# * `debug` - whether to enable debug mode, resulting mail is only printed, not reinjected
sub finalize_report {
- my ($tt, $template, $data, $mailfrom, $receiver, $debug) = @_;
+ my ($tt, $template_base, $data, $mailfrom, $receiver, $debug) = @_;
+
+ my $html_template;
+ my $html_path;
+ for my $path (@$PMG::Config::tt_include_path) {
+ if (-f "$path/$template_base.tt") {
+ $html_template = "$template_base.tt";
+ $html_path = "$path/$template_base.tt";
+ last;
+ }
+ }
my $html = '';
- $tt->process($template, $data, \$html) ||
- die $tt->error() . "\n";
-
+ if (defined($html_template) && -s $html_path) {
+ $tt->process($html_template, $data, \$html) ||
+ die $tt->error() . "\n";
+ }
my $title;
if ($html =~ m|^\s*<title>(.*)</title>|m) {
$title = $1;
- } else {
- die "unable to extract template title\n";
}
+ my $plain_template;
+ my $plain_path;
+ for my $path (@$PMG::Config::tt_include_path) {
+ if (-f "$path/$template_base.plain.tt") {
+ $plain_template = "$template_base.plain.tt";
+ $plain_path = "$path/$template_base.plain.tt";
+ last;
+ }
+ }
+
+ my $plaintext = '';
+ if (defined($plain_template) && -s $plain_path) {
+ $tt->process($plain_template, $data, \$plaintext) ||
+ die $tt->error() . "\n";
+ }
+
+ if ($plaintext =~ s/^subject: (.+)$//m) {
+ $title //= $1;
+ }
+
+ die "unable to extract template title\n" if !defined($title);
+
my $top = MIME::Entity->build(
- Type => "multipart/related",
+ Type => ($html && $plaintext) ? 'multipart/alternative' : 'multipart/related',
To => $data->{pmail_raw},
From => $mailfrom,
Subject => bencode_header(decode_entities($title)));
- $top->attach(
- Data => $html,
- Type => "text/html",
- Encoding => $debug ? 'binary' : 'quoted-printable');
-
+ if ($html) {
+ $top->attach(
+ Data => $html,
+ Type => "text/html",
+ Encoding => $debug ? 'binary' : 'quoted-printable');
+ }
+ if ($plaintext) {
+ $top->attach(
+ Data => $plaintext,
+ Type => 'text/plain; charset=utf-8',
+ Encoding => '8-bit');
+ }
if ($debug) {
$top->print();
return;
--
2.39.5
_______________________________________________
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 v4 2/2] fix #4211: convert quarantine link mail to template
2024-10-14 18:05 [pmg-devel] [PATCH pmg-api/docs v4] fix #4211: convert quarantine link mail to template Stoiko Ivanov
2024-10-14 18:05 ` [pmg-devel] [PATCH pmg-api v4 1/2] utils: allow specifying plain and/or html for finalize_report() Stoiko Ivanov
@ 2024-10-14 18:05 ` Stoiko Ivanov
2024-10-14 18:05 ` [pmg-devel] [PATCH pmg-docs v4 1/1] pmgconfig: document support for html and plain-text email templates Stoiko Ivanov
2024-10-21 10:08 ` [pmg-devel] [PATCH pmg-api/docs v4] fix #4211: convert quarantine link mail to template Christoph Heiss
3 siblings, 0 replies; 5+ messages in thread
From: Stoiko Ivanov @ 2024-10-14 18:05 UTC (permalink / raw)
To: pmg-devel
From: Christoph Heiss <c.heiss@proxmox.com>
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>
[S.I. adapt to changed arguments of finalize_report]
Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
src/Makefile | 2 ++
src/PMG/API2/Quarantine.pm | 19 +++++++------------
src/templates/quarantine-link.plain.tt | 6 ++++++
src/templates/quarantine-link.tt | 13 +++++++++++++
4 files changed, 28 insertions(+), 12 deletions(-)
create mode 100644 src/templates/quarantine-link.plain.tt
create mode 100644 src/templates/quarantine-link.tt
diff --git a/src/Makefile b/src/Makefile
index 8e49a10..1232880 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -44,6 +44,8 @@ TEMPLATES = \
postgresql.conf.in \
pg_hba.conf.in \
backup-notification.tt \
+ quarantine-link.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..16dec57 100644
--- a/src/PMG/API2/Quarantine.pm
+++ b/src/PMG/API2/Quarantine.pm
@@ -1235,18 +1235,13 @@ 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,
- );
-
- # we use an empty envelope sender (we don't want to receive NDRs)
- PMG::Utils::reinject_local_mail ($mail, '', [$receiver], undef, $fqdn);
+ my $tt = PMG::Config::get_template_toolkit();
+ my $vars = {
+ fqdn => $fqdn,
+ link => $link,
+ };
+
+ PMG::Utils::finalize_report($tt, 'quarantine-link', $vars, $mailfrom, $receiver);
}
__PACKAGE__->register_method ({
diff --git a/src/templates/quarantine-link.plain.tt b/src/templates/quarantine-link.plain.tt
new file mode 100644
index 0000000..bb04e67
--- /dev/null
+++ b/src/templates/quarantine-link.plain.tt
@@ -0,0 +1,6 @@
+subject: Proxmox Mail Gateway - Quarantine Link
+Here is your link for the spam quarantine on [% fqdn %]:
+
+[% link %]
+
+Powered by http://www.proxmox.com
diff --git a/src/templates/quarantine-link.tt b/src/templates/quarantine-link.tt
new file mode 100644
index 0000000..d6fd17e
--- /dev/null
+++ b/src/templates/quarantine-link.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>
--
2.39.5
_______________________________________________
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