public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api v4 1/2] utils: allow specifying plain and/or html for finalize_report()
Date: Mon, 14 Oct 2024 20:05:47 +0200	[thread overview]
Message-ID: <20241014180549.27671-2-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20241014180549.27671-1-s.ivanov@proxmox.com>

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


  reply	other threads:[~2024-10-14 18:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-10-14 18:05 ` [pmg-devel] [PATCH pmg-api v4 2/2] " 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241014180549.27671-2-s.ivanov@proxmox.com \
    --to=s.ivanov@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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