summaryrefslogtreecommitdiff
path: root/src/server/rz_mail.py
blob: 4addc9f2e97f94bc3fe10595b9e8131ae40b72d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import smtplib
from os.path import basename
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import Encoders

from flask import current_app

def send_message(recipients, subject, attachments, body):
    """
    attachments is a list of tuples (filename, mimetype, data)
    """
    assert isinstance(recipients, list)
    send_from = current_app.rz_config.mail_default_sender
    msg = MIMEMultipart(
        From=send_from,
        To=COMMASPACE.join(recipients),
        Date=formatdate(localtime=True),
        Subject=subject
    )
    msg.attach(MIMEText(body))

    for filename, mimetype, data in attachments:
        maintype, subtype = mimetype.split('/')
        part = MIMEBase(maintype, subtype)
        part.set_payload(data)
        Encoders.encode_base64(part)
        part.add_header('Content_Disposition', 'attachment; filename="%s"' % filename)
        msg.attach(part)

    smtp = smtplib.SMTP(current_app.rz_config.mail_hostname)
    smtp.sendmail(send_from, recipients, msg.as_string())
    smtp.close()