From b61a47dbebdf1c8fb4c1cfbd7f1b3755277d583d Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Mon, 4 Apr 2016 16:23:08 +0200 Subject: [PATCH] Tools: add sendmail can be used to send multipart (HTML, plain) mails to one or more recipients at once. Signed-off-by: Thomas Lamprecht --- src/PVE/Tools.pm | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 40dfc5a..cb8d9b2 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -1214,6 +1214,69 @@ sub sync_mountpoint { return $result; } +# support sending multi-part mail messages with a text and or a HTML part +# mailto may be a single email string or an array of receivers +sub sendmail { + my ($mailto, $subject, $text, $html, $mailfrom, $author) = @_; + + $mailto = [ $mailto ] if ref($mailto) ne 'ARRAY'; + + my $rcvrarg = ''; + foreach my $r (@$mailto) { + $rcvrarg .= " '$r'"; + } + my $rcvrtxt = join (', ', @$mailto); + + $mailfrom = $mailfrom || "root"; + $author = $author || 'Proxmox VE mail notifier'; + + open (MAIL,"|sendmail -B 8BITMIME -f $mailfrom $rcvrarg") || + die "unable to open 'sendmail' - $!"; + + # 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"; + + 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 ($text) { + print MAIL "Content-Type: text/plain;\n"; + print MAIL "\tcharset=\"UTF8\"\n"; + print MAIL "Content-Transfer-Encoding: 8bit\n"; + print MAIL "\n"; + + # avoid 'remove extra line breaks' issue (MS Outlook) + my $fill = ' '; + $text =~ s/^/$fill/gm; + + print MAIL $text; + + print MAIL "\n--$boundary\n"; + } + + if($html) { + print MAIL "Content-Type: text/html;\n"; + print MAIL "\tcharset=\"UTF8\"\n"; + print MAIL "Content-Transfer-Encoding: 8bit\n"; + print MAIL "\n"; + + print MAIL $html; + + print MAIL "\n--$boundary--\n"; + } + + close(MAIL); + +} + sub tempfile { my ($perm, %opts) = @_; -- 2.39.2