public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pmg-devel] [PATCH pmg-api v3 0/3] fix #4211: convert quarantine link mail to template
@ 2024-10-08 11:17 Christoph Heiss
  2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 1/3] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Christoph Heiss @ 2024-10-08 11:17 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/

history
=======

v1: https://lore.proxmox.com/pmg-devel/20241001125948.605201-1-c.heiss@proxmox.com/
v2: https://lore.proxmox.com/pmg-devel/20241007103214.1006844-1-c.heiss@proxmox.com/

Notable changes v1 -> v2:
  * add support for plaintext templates in addition to HTML

Notable changes v2 -> v3:
  * add short documentation for email templates

diffstat
========

pmg-api:

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             | 22 ++++++------
 src/PMG/Backup.pm                      |  5 +--
 src/PMG/CLI/pmgqm.pm                   |  5 ++-
 src/PMG/CLI/pmgreport.pm               |  5 ++-
 src/PMG/Utils.pm                       | 49 +++++++++++++++++++++++---
 src/templates/quarantine-link.html.tt  | 13 +++++++
 src/templates/quarantine-link.plain.tt |  5 +++
 8 files changed, 85 insertions(+), 21 deletions(-)
 create mode 100644 src/templates/quarantine-link.html.tt
 create mode 100644 src/templates/quarantine-link.plain.tt

pmg-docs:

Christoph Heiss (1):
  pmgconfig: document support for html and plain-text email templates

 pmgconfig.adoc | 8 ++++++++
 1 file changed, 8 insertions(+)

-- 
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] 4+ messages in thread

* [pmg-devel] [PATCH pmg-api v3 1/3] utils: allow specifying plain and/or html for finalize_report()
  2024-10-08 11:17 [pmg-devel] [PATCH pmg-api v3 0/3] fix #4211: convert quarantine link mail to template Christoph Heiss
@ 2024-10-08 11:17 ` Christoph Heiss
  2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 2/3] fix #4211: convert quarantine link mail to template Christoph Heiss
  2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-docs v3 3/3] pmgconfig: document support for html and plain-text email templates Christoph Heiss
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Heiss @ 2024-10-08 11:17 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>
---
Changes v1 -> v2:
  * new patch

Changes v2 -> v3:
  * changed additional parameters to hashref

 src/PMG/Backup.pm        |  5 ++--
 src/PMG/CLI/pmgqm.pm     |  5 +++-
 src/PMG/CLI/pmgreport.pm |  5 +++-
 src/PMG/Utils.pm         | 49 ++++++++++++++++++++++++++++++++++++----
 4 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm
index ab7e628..2d54f66 100644
--- a/src/PMG/Backup.pm
+++ b/src/PMG/Backup.pm
@@ -418,8 +418,9 @@ 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',
+    });
 }
 
 1;
diff --git a/src/PMG/CLI/pmgqm.pm b/src/PMG/CLI/pmgqm.pm
index 987ddc9..468d56f 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..a946b04 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..41aed83 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -7,6 +7,7 @@ use utf8;
 # TODO: can we remove this (as our perl source texts should be all UTF-8 compatible)?
 no utf8;
 
+use Carp;
 use Cwd;
 use DBI;
 use Data::Dumper;
@@ -1248,12 +1249,34 @@ sub format_uptime {
     }
 }
 
+# Sends a report to the given receiver, building the body from a HTML template.
+# Optionally a plain-text template can also be specified, which then sends a
+# multipart/alternative mail containing both variants.
+#
+# The HTML template is always required and must contain at <title> element.
+#
+# The resulting mail is then reinjected with an empty envelope sender.
+#
+# Parameters:
+#   * `tt` - The template toolkit instance to use
+#   * `data` - Template variables to pass to the template processor
+#   * `mailfrom` - Sender address
+#   * `receiver` - Recipient address
+#   * `params` - Further structured parameter as hashref.
+#                (Possible) values:
+#                `html` - name of the HTML template file (required)
+#                `plain` - name of the plain-text template file (optional)
+#                `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, $data, $mailfrom, $receiver, $params) = @_;
+
+    croak 'HTML template must be specified for report'
+	if !defined($params->{html});
 
     my $html = '';
 
-    $tt->process($template, $data, \$html) ||
+    $tt->process($params->{html}, $data, \$html) ||
 	die $tt->error() . "\n";
 
     my $title;
@@ -1263,21 +1286,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] 4+ messages in thread

* [pmg-devel] [PATCH pmg-api v3 2/3] fix #4211: convert quarantine link mail to template
  2024-10-08 11:17 [pmg-devel] [PATCH pmg-api v3 0/3] fix #4211: convert quarantine link mail to template Christoph Heiss
  2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 1/3] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
@ 2024-10-08 11:17 ` Christoph Heiss
  2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-docs v3 3/3] pmgconfig: document support for html and plain-text email templates Christoph Heiss
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Heiss @ 2024-10-08 11:17 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>
---
Changes v1 -> v2:
  * adapted to new finalize_report() changes
  * added plain-text template

Changes v2 -> v3:
  * adapted to finalize_report() changes

 src/Makefile                           |  2 ++
 src/PMG/API2/Quarantine.pm             | 22 ++++++++++------------
 src/templates/quarantine-link.html.tt  | 13 +++++++++++++
 src/templates/quarantine-link.plain.tt |  5 +++++
 4 files changed, 30 insertions(+), 12 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..058180c 100644
--- a/src/PMG/API2/Quarantine.pm
+++ b/src/PMG/API2/Quarantine.pm
@@ -1235,18 +1235,16 @@ 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, $vars, $mailfrom, $receiver, {
+	html => 'quarantine-link.html.tt',
+	plain => 'quarantine-link.plain.tt',
+    });
 }
 
 __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] 4+ messages in thread

* [pmg-devel] [PATCH pmg-docs v3 3/3] pmgconfig: document support for html and plain-text email templates
  2024-10-08 11:17 [pmg-devel] [PATCH pmg-api v3 0/3] fix #4211: convert quarantine link mail to template Christoph Heiss
  2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 1/3] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
  2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 2/3] fix #4211: convert quarantine link mail to template Christoph Heiss
@ 2024-10-08 11:17 ` Christoph Heiss
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Heiss @ 2024-10-08 11:17 UTC (permalink / raw)
  To: pmg-devel

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
Changes v2 -> v3:
  * new patch

 pmgconfig.adoc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/pmgconfig.adoc b/pmgconfig.adoc
index a9377c5..e9833d4 100644
--- a/pmgconfig.adoc
+++ b/pmgconfig.adoc
@@ -155,6 +155,14 @@ engine, and the Apache {spamassassin} project. These services use
 separate configuration files, so we need to rewrite those files when the
 configuration is changed.
 
+{pmg} also features support for customizing the email-based reports and
+notifications it can send to administrators and users. These can be adjusted in
+the same way as configuration files. For some reports and notifications, both
+HTML and plain-text variants exist, which will be send together as multi-part
+mail. These can be recognized by being suffixed using `.html.tt` or `.plain.tt`,
+respectively, and should be modified together if available to present a
+consistent experience to users.
+
 We use a template-based approach to generate these files. The {tts} is
 a well known, fast and flexible template processing system. You can
 find the default templates in `/var/lib/pmg/templates/`. Please do not
-- 
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] 4+ messages in thread

end of thread, other threads:[~2024-10-08 11:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-08 11:17 [pmg-devel] [PATCH pmg-api v3 0/3] fix #4211: convert quarantine link mail to template Christoph Heiss
2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 1/3] utils: allow specifying plain and/or html for finalize_report() Christoph Heiss
2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 2/3] fix #4211: convert quarantine link mail to template Christoph Heiss
2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-docs v3 3/3] pmgconfig: document support for html and plain-text email templates Christoph Heiss

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal