import smtplib
import json
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import datetime

try:
    with open('./t4sensorjson.cfg') as data_file:    
        data = json.load(data_file)
except:
    print "Error 22 on file Open t4sensorjson.cfg"
    exit(22)

emailadd = data["EmailID"]


to = emailadd
gmail_user = 'xxxxxxxxxx@gmail.com'
gmail_password = 'fdsjfhdjhfskjhdfks'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
msg = MIMEMultipart()
msg['Subject'] = 'Freezer Alert on %s' % today.strftime('%b %d %Y')
msg['From'] = 'Freeze Alert'
msg['To'] = ", ".join(to)
#msg['To'] = to
p2 = MIMEText('Here is the graph for last week.', 'plain')
p3 = MIMEText('\n Sent By: \n Raspberry Pi \n Freezer App', 'plain')

fp = open('test.png', 'rb')
img = MIMEImage(fp.read(), 'png')
fp.close()
msg.attach(p2)
msg.attach(img)
msg.attach(p3)

smtpserver.sendmail(gmail_user, to, msg.as_string())
smtpserver.quit()
