PushService: Initial release
[enigma2-plugins.git] / pushservice / src / PushMail.py
1 '''
2 Created on 14.11.2011
3
4 @author: Frank Glaser
5 '''
6
7 import os
8 import inspect
9
10 from Components.config import *
11
12 # Plugin internal
13 from . import _
14 from mail import Message, sendmail
15
16
17 # Constants
18 MAIL_HEADER_PREFIX = _("%s PushService: ")
19 MAIL_BODY_PREFIX = ""
20 MAIL_BODY_SUFFIX =      "\n\n" \
21                                                                                 + _("Provided by Dreambox Plugin %s %s") + "\n" \
22                                                                                 + _("C 2012 by betonme @ IHAD") + "\n" \
23                                                                                 + _("Support %s") + "\n" \
24                                                                                 + _("Donate %s")
25
26
27 class PushMail(object):
28         def __init__(self):
29                 pass
30
31         def push(self, subject, body="", attachments=[], success=None, error=None, timeout=30):
32                 from plugin import NAME, VERSION, SUPPORT, DONATE
33                 
34                 from_addr = config.pushservice.mailfrom.value
35                 to_addrs = [config.pushservice.mailfrom.value or config.pushservice.mailto.value]
36                 
37                 # Set SMTP parameters
38                 mailconf = {}
39                 mailconf["host"] = config.pushservice.smtpserver.value
40                 mailconf["port"] = config.pushservice.smtpport.value
41                 mailconf["username"] = config.pushservice.username.value
42                 mailconf["password"] = config.pushservice.password.value
43                 mailconf["tls"] = config.pushservice.smtptyp.value
44                 mailconf["timeout"] = timeout
45                 
46                 # Create message object
47                 subject = ( MAIL_HEADER_PREFIX % config.pushservice.boxname.value + str(subject) )
48                 body = ( MAIL_BODY_PREFIX + str(body) + MAIL_BODY_SUFFIX % ( NAME, VERSION, SUPPORT, DONATE) )
49                 message = Message(from_addr, to_addrs, subject, body) #TODO change mime="text/plain", charset="utf-8")
50                 if attachments:
51                         for attachment in attachments:
52                                 message.attach(attachment) #TODO change mime=None, charset=None, content=None):
53                 
54                 # Send message
55                 print _("[PushService] PushMail: Sending message: %s") % subject
56                 deferred, connector = sendmail(mailconf, message)
57                 
58                 # Define callbacks
59                 def callback(r):
60                         print _("[PushService] PushMail: Sent successfully: %s") % subject
61                         if callable(success):
62                                 # Check number of arguments
63                                 argspec = inspect.getargspec(success)
64                                 if len(argspec.args) > 1:
65                                         success(r)
66                                 else:
67                                         success()
68                 
69                 def errback(e):
70                         print _("[PushService] PushMail: Sent failed: %s") % subject + "\n" + str(e)
71                         if callable(error):
72                                 # Check number of arguments
73                                 argspec = inspect.getargspec(error)
74                                 if len(argspec.args) > 1:
75                                         error(e)
76                                 else:
77                                         error()
78                 
79                 # Add callbacks
80                 deferred.addCallback(callback)
81                 deferred.addErrback(errback)
82                 
83                 return connector