emailto=['xxxx@gmail.com']  ## Provide an Toemail address where the mail need to be sent.You can also provide any number of To eamil address For example: ['tamil@yopmail.com','sensor@yopmail.com']
emailfrom='xxxx@gmail.com' ## Provide the From Email address from which the mail to be send
password='xxxxx'               ##Provide password for from email
smtpserver='smtp.gmail.com'
port=587

msgbody=r'''Hi

The Device is not reboot for more than 48  Hours.

Thank you.'''

cut_off_limit = 48

import os
import re
import ctypes
from subprocess import PIPE, Popen
import socket
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText



def oscmd(command):
    obj = os.popen(command).read()
    return obj.strip()

def computername():
    return os.environ['COMPUTERNAME']

def ipaddress():
    return socket.gethostbyname(socket.gethostname())

subject='%s %s Alert : Device Uptime is More than 48 Hours!!!!!'%(computername(), ipaddress())

def emailreport(subject, emailto,emailfrom,password,smtpserver,port,msgbody):
    msg = MIMEMultipart()
    msg["From"] = emailfrom
    msg["To"] = ",".join(emailto)
    msg["Subject"] = subject
    msg.preamble = subject
    body = MIMEText(msgbody)
    msg.attach(body)       
    try:
        server = smtplib.SMTP(smtpserver,port)
        server.ehlo()
        server.starttls()
        server.login(emailfrom, password)
        server.sendmail(emailfrom, emailto, msg.as_string())
        server.quit()
        return " \nThe email report has been sent to "+msg["To"]
    except Exception as e:
        return e    

data= oscmd('powershell "(get-date) - (gcim Win32_OperatingSystem).LastBootUpTime"')
pattern= re.compile('.*\nHours             : (\d.*)')
m= re.match(pattern,data)
hour= m.group(1)

hour = int(hour)

if hour > cut_off_limit:
    print emailreport(subject,emailto,emailfrom,password,smtpserver,port,msgbody)
    print 'mail sent'

else:
    print "Last System Boot Time in Hours: "+str(hour)

