From: Christoph Heiss <c.heiss@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api v3 1/3] utils: allow specifying plain and/or html for finalize_report()
Date: Tue, 8 Oct 2024 13:17:51 +0200 [thread overview]
Message-ID: <20241008111753.831405-2-c.heiss@proxmox.com> (raw)
In-Reply-To: <20241008111753.831405-1-c.heiss@proxmox.com>
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
next prev parent reply other threads:[~2024-10-08 11:17 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2024-10-08 11:17 ` [pmg-devel] [PATCH pmg-api v3 2/3] " 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
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=20241008111753.831405-2-c.heiss@proxmox.com \
--to=c.heiss@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.