Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email to multiple recipients using win32com module in Python

Tags:

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?

like image 382
lsheng Avatar asked Mar 27 '14 08:03

lsheng


People also ask

How do I send an email to multiple users in Python?

To send email to multiple recipients using Python smtplib, we can use the sendmail method. We create the SMTP instance by using the SMTP server address as the argument. Then we create the message with the MIMEText class. We combine the recipients into a string with join .

How do you add multiple recipients in Python?

sendmail() function. In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.

How do I use win32com client in Python?

Run ' win32com\client\makepy.py ' (eg, run it from the command window, or double-click on it) and a list will be presented. Select the Type Library ' Microsoft Word 8.0 Object Library ' From a command prompt, run the command ' makepy.py "Microsoft Word 8.0 Object Library" ' (include the double quotes).

How do I send multiple emails in SMTP?

For smtp you need to have port number, logon email address and in To:"[email protected];[email protected]" …


1 Answers

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).

like image 109
Oliver Avatar answered Sep 18 '22 15:09

Oliver