Completed
Push — master ( 16fc24...51a4d2 )
by Thomas
8s
created

doorpi.action.SingleActions.createSnapshot()   A

Complexity

Conditions 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 15
rs 9.4285
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import logging
5
logger = logging.getLogger(__name__)
6
logger.debug("%s loaded", __name__)
7
8
import smtplib # used by: fire_action_mail
9
from email.mime.multipart import MIMEMultipart # used by: fire_action_mail
10
from email.mime.text import MIMEText # used by: fire_action_mail
11
from email.MIMEBase import MIMEBase # used by: fire_action_mail
0 ignored issues
show
Bug introduced by
The name MIMEBase does not seem to exist in module email.
Loading history...
12
from email import Encoders # used by: fire_action_mail
0 ignored issues
show
Bug introduced by
The name Encoders does not seem to exist in module email.
Loading history...
13
from email.Utils import COMMASPACE # used by: fire_action_mail
0 ignored issues
show
Bug introduced by
The name Utils does not seem to exist in module email.
Loading history...
14
15
from doorpi.action.base import SingleAction
16
import doorpi
17
import os
18
from take_snapshot import get_last_snapshot
19
import subprocess as sub
0 ignored issues
show
Unused Code introduced by
Unused subprocess imported as sub
Loading history...
20
21
def fire_action_mail(smtp_to, smtp_subject, smtp_text, smtp_snapshot):
22
    try:
23
        smtp_host = doorpi.DoorPi().config.get('SMTP', 'server', 'smtp.gmail.com')
24
        smtp_port = doorpi.DoorPi().config.get_int('SMTP', 'port', 465)
25
        smtp_user = doorpi.DoorPi().config.get('SMTP', 'username')
26
        smtp_password = doorpi.DoorPi().config.get('SMTP', 'password')
27
        smtp_from = doorpi.DoorPi().config.get('SMTP', 'from')
28
29
        smtp_use_tls = doorpi.DoorPi().config.get_boolean('SMTP', 'use_tls', False)
30
        smtp_use_ssl = doorpi.DoorPi().config.get_boolean('SMTP', 'use_ssl', True)
31
        smtp_need_login = doorpi.DoorPi().config.get_boolean('SMTP', 'need_login', True)
32
33
        smtp_tolist = smtp_to.split()
34
35
        email_signature = doorpi.DoorPi().config.get_string_parsed('SMTP', 'signature', '!EPILOG!')
36
37
        if smtp_use_ssl:
38
            server = smtplib.SMTP_SSL(smtp_host, smtp_port)
39
        else:
40
            server = smtplib.SMTP(smtp_host, smtp_port)
41
42
        server.ehlo()
43
        if smtp_use_tls and not smtp_use_ssl:
44
            server.starttls()
45
        if smtp_need_login:
46
            server.login(smtp_user, smtp_password)
47
48
        msg = MIMEMultipart()
49
        msg['From'] = smtp_from
50
        msg['To'] = COMMASPACE.join(smtp_tolist)
51
        msg['Subject'] = doorpi.DoorPi().parse_string(smtp_subject)
52
        msg.attach(MIMEText(doorpi.DoorPi().parse_string(smtp_text), 'html'))
53
        if email_signature and len(email_signature) > 0:
54
            msg.attach(MIMEText('\nsent by:\n'+doorpi.DoorPi().epilog, 'plain'))
55
56
        if smtp_snapshot:
57
            smtp_snapshot = doorpi.DoorPi().parse_string(smtp_snapshot)
58
            if not os.path.exists(smtp_snapshot):
59
                smtp_snapshot = get_last_snapshot()
60
61
        try:
62
            with open(smtp_snapshot, "rb") as snapshot_file:
63
                part = MIMEBase('application',"octet-stream")
64
                part.set_payload(snapshot_file.read())
65
                Encoders.encode_base64(part)
66
                part.add_header(
67
                    'Content-Disposition',
68
                    'attachment; filename="%s"' % os.path.basename(smtp_snapshot))
69
                msg.attach(part)
70
        except Exception as exp:
71
            logger.exception("send not attachment for this mail: %s" % exp)
72
73
        server.sendmail(smtp_from, smtp_tolist, msg.as_string())
74
        server.quit()
75
    except:
76
        logger.exception("couldn't send email")
77
        return False
78
    return True
79
80
81
def get(parameters):
82
    parameter_list = parameters.split(',')
83
    if len(parameter_list) < 3 or len(parameter_list) > 4: return None
84
85
    smtp_to = parameter_list[0]
86
    smtp_subject = parameter_list[1]
87
    smtp_text = parameter_list[2]
88
    if (len(parameter_list) == 4):
89
        smtp_snapshot = parameter_list[3]
90
    else:
91
        smtp_snapshot = False
92
    
93
    return MailtoAction(fire_action_mail,
94
                     smtp_to = smtp_to,
95
                     smtp_subject = smtp_subject,
96
                     smtp_text = smtp_text,
97
                     smtp_snapshot = smtp_snapshot)
98
99
100
class MailtoAction(SingleAction):
101
    pass
102