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)