26

I am using win32com to send emails after my code is done. However, I cannot figure out how to send it to more than 1 person. Now I can only add myself in cc, but not in the recipient list.

Here is my code:

import win32com.client
import datetime as date

olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = 'Hi'
newMail.Body = 'Hi'
newMail.To = 'Amy'
newMail.CC = 'Bob'    
newMail.Send()

However if I try this:

newMail.To = ['Amy','Bob']

An error occurs:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Office Outlook', u'Type Mismatch: Cannot coerce parameter value. Outlook cannot translate your string.', None, 0, -2147352571), 1)

Can anyone help?

lsheng
  • 3,539
  • 10
  • 33
  • 43

2 Answers2

31

Try separating by semicolons:

newMail.To = 'Amy; john; sandy'

If you do a web search for "outlook interop createitem" you can find the docs for MailItem.To where this is explained.

Update: this is not an Outlook script, it is a Python script that uses Python's win32com module to control Outlook. The docs I'm referring to are the VB/C# docs for Outlook's COM interface (for example the possible values of OlItemType).

Oliver
  • 27,510
  • 9
  • 72
  • 103
  • Thanks I actually dont know whats going on inside this module, and I havent written outlook scripts before. Is MailItem.To a built in function in Outlook? If so, I think for most problems I can just go for Outlook Docs. – lsheng Mar 28 '14 at 02:59
  • @Twinkle If this worked please accept answer so we know this resolved issue. I will clarify answer. – Oliver Mar 28 '14 at 05:55
  • 2
    If you want it it be more dynamic then you should try the following: newMail.To = ";".join([i for i in to if isinstance(to, list) == True]). – Naufal Oct 04 '18 at 13:57
  • How to add a specific sender name if required? – Anuj Masand Mar 05 '20 at 12:54
0

Convert the list to a string, make sure you add the ';' at the end of each recipient:

newMail.To = "".join(<your_list>)
  • Shouldn't this be `newMail.To = ";".join()` (including the semicolon in the join statement) – PyHP3D Apr 04 '22 at 20:34