Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email to a Microsoft Exchange group using Python?

Tags:

python

smtplib

I've written a python script to send out emails, but now I'm wondering if it's possible to send emails to Microsoft exchange groups using python? I've tried including the group in the cc and to fields but that doesn't seem to do the trick. It shows up, but doesn't seem to correspond to a group of emails; it's just plain text.

Anyone know if this is possible?

like image 691
Ethan Avatar asked Jun 06 '13 17:06

Ethan


People also ask

How do you send an email to multiple recipients 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 .


1 Answers

This is definitely possible. Your exchange server should recognize it if you treat it as a full address. For example if you want to send it to person1, person2, and group3, use the following:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

address_book = ['[email protected]', '[email protected]', '[email protected]']
msg = MIMEMultipart()    
sender = '[email protected]'
subject = "My subject"
body = "This is my email body"

msg['From'] = sender
msg['To'] = ','.join(address_book)
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
text=msg.as_string()
#print text
# Send the message via our SMTP server
s = smtplib.SMTP('our.exchangeserver.com')
s.sendmail(sender,address_book, text)
s.quit()        
like image 175
jsucsy Avatar answered Oct 08 '22 20:10

jsucsy