From 4be1a6ecb9eb8ecae612ba1b5c18bf38c796bdbf Mon Sep 17 00:00:00 2001 From: RafTim Date: Sun, 3 Nov 2013 04:04:28 +0100 Subject: [PATCH] changed mail task according to new config Q --- linspector/tasks/mail.py | 30 ++++++++++++++++++++---------- linspector/tasks/task.py | 3 +++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/linspector/tasks/mail.py b/linspector/tasks/mail.py index cca016e..921821e 100644 --- a/linspector/tasks/mail.py +++ b/linspector/tasks/mail.py @@ -33,13 +33,23 @@ logger = getLogger(__name__) class MailTask(Task): + + def __init__(self, **kwargs): - if not "type" in kwargs: - raise Exception("'type' not in typeDict " + str(kwargs)) - if not "args" in kwargs: - raise Exception("typeDict " + str(kwargs) + " has no arguments!") - self.set_task_type(kwargs["type"]) - self.recipient = kwargs["args"]["rcpt"] + mandatory_args = ["host", "password", "from", "username"] + for arg in mandatory_args: + if not arg in kwargs: + self.raise_config_exception(kwargs, arg) + self.host = kwargs["host"] + self.password = kwargs["password"] + self.fromName = kwargs["from"] + self.userName = kwargs["username"] + self.port = 25 + if "port" in kwargs: + self.port = kwargs["port"] + + #self.set_task_type(kwargs["type"]) + #self.recipient = kwargs["args"]["rcpt"] def execute(self, msg, core): message = MIMEText(msg) @@ -47,11 +57,11 @@ class MailTask(Task): now = datetime.datetime.now() message['Date'] = now.strftime("%a, %d %b %Y %H:%M:%S") #TODO: totally unstable just to use values from core. make checks before...! - message['From'] = core["tasks"]["mail"]["from"] + message['From'] = self.fromName message['To'] = self.recipient - s = smtplib.SMTP(core["tasks"]["mail"]["host"], core["tasks"]["mail"]["port"]) - s.login(core["tasks"]["mail"]["username"], core["tasks"]["mail"]["password"]) - s.sendmail(core["tasks"]["mail"]["from"], self.recipient, message.as_string()) + s = smtplib.SMTP(self.host, self.port) + s.login(self.userName, self.password) + s.sendmail(self.fromName, self.recipient, message.as_string()) s.quit() diff --git a/linspector/tasks/task.py b/linspector/tasks/task.py index 915d115..82be48f 100644 --- a/linspector/tasks/task.py +++ b/linspector/tasks/task.py @@ -25,6 +25,9 @@ logger = getLogger(__name__) class Task: + def raise_config_exception(self, typeDict, name): + raise Exception("typeDict " + str(typeDict) + " has no " + str(name) + " argument!") + def set_task_type(self, taskType): self._taskType = taskType