From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pmg-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 65B211FF15F
	for <inbox@lore.proxmox.com>; Mon,  7 Oct 2024 12:32:34 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 3DD493BDA8;
	Mon,  7 Oct 2024 12:33:00 +0200 (CEST)
From: Christoph Heiss <c.heiss@proxmox.com>
To: pmg-devel@lists.proxmox.com
Date: Mon,  7 Oct 2024 12:32:11 +0200
Message-ID: <20241007103214.1006844-2-c.heiss@proxmox.com>
X-Mailer: git-send-email 2.46.0
In-Reply-To: <20241007103214.1006844-1-c.heiss@proxmox.com>
References: <20241007103214.1006844-1-c.heiss@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.029 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pmg-devel] [PATCH pmg-api v2 1/2] utils: allow specifying plain
 and/or html for finalize_report()
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pmg-devel-bounces@lists.proxmox.com
Sender: "pmg-devel" <pmg-devel-bounces@lists.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>
---
 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