#!/usr/bin/env python
# -*- coding: utf-8 -*-

##This file contains functions to interact with the customer with email in the main.py file##
import geocode
from email.mime.text import MIMEText
from smtplib import SMTP
import os

#For security reasons we storage all the private information in Heroku server as system variables. We will read them for functions 
sender_email = os.environ.get('MAIL_USER', None)
sender_pass = os.environ.get('MAIL_PASS', None)
server_email = os.environ.get('MAIL_SERVER', None)
port_email = os.environ.get('MAIL_PORT', None)
SSL_email = os.environ.get('MAIL_SSL', None)   

#This funcion is used to send an email notification to the customer and check if the IMEI change was unauthorized.
def email_alert(recipient, iccid, customer):
    #We create the email for customer's name and iccid
    message = "<br>Hola %s,<br><br> Se ha detectado una alerta de seguridad de cambio de IMEI en la SIM con iccid = %s<br><br> Si este cambio de IMEI ha sido voluntario, por favor, ignore este mensaje. De lo contrario por favor hago acceda al siguiente link para localizar su SIM y desactivarla: <br><br> <center><a href='https://jasper-alert.herokuapp.com/response'> Localizar y desactivar SIM </a><br><br></center>    Muchas gracias,<br><br>    Equipo de Cisco Jasper<br>" % (customer, iccid)
    message = MIMEText(message, "html", "uft-8")
    message["From"] = sender_email
    message["To"] = recipient
    message["Subject"] = "IMEI change alert"
    #We try to make the conection with the SMTP server
    smtp = SMTP(server_email, port_email)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    print "Conexion exitosa con Gmail"
    print "Concectado a Gmail"
    #We introduce the credentials of the sender in the SMTP server and send the email.
    smtp.login(sender_email, sender_pass)
    smtp.sendmail(sender_email, recipient, message)
    smtp.quit()
    print "fin"
    return "Mensaje enviado"

#This function is used to send the location and iccid of the SIM card that has been changed.
def email_action (recipient, customer, location, iccid,address):
     #We create the email for customer's name, iccid and location of the SIM card
    message = "<br>Hola %s,<br><br> Tras tu confirmación de que ha habido un acceso no autorizado a la SIM con iccid = %s hemos procedido a su localización y desactivación. <br><br> Su ultima posicion antes de la desactivacion era %s, en las coordenadas exactas de %s, %s <br><br> Rogamos se ponga en contacto con el equipo de Jasper para investigar en profundidad el problema<br><br>   Muchas gracias,<br><br>    Equipo de Cisco Jasper<br>" % (customer, iccid, address, location[0], location[1])
    message = MIMEText(message, "html", "uft-8")
    message["From"] = sender_email
    message["To"] = recipient
    message["Subject"] = "Alert responded"
    #We try to make the conection with the SMTP server
    smtp = SMTP(server_email, port_email)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    print "Conexion exitosa con Gmail"
    print "Concectado a Gmail"
    #We introduce the credentials of the sender in the SMTP server and send the email.
    smtp.login(sender_email, sender_pass)
    smtp.sendmail(sender_email, recipient, message)
    smtp.quit()
    print "fin"
    return "Mensaje enviado"




@@ -772,20 +772,26 @@
 customer
+,event
 ):%0D%0A
-
     #We 
@@ -838,16 +838,111 @@
  iccid%0D%0A
+    if event == %22SIM_STATE_CHANGE%22:%0D%0A        message = %0D%0A    elif event == %22IMEI_CHANGE%22:%0D%0A    
     mess
@@ -1429,31 +1429,184 @@
 iccid)%0D%0A    
-message
+elif event == %22DATA_LIMIT%22%0D%0A        message = %0D%0A    elif event == %22PAST24H_SESSION_USAGE_LESSTHAN%22%0D%0A        message = %0D%0A    else%0D%0A        message = %22%22%0D%0A    body
  = MIMEText(
@@ -1628,39 +1628,36 @@
 , %22uft-8%22)%0D%0A    
-message
+body
 %5B%22From%22%5D = sende
@@ -1661,39 +1661,36 @@
 nder_email%0D%0A    
-message
+body
 %5B%22To%22%5D = recipie
@@ -1689,39 +1689,36 @@
  recipient%0D%0A    
-message
+body
 %5B%22Subject%22%5D = %22I
@@ -2128,39 +2128,36 @@
 ail, recipient, 
-message
+body
 )%0D%0A    smtp.quit
@@ -2369,17 +2369,16 @@
 ress):%0D%0A
- 
     #We 

Create email_lib.py