|
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 # -*- coding: utf-8 -*- |
|
23 |
|
24 import smtplib |
|
25 import copy |
|
26 from functools import partial |
|
27 |
|
28 from ..validate import vInteger, vEmail,vBool |
|
29 from ..model.status import Status |
|
30 from ..config import Option |
|
31 from .provider import Provider, providers |
|
32 |
|
33 class SMTP(Provider): |
|
34 """A SMTP Provider to send emails. |
|
35 This Provider has only one typ ``"mail"`` and one route ``None``. |
|
36 |
|
37 If :attr:`~iro.offer.provider.Provider.testmode` is **True** no mail will be send, only a connection is created to server. |
|
38 |
|
39 """ |
|
40 def __init__(self, name): |
|
41 options = [ |
|
42 ("host", Option(lambda x,y: x, long="Hostname of MTA", must=True)), |
|
43 ("port", Option(partial(vInteger,minv=0),long="Port of the MTA", default=25)), |
|
44 ("user", Option(lambda x,y: x, long="username to login into MTA.",default="")), |
|
45 ("password", Option(lambda x,y: x, long="password to login into MTA.",default="")), |
|
46 ("SSL", Option(vBool,long="use SSL for connection to MTA", default=False)), |
|
47 ("TLS", Option(vBool,long="use TLS for connection to MTA", default=False)), |
|
48 ("send_from", Option(vEmail,long="Emailaddress from which mail will be sended.",must=True)), |
|
49 ] |
|
50 Provider.__init__(self,name,{"mail":[None]},options) |
|
51 |
|
52 def send(self, recipient, mail): |
|
53 """sends a mail to recipient |
|
54 |
|
55 :param string recipient: A valid email address. |
|
56 :param `iro.model.message.Mail` mail: Mail to send. |
|
57 :return: |
|
58 - All went ok -- :class:`iro.model.status.Status` object |
|
59 - otherwise -- an exception |
|
60 """ |
|
61 if not self.testmode: |
|
62 if self.SSL: |
|
63 smtp = smtplib.SMTP_SSL(self.host,self.port) |
|
64 else: |
|
65 smtp = smtplib.SMTP(self.host,self.port) |
|
66 |
|
67 if self.TLS: |
|
68 smtp.starttls() |
|
69 |
|
70 if not self.user == "": |
|
71 smtp.login(self.user,self.password) |
|
72 try: |
|
73 frm=self.send_from |
|
74 |
|
75 if mail.getFrom(): |
|
76 frm = mail.getFrom() |
|
77 |
|
78 tmpmail=copy.deepcopy(mail) |
|
79 tmpmail.content['From'] = frm |
|
80 tmpmail.content['To'] = recipient |
|
81 if not self.testmode: |
|
82 smtp.sendmail(frm, recipient, tmpmail.as_string()) |
|
83 return Status(self, None) |
|
84 finally: |
|
85 smtp.quit() |
|
86 |
|
87 def getSendFunc(self, typ, route): |
|
88 """returns :meth:`send` method, if typ and route is valid.""" |
|
89 |
|
90 Provider.getSendFunc(self, typ, route) |
|
91 return self.send |
|
92 |
|
93 providers["smtp"]=SMTP |
|
94 |