import time
from datetime import datetime
import smtplib
import MySQLdb
import sys

# from email.mime.image import MIMEImage

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Define mysql  connection
db = MySQLdb.connect("localhost","root","PASSWORD","septic" )

cursor = db.cursor()

cursor.execute("call sp_Checklogs;")
data = cursor.fetchall()

result = ""
for row in data:
    result = row[0]

cursor.close()
db.close()

if result != "Nothing":
    f_time = datetime.now().strftime('%a %d %b @ %H:%M')

    toaddr = 'RecipientEmailAddress'    # redacted CHANGE ME
    me = 'GMAILACCOUNTFORSENDING' # redacted CHANGE ME
    password = 'PASSWORD' # redacted CHANGE ME
    subject = 'Email from septic.espich.com, Local time ' + f_time

    # Add the From: and To: headers at the start!
    msg = ("From: %s\r\nTo: %s\r\n"
           % (me,toaddr))
    msg = msg + ("Subject: %s\r\n" % subject)
    msg = msg + "Content-Type: text/plain; charset=\"us-ascii\"\r\n"
    msg = msg + "Content-Transfer-Encoding: quoted-printable\r\n"
    msg = msg + "MIME-Version: 1.0\r\n"
    msg = msg + "\r\n"
    msg = msg + result

    try:
        s = smtplib.SMTP('smtp.gmail.com',587)
        s.set_debuglevel(True)
        s.starttls()
        s.login(me,password)
        s.sendmail(me,toaddr,msg)
        s.quit()
    except:
       print ("Error: unable to send email")
