]> git.proxmox.com Git - pve-common.git/commitdiff
Tools: add sendmail
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 4 Apr 2016 14:23:08 +0000 (16:23 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 5 Apr 2016 04:21:45 +0000 (06:21 +0200)
can be used to send multipart (HTML, plain) mails to one or more
recipients at once.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/Tools.pm

index 40dfc5a36908eb73c569b9e5ce406f598561c933..cb8d9b27b78558c1d46a28f8dbb320e2eb90c521 100644 (file)
@@ -1214,6 +1214,69 @@ sub sync_mountpoint {
     return $result;
 }
 
     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) = @_;
 
 sub tempfile {
     my ($perm, %opts) = @_;