~cedric/guardian

95f7b519708f0f94c7de8e28e5ef0e4ad9739e7a — Cédric Bonhomme 3 years ago fffe52d
added function in order to send notification via email
2 files changed, 21 insertions(+), 5 deletions(-)

M guardian/conf.py
M guardian/notification.py
M guardian/conf.py => guardian/conf.py +5 -5
@@ 16,8 16,8 @@ IRC_CHANNEL = config.get("irc", "channel")
IRKER_HOST = config.get("irc", "host")
IRKER_PORT = int(config.get("irc", "port"))

MAIL_FROM = config.get('email','mail_from')
MAIL_TO = [config.get('email','mail_to')]
SMTP_SERVER = config.get('email','smtp')
USERNAME =  config.get('email','username')
PASSWORD =  config.get('email','password')
MAIL_FROM = config.get("email", "mail_from")
MAIL_TO = [config.get("email", "mail_to")]
SMTP_SERVER = config.get("email", "smtp")
USERNAME = config.get("email", "username")
PASSWORD = config.get("email", "password")

M guardian/notification.py => guardian/notification.py +16 -0
@@ 3,6 3,8 @@

import json
import socket
import smtplib
from email.mime.text import MIMEText

from guardian import conf



@@ 15,3 17,17 @@ def irker(message):
        s.sendall(json.dumps(data).encode("utf-8"))
    except socket.error as e:
        sys.stderr.write("irkerd: write to server failed: %r\n" % e)


def mail(mfrom, mto, message):
    """Send the notification via mail."""
    email = MIMEText(message, "plain", "utf-8")
    email["From"] = mfrom
    email["To"] = mto
    email["Subject"] = "Guardian : Alert"
    # email['Text'] = message

    server = smtplib.SMTP(conf.SMTP_SERVER)
    server.login(conf.USERNAME, conf.PASSWORD)
    server.send_message(email)
    server.quit()