linspector-old/lib/tasks/mail.py

35 lines
1.1 KiB
Python
Raw Normal View History

"""
2013-09-19 21:28:56 +02:00
The mail task.
2013-08-15 23:41:03 +02:00
http://docs.python.org/2/library/email-examples.html
2013-06-18 00:10:14 +02:00
"""
2013-09-19 22:20:42 +02:00
import datetime
import smtplib
from email.mime.text import MIMEText
2013-06-19 23:09:57 +02:00
from lib.tasks.task import Task
2013-06-18 00:10:14 +02:00
2013-06-18 02:58:40 +02:00
class MailTask(Task):
2013-06-18 00:10:14 +02:00
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 nor arguments!")
self.set_task_type(kwargs["type"])
self.recipient = kwargs["args"]["rcpt"]
def execute(self, msg, core):
message = MIMEText(msg)
message['Subject'] = msg
2013-09-19 22:20:42 +02:00
now = datetime.datetime.now()
message['Date'] = now.strftime("%a, %d %b %Y %H:%M:%S")
message['From'] = core["tasks"]["mail"]["from"]
2013-08-15 22:33:15 +02:00
message['To'] = self.recipient
s = smtplib.SMTP(core["tasks"]["mail"]["host"], core["tasks"]["mail"]["port"])
2013-08-15 22:33:15 +02:00
s.sendmail("warning@linspector.org", self.recipient, message.as_string())
s.quit()
2013-06-18 00:10:14 +02:00
def create(taskDict):
return MailTask(**taskDict)