I have prepared code for mail automation. It will read the contents of of a CSV file and compare it with mail body and fetch the information and share it via mail.
The if-statement will not work code run: it only executes the else statement. The if-statement works only if we remove the else.
import csv
from ast import Break, If
import win32com.client as client
outlook = client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders.Item("xyx.comt")
Verizon = folder.Folders.Item("Inbox").Folders("Ver")
Inbox =folder.Folders.Item("Inbox")
message = Ver.Items
message = message.GetLast()
csv_file=csv.reader(open("C:\\Users\\Documents\\Ver2022.csv",'r'))
if message.SenderName == 'abc.com' and "INCIDENT" in message.Subject:
#Compare mail body and cvc file
messageBody=str(message.body)
for row in csv_file:
if row[2] in messageBody:
print(row)
#Extrcat data from cvc file and send to mail
NewMsg=message.Forward()
NewMsg.Subject=f"Ver||{row[1:3],row[5:7]}"
NewMsg.Body = f"Hello Team,\n\tPlease find below device deatils:\n\t Site ID :{row[1]}\n\t Device Name: {row[2]}\n\t Circuit ID :{row[3]}\n\t Circuit Type : {row[4]}\n\t Topology: {row[5]}\n\t Site Country: {row[6]}\n\t Site City: {row[7]}\n\t Site Contact details:{row[8]}\n\t Site Address :{row[9]}\n\t\n\nThanks&Regards\nabc"+NewMsg.Body
NewMsg.To=('xyz.com')
NewMsg.Send()
break
else:
NewMsg=message.Forward()
NewMsg.Subject=f"The device is not available in inventory"
NewMsg.Body = f"Hello Team,\n\tThe device is not available in inventory \n\t\nThanks&Regards\nxyz"+NewMsg.Body
NewMsg.To=('abc.com')
NewMsg.Send()
break
message.Move(Inbox)