email1
import win32com.client
import os
from datetime import datetime
import pandas as pd
def confirmation(stri):
stri1 = f"{stri}\n"
file_path = r"C:\Users\AK001027556\Desktop\python_excel_script\Automation\Email\results.txt"
with open(file_path, "a") as file:
file.write(stri1)
def clear_results_file_content():
file_path = r"C:\Users\AK001027556\Desktop\python_excel_script\Automation\Email\results.txt"
with open(file_path, "w") as file:
pass
def trigger_bat_file():
bat_file_path = r"C:\Users\AK001027556\Desktop\python_excel_script\Automation\Practice\step_libraries.bat"
os.system(f'start {bat_file_path}')
def print_excel_file(excel_path):
excel_data = pd.read_excel(excel_path)
print(excel_data)
def send_results_file(to, subject):
result_path = r"C:\Users\AK001027556\Desktop\python_excel_script\Automation\Email\results.txt"
outlook = win32com.client.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)
mail.To = to
mail.Subject = subject
mail.Body = "Test Execution Completed"
mail.Attachments.Add(result_path)
mail.Send()
print("Result file sent")
def check_email():
print("Execution Started")
confirmation("Execution Started")
Email_sender = [
"[email protected]",
"003BF4A6", "D09E19CB", "DF37E2CB"]
Email_subject = ["Start Execution", "S sent a message", "Remote Execution Trail",
"Re: Turn_Light Test Cases Review", "Turn_Light Test Cases Review","Turn_Light Template"]
outputDir_email = r"C:\Users\AK001027556\Desktop\result"
print("step1")
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
unread_messages = messages.Restrict("[Unread] = True")
unread_messages.Sort("[Unread]", True)
print("step2")
outputDir = os.getcwd()
for message in unread_messages:
subject = message.Subject
sender1 = message.SenderEmailAddress
s1 = slice(-11, -3)
sender = sender1[s1]
# body = message.Body
print("step3")
print("Subject : ", subject)
print("Sender : ", sender)
start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_entry = f"Start Time: {start_time}"
confirmation(log_entry)
print("step3")
if (subject in Email_subject) and (sender in Email_sender):
confirmation("The test Execution Started")
message.UnRead = False
print("step4")
attach = ""
try:
s = message.sender
for attachment in message.Attachments:
if attachment.FileName.endswith(".xlsx") or attachment.FileName.endswith(".xls"):
attach = attachment.FileName
attachment.SaveASFile(os.path.join(outputDir, attachment.FileName))
print(f"attachment {attachment.FileName} from {s} saved")
except Exception as e:
print("Error when saving the attachment:" + str(e))
print_excel_file(attach)
# trigger_bat_file()
subject = "result of " + attach +" Execution"
send_results_file("[email protected]", subject)
confirmation("Execution Ended")
break
else:
confirmation("Parameter Error")
#clear_results_file_content()
check_email()