#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('variableName') with that parameter's name
Receiver = itsm.getParameter('Receiver')  ## Provide an Toemail address where the mail need to be sent. the datatype should be a string.
Sender = itsm.getParameter('Sender')  ## 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
import sys
import shutil
import zipfile
from distutils.dir_util import copy_tree
import ctypes
import os
import os,re,sys
import _winreg,difflib,filecmp
import os
import smtplib
import mimetypes
import socket
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
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

def gmail(sender_email,password,receiver,file):
    fileToSend = file 
    msg = MIMEMultipart()
    msg["From"] = sender_email
    msg["To"] = receiver
    msg["Subject"] = "ITSM Logs report"
    
    ctype, encoding = mimetypes.guess_type(fileToSend)
    if ctype is None or encoding is not None:
        ctype = "application/octet-stream"

    maintype, subtype = ctype.split("/", 1)

    if maintype == "text":
        cp = open(fileToSend)
        # Note: we should handle calculating the charset
        attachment = MIMEText(cp.read(), _subtype=subtype)
        cp.close()
    elif maintype == "image":
        cp = open(fileToSend, "rb")
        attachment = MIMEImage(cp.read(), _subtype=subtype)
        cp.close()
    elif maintype == "audio":
        cp = open(fileToSend, "rb")
        attachment = MIMEAudio(cp.read(), _subtype=subtype)
        cp.close()
    else:
        cp = open(fileToSend, "rb")
        attachment = MIMEBase(maintype, subtype)
        attachment.set_payload(cp.read())
        cp.close()
        encoders.encode_base64(attachment)
    attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
    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()

if 'PROGRAMW6432' in os.environ.keys():
    print "yes"
    log_folders=[r"C:\Program Files (x86)\ITarian\Endpoint Manager\rmmlogs"]
else:
    log_folders=[r"C:\Program Files\ITarian\Endpoint Manager\rmmlogs"]
Dest_path = os.path.join(os.environ["PROGRAMDATA"])+r'\CCC'
if os.path.exists(Dest_path):
    Dest_path=Dest_path
else:
    try:
        os.mkdir(Dest_path)
    except:
        pass
for i in range(0,1):
               source_path = log_folders[i]
               copy_tree(source_path, Dest_path)
path_dest=os.path.join(os.environ['PROGRAMDATA'])+r'\\'+os.environ['COMPUTERNAME']+r'.zip'
path_sourc=Dest_path
fantasy_zip = zipfile.ZipFile(path_dest, 'w')
for folder, subfolders, files in os.walk(path_sourc):
    for file in files:
        fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), path_sourc), compress_type = zipfile.ZIP_DEFLATED)
fantasy_zip.close()
fileToSend=path_dest
gmail(Sender,Password,Receiver,fileToSend)
try:
    os.remove(fileToSend)
    shutil.rmtree(Dest_path)
except:
    pass