1

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)
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 1
    so what happens if you print out the `row[2]` variable and the `messageBody` variable directly below the for loop? I think one of the two is not what you expect. Note that we cannot help you as we do not know what the contents of the CSV file are, nor do we know what a sample `messageBody` is. – Edo Akse Jun 23 '22 at 15:34
  • show example of your messagebody and row[2] data – 0m3r Jun 23 '22 at 21:21
  • We have device name in messagebody and details of that device is mentioned in cvc file. – Tejshree Bhosale Jun 28 '22 at 12:19
  • So when we received any mail from vender ...python read device name from messagebody and compare with row 2 in cvc file where all device names are mentioned if code received device name in cvc file row 2 then it return row via mail – Tejshree Bhosale Jun 28 '22 at 12:21
  • If I remove else statement code give me below proper output - – Tejshree Bhosale Jun 28 '22 at 12:23
  • Hello Team, Please find below device deatils: Site ID :PL-10011-Opole01 Device Name: atosagn-pl10011-32654571e003 Circuit ID :MEO15515 Circuit Type : INET1-OBS Topology: SLA1 Site Country: Poland Site City: Opole Site Contact details: Site Address :Oleska 121 – Tejshree Bhosale Jun 28 '22 at 12:23

1 Answers1

1

It is not clear what values are used in the following condition:

if row[2] in messageBody:

Be aware, you may use the HTMLBody property which returns an HTML markup which represents the message body or use the Word object model. The WordEditor of the Inspector class returns a Document instance which represents your item's message body, so you can use Word properties and methods.


The Outlook object model supports three main ways of dealing/customizing the message body:

  1. The Body property returns or sets a string representing the clear-text body of the Outlook item.
  2. The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • We have device name in messagebody and details of that device is mentioned in cvc file. So when we received any mail from vender ...python read device name from messagebody and compare with row 2 in cvc file where all device names are mentioned if code received device name in cvc file row 2 then it return row via mail – – Tejshree Bhosale Jun 28 '22 at 12:26
  • If I remove else statement code give me below proper output - – Hello Team, Please find below device deatils: Site ID :PL-10011-Opole01 Device Name: atosagn-pl10011-32654571e003 Circuit ID :MEO15515 Circuit Type : INET1-OBS Topology: SLA1 Site Country: Poland Site City: Opole Site Contact details: Site Address – Tejshree Bhosale Jun 28 '22 at 12:26