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

doorpi/action/SingleActions/mailto.py (2 issues)

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
12
from email import Encoders # used by: fire_action_mail
13
from email.Utils import COMMASPACE # used by: fire_action_mail
14
15
from doorpi.action.base import SingleAction
16
import doorpi
17
import os
18
import subprocess as sub
19
20
def fire_action_mail(smtp_to, smtp_subject, smtp_text, smtp_snapshot):
21
    try:
22
        smtp_host = doorpi.DoorPi().config.get('SMTP', 'server')
23
        smtp_port = doorpi.DoorPi().config.get_int('SMTP', 'port')
24
        smtp_user = doorpi.DoorPi().config.get('SMTP', 'username')
25
        smtp_password = doorpi.DoorPi().config.get('SMTP', 'password')
26
        smtp_from = doorpi.DoorPi().config.get('SMTP', 'from')
27
28
        smtp_use_tls = doorpi.DoorPi().config.get_boolean('SMTP', 'use_tls')
29
        smtp_need_login = doorpi.DoorPi().config.get_boolean('SMTP', 'need_login')
30
31
        smtp_tolist = smtp_to.split()
32
33
        server = smtplib.SMTP()
34
        server.connect(smtp_host, smtp_port)
35
        server.ehlo()
36
37
        if smtp_use_tls: server.starttls()
38
        if smtp_need_login: server.login(smtp_user, smtp_password)
39
40
        msg = MIMEMultipart()
41
        msg['From'] = smtp_from
42
        msg['To'] = COMMASPACE.join(smtp_tolist)
43
        msg['Subject'] = doorpi.DoorPi().parse_string(smtp_subject)
44
        msg.attach(MIMEText(doorpi.DoorPi().parse_string(smtp_text), 'html'))
45
        msg.attach(MIMEText('\nsent by:\n'+doorpi.DoorPi().epilog, 'plain'))
46
47
        #add a snapshot
48
        file = []
49
        if smtp_snapshot and len(doorpi.DoorPi().config.get('SIP-Phone', 'capture_device', '')) > 0:
50
            file = createSnapshot()
51
        if len(file) > 0:
52
            part = MIMEBase('application',"octet-stream")
53
            part.set_payload(open(file,"rb").read())
54
            Encoders.encode_base64(part)
55
            part.add_header('Content-Disposition', 'attachment; filename="%s"'
56
                   % os.path.basename(file))
57
            msg.attach(part)
58
59
        server.sendmail(smtp_from, smtp_tolist, msg.as_string())
60
        server.quit()
61
    except:
62
        logger.exception("couldn't send email")
63
        return False
64
    return True
65
66
def createSnapshot():
67
    snapshot_file = '/tmp/doorpi.jpg'
68
    size = doorpi.DoorPi().config.get_string('DoorPi', 'snapshot_size', '1280x720')
69
    command = "fswebcam --no-banner -r " + size + " " + snapshot_file
70
    try:
71
        retcode = subprocess.call(command, shell=True)
0 ignored issues
show
Comprehensibility Best Practice introduced by pistreamer
Undefined variable 'subprocess'
Loading history...
72
        if retcode != 0:
73
            logger.error('error creating snapshot')
74
        else:
75
            logger.info('snapshot created: %s', snapshot_file)
76
            return snapshot_file
77
78
    except OSError as e:
0 ignored issues
show
The variable e seems to be unused.
Loading history...
79
        logger.error('error creating snapshot')
80
    return ''
81
82
def get(parameters):
83
    parameter_list = parameters.split(',')
84
    if len(parameter_list) < 3 or len(parameter_list) > 4: return None
85
86
    smtp_to = parameter_list[0]
87
    smtp_subject = parameter_list[1]
88
    smtp_text = parameter_list[2]
89
    if (len(parameter_list) == 4):
90
        smtp_snapshot = parameter_list[3]
91
    else:
92
        smtp_snapshot = False
93
    
94
    return MailtoAction(fire_action_mail,
95
                     smtp_to = smtp_to,
96
                     smtp_subject = smtp_subject,
97
                     smtp_text = smtp_text,
98
                     smtp_snapshot = smtp_snapshot)
99
100
101
class MailtoAction(SingleAction):
102
    pass
103