Receiver = itsm.getParameter('EmailTo')  ## Provide an Toemail address where the mail need to be sent. the datatype should be a string.
Sender = itsm.getParameter('EmailFrom')  ## Provide the From Email address from which the mail to be send. the datatype should be a string.
Password = itsm.getParameter('Password') ##Provide password for from email. the datatype should be a string.


import os
from subprocess import PIPE, Popen
import smtplib
import socket
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


ip = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]

devicename = os.popen("hostname").read().split("-")[0]

text = "Device Name: %s\nIP: %s\n\n"%(devicename,ip)

def gmail(sender_email,password,receiver,text):
    msg = MIMEMultipart()
    msg["From"] = sender_email
    msg["To"] = receiver
    msg["Subject"] = "Mac OS version details for the DeviceName:%s"%(devicename)
    attachment = MIMEText(text, _subtype="plain")
    attachment.add_header('Content-Disposition', 'attachment', filename="%s_MacOSversion.txt"%(devicename))
    msg.attach(attachment)
    server = smtplib.SMTP("smtp.gmail.com",587)
    server.starttls()
    server.login(sender_email,password)
    server.sendmail(sender_email, receiver, msg.as_string())
    server.quit()
    print("successfully sent the mail")

def ecmd(command):   
    from subprocess import Popen, PIPE
    import ctypes
    obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    return out,err

output,error = ecmd("sw_vers")

if output:
    text += "here is the mac os version details:\n%s"%(output.decode('utf-8').strip())
    gmail(Sender,Password,Receiver,text)
else:
    print(error)