6

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?

1
  • I face a problem when cc the "group email address". The user who exist in group email address have not received the mail.
    – W. Dan
    Commented Mar 31, 2021 at 7:14

1 Answer 1

12

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()        
3
  • So the problem is that this doesn't include the group name in the to field. Many people filter their email based on who it's from and i don't want to muck that up
    – Ethan
    Commented Jun 12, 2013 at 17:31
  • 1
    Understood, I handle my email the same way. I use this exact block of code (albeit with real addresses, subject, and body) and all of the addresses (including the group) show up in the "To" field. Possibly related to the SMTP server your company uses? Try replacing the ','.join with ';'.join ?
    – jsucsy
    Commented Jun 12, 2013 at 20:44
  • @Ethan "Name <[email protected]>" works for the from field, but my Outlook looks up its own name for the to field. Commented Mar 18, 2014 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.