email_service.py 1000 Bytes
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1
import os
2
import smtplib
Arne Gerdes's avatar
Arne Gerdes committed
3
4
from email import encoders
from email.mime.base import MIMEBase
5
6
7
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

Arne Gerdes's avatar
Arne Gerdes committed
8
9

def sendMail(subject, to='tjado.ihmels@uni-oldenburg.de', body='', filepath=''):
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
    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
35
    server.quit()