I am trying to automate a mass e-mailing and need to change the 'From' field in Outlook. The e-mail cannot be 'Sent on Behalf of' per compliance reasons. When I attempted this solution, I received the following error...
pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 1)
This error is referencing the mail._oleobj_.Invoke(*(64209, 0, 8, 0, email_from))
line. Code snippet is below. Let me know what I have wrong or if there is a better way.
to = 'jsmith@external_domain.com'
cc = 'my_group_e-mail@my_domain.com'
subject = personalized_subject_from_data_source
body = f'''personalized body from other data source with a {table}'''
outlook = win32.Dispatch('outlook.application')
# adding the code suggested by h0r53:
email_from = None
for account in outlook.Session.Accounts:
if account.DisplayName == 'why_i_asked_email@my_domain.com':
email_from = account
break
mail = outlook.CreateItem(0)
# email_from = 'why_i_asked_email@my_domain.com'
mail._oleobj_.Invoke(*(64209, 0, 8, 0, email_from))
mail.To = to
mail.CC = cc
mail.Subject = subject
mail.HTMLbody = body
mail.Display(True)
EDIT
After adding the code suggested by h0r53, I still receive the same error. the variable account
is listed as <COMObject <unknown>>
and thus never satisfies the logic gate and when the for
iteration completes, email_from == None
and thus throws the same error. Any further help is appreciated
Thanks!!