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 ctypes
import smtplib
import socket
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class HardDiskInfo:

    def __init__(self):
        self.cp_name = os.environ['COMPUTERNAME']
        self.ip = socket.gethostbyname(socket.gethostname())
        
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    
    def gmail(self,sender_email,password,receiver,text):
        msg = MIMEMultipart()
        msg["From"] = sender_email
        msg["To"] = receiver
        msg["Subject"] = "physical hard disk information for the Computer Name: %s and IP: %s"%(self.cp_name,self.ip)
        attachment = MIMEText(text, _subtype="plain")
        attachment.add_header('Content-Disposition', 'attachment', filename="PhysicalHardDiskInfo.txt") 
        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(self,command):      
        with self.disable_file_system_redirection():
            obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
        out, err = obj.communicate()
        ret=obj.returncode
        return ret,out,err
        
    def GettingHDInfo(self):
        self.ecmd('powershell "Set-ExecutionPolicy RemoteSigned"')
        ret,out,err = self.ecmd('powershell Get-PhysicalDisk')
        if ret == 0:
            if out:
                self.gmail(Sender,Password,Receiver,out)
            else:
                print("couldn't get output")
        else:
            print("got an error while executing the command: %s"%(err))

OBJ = HardDiskInfo()

OBJ.GettingHDInfo()