summaryrefslogtreecommitdiff
path: root/src/server/rz_mail.py
blob: 0ecf1d61ab77fa5b2402b89a52d4565bec1f620b (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
import magic

from flask_mail import Mail, Message

mail = None

magic_mime = magic.open(magic.MAGIC_MIME)
magic_mime.load()

def mimetype(data):
    return magic_mime.buffer(data).split(';')[0]

def init_mail(webapp):
    global mail
    mail = Mail(webapp)

def send_message(recipients, subject, attachments, body):
    """
    attachments is a list of tuples (filename, data)
    libmagic is used to guess the mimetype
    """
    msg = Message(subject, recipients=recipients)
    msg.body = body
    for filename, data in attachments:
        msg.attach(filename=filename, content_type=mimetype(data), data=data)
    mail.send(msg)