|
1 # -*- coding: utf-8 -*- |
|
2 #Copyright (C) 2009 Sandro Knauß <bugs@sandroknauss.de> |
|
3 |
|
4 #This program is free software; you can redistribute it and/or modify it under the terms |
|
5 #of the GNU General Public License as published by the Free Software Foundation; |
|
6 #either version 3 of the License, or any later version. |
|
7 #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|
8 #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
9 #See the GNU General Public License for more details. |
|
10 |
|
11 #You should have received a copy of the GNU General Public License |
|
12 #along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
13 |
|
14 import smtplib |
|
15 import copy |
|
16 |
|
17 from ..model.status import Status |
|
18 from ..error import UnknownOption, NeededOption, NoRoute, NoTyp |
|
19 from ..offer import providers |
|
20 from .provider import Provider |
|
21 |
|
22 class SMTP(Provider): |
|
23 def __init__(self, name, config): |
|
24 Provider.__init__(self,name,config,["std"],["mail"]) |
|
25 |
|
26 |
|
27 def loadConfig(self): |
|
28 """Read options from config""" |
|
29 needed=["send_from","host", "port", "user", "password"] |
|
30 |
|
31 for n in needed: |
|
32 setattr(self,n,None) |
|
33 |
|
34 Provider.loadConfig(self) |
|
35 self.bTLS = False |
|
36 self.bSSL = False |
|
37 |
|
38 for (n, v) in self.config: |
|
39 if n in needed: |
|
40 setattr(self,n,v) |
|
41 elif n == "TLS": |
|
42 self.bTLS = bool(v) |
|
43 elif n == "SSL": |
|
44 self.bSSL = bool(v) |
|
45 else: |
|
46 raise UnknownOption(self.name, n) |
|
47 |
|
48 for n in needed: |
|
49 if getattr(self,n) is None: |
|
50 raise NeededOption(self.name, n) |
|
51 |
|
52 def send(self,mail,recipient): |
|
53 try: |
|
54 if not self.testmode: |
|
55 if self.bSSL: |
|
56 smtp = smtplib.SMTP_SSL(self.host,self.port) |
|
57 else: |
|
58 smtp = smtplib.SMTP(self.host,self.port) |
|
59 |
|
60 if self.bTLS: |
|
61 smtp.starttls() |
|
62 |
|
63 if not self.user == "": |
|
64 smtp.login(self.user,self.pw) |
|
65 try: |
|
66 frm=self.send_from |
|
67 |
|
68 if mail.getFrom(): |
|
69 frm = mail.getFrom() |
|
70 |
|
71 tmpmail=copy.deepcopy(mail) |
|
72 tmpmail.content['From'] = frm |
|
73 tmpmail.content['To']=recipient |
|
74 if not self.testmode: |
|
75 smtp.sendmail(frm, recipient, tmpmail.as_string()) |
|
76 return Status(self, "std") |
|
77 finally: |
|
78 smtp.quit() |
|
79 except Exception as e: |
|
80 return Status(self,"std",e) |
|
81 |
|
82 def getSendFunc(self, typ, route): |
|
83 if typ != "mail": |
|
84 raise NoTyp(route) |
|
85 elif route != "std": |
|
86 raise NoRoute(route) |
|
87 return self.send |
|
88 |
|
89 providers["smtp"]=SMTP |
|
90 |