PushService: Fixed load error
[enigma2-plugins.git] / pushservice / src / Services / SMTP.py
1 #######################################################################
2 #
3 #    Push Service for Enigma-2
4 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
5 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=167779
6 #
7 #    This program is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU General Public License
9 #    as published by the Free Software Foundation; either version 2
10 #    of the License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #######################################################################
18
19 # Config
20 from Components.config import config, NoSave, ConfigText, ConfigNumber, ConfigYesNo, ConfigPassword
21
22 # Plugin internal
23 from Plugins.Extensions.PushService.__init__ import _
24 from Plugins.Extensions.PushService.ServiceBase import ServiceBase
25
26 # Plugin specific
27 from mail.mail import Message, sendmail
28
29
30 # Constants
31 MAIL_HEADER_TEMPLATE = _("{box:s} {name:s}: {plugin:s}: {subject:s}")
32 MAIL_BODY_TEMPLATE =    _("{body:s}\n\n") \
33                                                                 + _("Provided by Dreambox Plugin {name:s} {version:s} - {plugin:s}\n") \
34                                                                 + _("C 2012 by betonme @ IHAD\n") \
35                                                                 + _("Support {support:s}\n") \
36                                                                 + _("Donate {donate:s}")
37
38
39 class SMTP(ServiceBase):
40         
41         ForceSingleInstance = False
42         
43         def __init__(self):
44                 # Is called on instance creation
45                 ServiceBase.__init__(self)
46                 self.connectors = []
47                 
48                 # Default configuration
49                 self.setOption( 'smtpserver', NoSave(ConfigText(default="smtp.server.com", fixed_size = False)),    _("SMTP Server") )
50                 self.setOption( 'smtpport',   NoSave(ConfigNumber(default = 587)),                                  _("SMTP Port") )
51                 self.setOption( 'smtpssl',    NoSave(ConfigYesNo(default = True)),                                  _("SMTP SSL") )
52                 self.setOption( 'smtptls',    NoSave(ConfigYesNo(default = True)),                                  _("SMTP TLS") )
53                 self.setOption( 'timeout',    NoSave(ConfigNumber(default = 30)),                                   _("Timeout") )
54                 
55                 self.setOption( 'username',   NoSave(ConfigText(default="user", fixed_size = False)),               _("User name") )
56                 self.setOption( 'password',   NoSave(ConfigPassword(default="password")),                           _("Password") )
57                 
58                 self.setOption( 'mailfrom',   NoSave(ConfigText(default = "abc@provider.com", fixed_size = False)), _("Mail from") )
59                 self.setOption( 'mailto',     NoSave(ConfigText(fixed_size = False)),                               _("Mail to or leave empty") )
60
61         def push(self, callback, errback, pluginname, subject, body="", attachments=[]):
62                 from Plugins.Extensions.PushService.plugin import NAME, VERSION, SUPPORT, DONATE
63                 
64                 # Set SMTP parameters
65                 mailconf = {}
66                 mailconf["host"]     = self.getValue('smtpserver')
67                 mailconf["port"]     = self.getValue('smtpport')
68                 mailconf["username"] = self.getValue('username')
69                 mailconf["password"] = self.getValue('password')
70                 mailconf["ssl"]      = self.getValue('smtpssl')
71                 mailconf["tls"]      = self.getValue('smtptls')
72                 mailconf["timeout"]  = self.getValue('timeout')
73                 
74                 # Create message object
75                 from_addr = self.getValue('mailfrom')
76                 to_addrs = [self.getValue('mailto') or from_addr]
77                 
78                 # Prepare message
79                 if body == "": body = subject
80                 subject = MAIL_HEADER_TEMPLATE.format( **{'box': config.pushservice.boxname.value, 'name': NAME, 'plugin': pluginname, 'subject': subject} )
81                 body    = MAIL_BODY_TEMPLATE.format( **{'body': str(body), 'name': NAME, 'version': VERSION, 'plugin': pluginname, 'support': SUPPORT, 'donate': DONATE} )
82                 message = Message(from_addr, to_addrs, subject, body) #TODO change mime="text/plain", charset="utf-8")
83                 if attachments:
84                         for attachment in attachments:
85                                 message.attach(attachment) #TODO change mime=None, charset=None, content=None):
86                 
87                 # Send message
88                 print _("[PushService] PushMail: Sending message: %s") % subject
89                 deferred, connector = sendmail(mailconf, message)
90                 
91                 # Add callbacks
92                 deferred.addCallback(callback)
93                 deferred.addErrback(errback)
94                 
95                 self.connectors.append(connector)
96
97         def cancel(self):
98                 # Cancel push
99                 if self.connectors:
100                         for connector in self.connectors:
101                                 connector.disconnect()