How can I send mail to 'Cc'address in smtplib?

Gilles Lenfant glenfant at equod.com.nospam
Sat Oct 21 22:31:21 EDT 2000


Try something like this:

#!/usr/bin/env python
__doc__ = '''Module "ordermailsend"
Function "orderMailSend" sends a mail for an order
Warning: patch the 'quopri.py' module as described in 'README.txt' before
'''
import StringIO, MimeWriter, quopri

def MailSend(server, title, msgTXT, msgHTML, sender, TOrecipients,
CCrecipients, BCCrecipients):
    '''Sends a multipart mail"""
    fullMsg = StringIO.StringIO()
    writer = MimeWriter.MimeWriter(fullMsg)
    writer.addheader('From', sender)
    for rc in TOrecipients:
        writer.addheader('To', rc)
    for rc in CCrecipients
        writer.addheader('Cc', rc)
    for rc in BCCrecipients
        writer.addheader('Bcc', rc)
    writer.addheader('Subject', title)
    writer.addheader('MIME-Version', '1.0')
    writer.flushheaders()
    writer.startmultipartbody('alternative',
'--------=BoundaryEoxxzykttGrrr------')
    # Writing the raw text part of the message
    subpart = writer.nextpart()
    subpart.addheader('Content-Transfer-Encoding', 'quoted-printable')
    subflow = subpart.startbody('text/plain; charset=ISO-8859-1')
    quopri.encode(StringIO.StringIO(msgTXT), subflow, 1)
    # Writing the HTML part of the message
    subpart = writer.nextpart()
    subpart.addheader('Content-Transfer-Encoding', 'quoted-printable')
    subflow = subpart.startbody('text/html; charset=ISO-8859-1')
    quopri.encode(StringIO.StringIO(msgHTML), subflow, 1)
    writer.lastpart()
    # sending the mail
    allrecipients = TOrecipients + CCrecipients + BCCrecipients
    server.sendmail(sender, allrecipients, fullMsg.getvalue())
    fullMsg.close()

# Using the mail
servername = 'an.smtp.server'
title = 'Good news'
msgT = 'I can write mail in HTML'
msgH = '<html><head></head><body><center><h1>I can write mail in
HTML</h1></center></body></html>'
sender = '"john doe" <jdoe at stuff.com>'
toList = ('"My mom" <mom at family.com>', '"My dad" <dad at family.com>')
ccList = ('"My wife" <darling at home.net>', '"My dog" <grrr at home.net>')
bccList =('"Fred doe" <fdoe at dot.com>', 'afriend at somewhere.net')

oserver = smtplib.SMTP(Smtp)
try:
    MailSend (oserver, title, msgT, msgH, sender, toList, ccList, bccList)
except smtplib.SMTPRecipientsRefused, e:
    print 'Error: recipient(s)', e.recipients, 'denied by the server'
except smtplib.SMTPException, e:
    print 'SMTP server has error:', e
else:
   # They should recieve it
server.quit()

WARNING
"quopri.py" module is buggy (python 1.5.2, don't know for others) and does
not encode text as the mail client expect it !
Edit the "quopri.py" module and comment out or remove the first 3 lines of
the "quote" function. It should look like this:
----- quopri.py -----
...
def quote(c):
## if c == ESCAPE:
##  return ESCAPE * 2
## else:
  i = ord(c)
  return ESCAPE + HEX[i/16] + HEX[i%16]
...
--- end quopri.py ---




More information about the Python-list mailing list