emai - Multiple recipenets in "To:"

Miki Tebeka miki.tebeka at zoran.com
Tue Mar 9 13:46:27 EST 2004


Hello All,

Our nightly build system is supposed to send email notification on build 
status to several developers. Using SMTP and email packages I can post 
email to one user at a time, then I try to add multiple email addresses 
in the "To" field only the 1'st person gets the email.

A lot of this code is taken from the email demo.
---
VERBOSE = False
def inform(msg):
     if not VERBOSE:
         return
     print "*", msg
     stdout.flush()

def sendmail(msg, to, attachments=None, **kw):
     '''Send email
         msg - Message
         to - To address
         attachments - List of files to attach
         kw - Other keyword arguments
     '''
     main = MIMEMultipart()
     subject = kw.get("subject", "Email Message")
     inform("Subject = %s" % subject)
     main["Subject"] = subject
     _from = kw.get("from","%s at zoran.co.il" % getuser())
     inform("From = %s" % _from)
     main["From"] = _from
     to = ", ".join(list(to))
     inform("To = %s" % to)
     main["To"] = to
     main.epilogue = ""

     main.attach(MIMEText(msg))
     if attachments:
         for path in attachments:
             if not isfile(path):
                 continue
             # Guess the content type based on the file"s extension.  	
             # Encoding
             # will be ignored, although we should check for simple
             # things like
             # gzip"d or compressed files.
             ctype, encoding = guess_type(path)
             if ctype is None or encoding is not None:
                 # No guess could be made, or the file is encoded
                 # (compressed), so
                 # use a generic bag-of-bits type.
                 ctype = "application/octet-stream"
             maintype, subtype = ctype.split("/", 1)
             if maintype == "text":
                 inform("Attaching %s as text file" % path)
                 fp = open(path)
                 # Note: we should handle calculating the charset
                 msg = MIMEText(fp.read(), _subtype=subtype)
                 fp.close()
             elif maintype == "image":
                 inform("Attaching %s as image file" % path)
                 fp = open(path, "rb")
                 msg = MIMEImage(fp.read(), _subtype=subtype)
                 fp.close()
             elif maintype == "audio":
                 inform("Attaching %s as audio file" % path)
                 fp = open(path, "rb")
                 msg = MIMEAudio(fp.read(), _subtype=subtype)
                 fp.close()
             else:
                 inform("Attaching %s as generic file [%s]" %  \
                         (path, ctype))
                 fp = open(path, "rb")
                 msg = MIMEBase(maintype, subtype)
                 msg.set_payload(fp.read())
                 fp.close()
                 # Encode the payload using Base64
                 encode_base64(msg)
             # Set the filename parameter
             msg.add_header("Content-Disposition", "attachment",
                     filename=basename(path))
             main.attach(msg)
     s = SMTP()
     server = kw.get("server", "mail.zoran.co.il")
     inform("Connecting to %s..." % server)
     s.connect(server)
     msgstr = main.as_string()
     inform("Sending %d bytes..." % len(msgstr))
     s.sendmail(main["From"], main["To"], msgstr)
     s.close()
---

Thanks.
Miki



More information about the Python-list mailing list