# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
#
# This file is part of Iro.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# #Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -*- coding: utf-8 -*-
import smtplib
import copy
from functools import partial
from ..validate import vInteger, vEmail,vBool
from ..model.status import Status
from ..config import Option
from .provider import Provider, providers
[docs]class SMTP(Provider):
"""A SMTP Provider to send emails.
This Provider has only one typ ``"mail"`` and one route ``None``.
If :attr:`~iro.offer.provider.Provider.testmode` is **True** no mail will be send, only a connection is created to server.
"""
def __init__(self, name):
options = [
("host", Option(lambda x,y: x, long="Hostname of MTA", must=True)),
("port", Option(partial(vInteger,minv=0),long="Port of the MTA", default=25)),
("user", Option(lambda x,y: x, long="username to login into MTA.",default="")),
("password", Option(lambda x,y: x, long="password to login into MTA.",default="")),
("SSL", Option(vBool,long="use SSL for connection to MTA", default=False)),
("TLS", Option(vBool,long="use TLS for connection to MTA", default=False)),
("send_from", Option(vEmail,long="Emailaddress from which mail will be sended.",must=True)),
]
Provider.__init__(self,name,{"mail":[None]},options)
[docs] def send(self, recipient, mail):
"""sends a mail to recipient
:param string recipient: A valid email address.
:param `iro.model.message.Mail` mail: Mail to send.
:return:
- All went ok -- :class:`iro.model.status.Status` object
- otherwise -- an exception
"""
if not self.testmode:
if self.SSL:
smtp = smtplib.SMTP_SSL(self.host,self.port)
else:
smtp = smtplib.SMTP(self.host,self.port)
if self.TLS:
smtp.starttls()
if not self.user == "":
smtp.login(self.user,self.password)
try:
frm=self.send_from
if mail.getFrom():
frm = mail.getFrom()
tmpmail=copy.deepcopy(mail)
tmpmail.content['From'] = frm
tmpmail.content['To'] = recipient
if not self.testmode:
smtp.sendmail(frm, recipient, tmpmail.as_string())
return Status(self, None)
finally:
smtp.quit()
[docs] def getSendFunc(self, typ, route):
"""returns :meth:`send` method, if typ and route is valid."""
Provider.getSendFunc(self, typ, route)
return self.send
providers["smtp"]=SMTP