From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A1E9B60BF0 for ; Thu, 3 Sep 2020 14:10:03 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8BD5E18AEE for ; Thu, 3 Sep 2020 14:10:03 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 54BFB18A62 for ; Thu, 3 Sep 2020 14:10:01 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 20D4E4206A for ; Thu, 3 Sep 2020 14:10:01 +0200 (CEST) From: Stoiko Ivanov To: pve-devel@lists.proxmox.com Date: Thu, 3 Sep 2020 14:09:44 +0200 Message-Id: <20200903120946.23596-2-s.ivanov@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200903120946.23596-1-s.ivanov@proxmox.com> References: <20200903120946.23596-1-s.ivanov@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [tools.pm, proxmox.com, ietf.org] Subject: [pve-devel] [PATCH common v2 1/2] sendmail-helper: only send multipart if necessary X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Sep 2020 12:10:03 -0000 PVE::Tools::sendmail currently always sends a multipart/alternative message irrespective of the actual content of the mail (e.g. a plain-text only mail need not be sent as multipart message). Additionally a few small refactorings based on the discussion in https://lists.proxmox.com/pipermail/pbs-devel/2020-August/000423.html and commited in 66004f22c6475ceb0146cf2df1f380f9f0274be4 in the rust proxmox repository git://git.proxmox.com/git/proxmox.git were carried over. tested by creating a backup of a VM and setting an e-mail address, having ha-manager send a mail after fencing as well as sending a few small mails via 'perl -e' Signed-off-by: Stoiko Ivanov --- src/PVE/Tools.pm | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index f9270d9..3b5b199 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -25,6 +25,8 @@ use Text::ParseWords; use String::ShellQuote; use Time::HiRes qw(usleep gettimeofday tv_interval alarm); use Scalar::Util 'weaken'; +use Date::Format qw(time2str); + use PVE::Syscall; # avoid warning when parsing long hex values with hex() @@ -1460,23 +1462,36 @@ sub sendmail { open (MAIL, "|-", "sendmail", "-B", "8BITMIME", "-f", $mailfrom, "--", @$mailto) || die "unable to open 'sendmail' - $!"; + my $date = time2str('%a, %d %b %Y %H:%M:%S %z', time()); + + my $is_multipart = $text && $html; + # multipart spec see https://www.ietf.org/rfc/rfc1521.txt my $boundary = "----_=_NextPart_001_".int(time).$$; - print MAIL "Content-Type: multipart/alternative;\n"; - print MAIL "\tboundary=\"$boundary\"\n"; - print MAIL "MIME-Version: 1.0\n"; + if ($subject =~ /[^[:ascii:]]/) { + $subject = Encode::encode('MIME-Header', $subject); + } - print MAIL "FROM: $author <$mailfrom>\n"; - print MAIL "TO: $rcvrtxt\n"; - print MAIL "SUBJECT: $subject\n"; - print MAIL "\n"; - print MAIL "This is a multi-part message in MIME format.\n\n"; - print MAIL "--$boundary\n"; + if ($subject =~ /[^[:ascii:]]/ || $is_multipart) { + print MAIL "MIME-Version: 1.0\n"; + } + print MAIL "From: $author <$mailfrom>\n"; + print MAIL "To: $rcvrtxt\n"; + print MAIL "Date: $date\n"; + print MAIL "Subject: $subject\n"; + + if ($is_multipart) { + print MAIL "Content-Type: multipart/alternative;\n"; + print MAIL "\tboundary=\"$boundary\"\n"; + print MAIL "\n"; + print MAIL "This is a multi-part message in MIME format.\n\n"; + print MAIL "--$boundary\n"; + } if (defined($text)) { print MAIL "Content-Type: text/plain;\n"; - print MAIL "\tcharset=\"UTF8\"\n"; + print MAIL "\tcharset=\"UTF-8\"\n"; print MAIL "Content-Transfer-Encoding: 8bit\n"; print MAIL "\n"; @@ -1486,18 +1501,18 @@ sub sendmail { print MAIL $text; - print MAIL "\n--$boundary\n"; + print MAIL "\n--$boundary\n" if $is_multipart; } if (defined($html)) { print MAIL "Content-Type: text/html;\n"; - print MAIL "\tcharset=\"UTF8\"\n"; + print MAIL "\tcharset=\"UTF-8\"\n"; print MAIL "Content-Transfer-Encoding: 8bit\n"; print MAIL "\n"; print MAIL $html; - print MAIL "\n--$boundary--\n"; + print MAIL "\n--$boundary--\n" if $is_multipart; } close(MAIL); -- 2.20.1