-1

I'm able to get a list of multiple recipients in my Outlook email using:

for recipient in emails:
     mail.Recipients.Add(recipient)

But I can't seem to figure out how to put all of those recipients in the BCC field, instead of the To field.

If I try to iterate through all of the mail.Recipients like

mail.Recipients[0].Type = olBCC 

I get an error:

NameError: name 'olBCC' is not defined

What I'd like to do is send out a single email with everyone in the BCC instead of the To field, given that my email message will be exactly the same for every email.

Here is the full code:

 import win32com.client
 from openpyxl import load_workbook
 wb = load_workbook(filename = r"C:\file.xlsx")
 sheets = wb.sheetnames

 ws = wb[sheets[0]]
 rows = ws.max_row
 columns = ws.max_column
 count = 0
 emails = []

 for row in range(1,rows):
     if count != 0:
         break
     for column in range(1,columns):
         if '@blah' in str(ws.cell(row,column).value):
            count = count+1
            email_column = column
            
 for email_row in range(row,rows):
     email = str(ws.cell(email_row,email_column).value)
     if  email not in emails and ('None' not in email):
         if abs(int(ws.cell(email_row,6).value)) <5:
             emails.append(email)         

 outlook = win32com.client.Dispatch('outlook.application')
 mail = outlook.CreateItem(0)

 for recipient in emails:
      mail.Recipients.Add(recipient)

 mail.Subject = 'hi'
 email_body = ("Blah. Blah.") 
 mail.Body = email_body
 mail.Display(True)
  • may be useful [link](https://stackoverflow.com/questions/26322255/parsing-outlook-msg-files-with-python) – gowridev May 26 '21 at 18:31
  • good question, but can you please add the module that you use and the full code so that we can replicate and resolve the issue. – D.L May 26 '21 at 18:31

2 Answers2

0

as @gowridev wrote in the comments.

solution (modded for python3) is here:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(r"C:\test_msg.msg")

print(msg.SenderName)
print(msg.SenderEmailAddress)
print(msg.SentOn)
print(msg.To)
print(msg.CC)
print(msg.BCC)
print(msg.Subject)
print(msg.Body)

count_attachments = msg.Attachments.Count
if count_attachments > 0:
    for item in range(count_attachments):
        print msg.Attachments.Item(item + 1).Filename

del outlook, msg

here is the link: Parsing outlook .msg files with python

D.L
  • 4,339
  • 5
  • 22
  • 45
0

olBCC is 3.

Secondly, Recipients collection is 1 based, not 0.

Thirdly, Recipients.Add returns a Recipient object. Your code ignores the result of the call:

for recipient in emails:
     mail.Recipients.Add(recipient).Type = 3
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • This worked! Thank you! Also, apologies for major flubs. This is only my second day learning Python. But I really appreciate your help. On that note, I don't know if I follow what you mean by "1 based, not 0", but I'm happy that this worked. – BenDerisgrate May 26 '21 at 19:48
  • "1 based" means to access the first recipient in the recipients collection, you need to use mail.Recipients[1]. If the reply answers your question, please mark it as such. Thank you! – Dmitry Streblechenko May 26 '21 at 19:58