email_service.py 1.57 KB
Newer Older
tihmels's avatar
tihmels committed
1
2
3
4
5
6
7
8
9
10
11
# ***************************************************
# * Copyright (C) 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# ***************************************************/



Arne Gerdes's avatar
Arne Gerdes committed
12
import os
13
import smtplib
Arne Gerdes's avatar
Arne Gerdes committed
14
15
from email import encoders
from email.mime.base import MIMEBase
16
17
18
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

Arne Gerdes's avatar
Arne Gerdes committed
19
20

def sendMail(subject, to='tjado.ihmels@uni-oldenburg.de', body='', filepath=''):
tihmels's avatar
tihmels committed
21
    """
tihmels's avatar
tihmels committed
22
    For a longer runtime on a remote server, emails serve as feedback
tihmels's avatar
tihmels committed
23
24
25
26
27
28
    :param subject: subject
    :param to: email adress
    :param body: email text
    :param filepath: optional attachment. For example a logfile
    :return:
    """
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    fromaddr = "projectmood18@gmail.com"

    msg = MIMEMultipart()

    msg['From'] = fromaddr
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(body, 'plain'))

    if filepath:
        filename = os.path.basename(filepath)
        attachment = open(filepath, "rb")
        part = MIMEBase('application', 'octet-stream')
        part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

        msg.attach(part)

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "kR2-zgY-BTf-gRT")
    text = msg.as_string()
    server.sendmail(fromaddr, to, text)
Arne Gerdes's avatar
Arne Gerdes committed
54
    server.quit()