File: /volume1/@appstore/MailPlus-Server/scripts/common/send_mail.py
#!/usr/bin/env python3
# -*- encoding=utf8 -*-
# Copyright (c) 2000-2020 Synology Inc. All rights reserved.
import sys
sys.path.append("/var/packages/MailPlus-Server/target/scripts/common")
from addr_util import *
def send_mail(subject, msg, sender, recipients):
from email.mime.text import MIMEText
from email import utils, policy
import subprocess
email_policy = None if (isAsciiString(sender) and isAsciiString(recipients)) else policy.SMTPUTF8
msg = MIMEText(msg, 'plain', 'utf8', policy=email_policy)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipients
msg['Message-ID'] = utils.make_msgid()
args = ['/var/packages/MailPlus-Server/target/sbin/sendmail', '-t', '-f', sender]
sp = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
sp.communicate(msg.as_string())
sp.wait()