#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Nov 11 11:42:09 2018
Algorithm For IW2018 Encrypted Networking Traffic Classification

@author: liutao
"""

# Load libraries
from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import create_engine
import json
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.application import MIMEApplication

app = Flask(__name__)
api = Api(app)

# Send email with alert
class notification(Resource):
    def post(self):
        content = request.get_json(force=True)
        print (content)
        msg = MIMEMultipart()
        msg['Subject'] = "Project notification! Alter action"
        body = "Dear Customer, there are malware packets in the file as below.\n\n"+ content['malware'] + "\n\n Alert Notfication from IW 2018 project." 
        msg.attach(MIMEText(body, 'plain'))

        try:
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.starttls()
            server.login("souce email address", "password")
            server.sendmail("souce email address", "target email address", msg.as_string())
            print('e-mail sent!')
            server.quit()
            return '{ "message":"Alert e-mail sent!" }'
        except KeyboardInterrupt:
            print('You cancelled the operation.')

api.add_resource(notification, '/notification') # Route_1

if __name__ == '__main__':
     app.run(host='0.0.0.0',port='5081')
