linspector-old/linspector/tasks/alert/mail.py

78 lines
2.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
2013-10-08 00:08:47 +02:00
http://docs.python.org/2/library/email-examples.html#
Copyright (c) 2011-2013 by Johannes Findeisen and Rafael Timmerberg
2013-10-08 00:08:47 +02:00
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-06-18 00:10:14 +02:00
"""
2013-09-19 22:20:42 +02:00
import datetime
import smtplib
2013-11-04 00:08:59 +01:00
import time
from email.mime.text import MIMEText
from logging import getLogger
from linspector.tasks.task import Task
2013-06-18 00:10:14 +02:00
logger = getLogger(__name__)
2013-06-18 02:58:40 +02:00
class MailTask(Task):
2013-06-18 00:10:14 +02:00
def __init__(self, **kwargs):
super(MailTask, self).__init__(**kwargs)
args = self.get_arguments()
if "sender" in args:
self.sender = args["sender"]
else:
raise Exception("There is no sender set")
if "rcpt" in args:
self.rcpt = args["rcpt"]
else:
raise Exception("There is no rcpt set")
self.host = "localhost"
if "host" in args:
self.host = args["host"]
self.port = 25
if "port" in args:
self.port = args["port"]
# ...and so on for all possible args
def execute(self, msg):
logger.debug("Eecuting Task!")
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'] = self.sender
message['To'] = self.rcpt
s = smtplib.SMTP(self.host, self.port)
#s.login(self.userName, self.password)
s.sendmail(self.sender, self.rcpt, message.as_string())
s.quit()
2013-06-18 00:10:14 +02:00
def create(kwargs):
return MailTask(**kwargs)