Sample code to build rfc822 mail message building

praba kar prabapython at yahoo.co.in
Sat Aug 6 03:45:30 EDT 2005


Dear All,

       I am new to python world. I have pasted my code
which I used it to build rfc822 format mails for
webbased mailing system task(which is like
to yahoo.com web interface).  Now I am asking some
suggestions and guidence regarding this below code.
Still What way I can improve this code. If anyone find
error kindly let me know how to correct it.

# This function to handle attachment files 
def addAttachment(filename,ctype):
    #mimetypes guesses the type of file and stores it
in ctype
    maintype, subtype = ctype.split('/', 1)
    if not os.path.exists(filename): return 0
    fp = open(filename, 'rb')           #open the file
    if maintype == 'text':
        #check for maintype value and encode and
return according to the
        # type of file
        attach = MIMEText(fp.read(),_subtype=subtype)
    elif maintype == 'message' and  subtype ==
"rfc822":
        attach = email.message_from_file(fp)
        attach =  MIMEMessage(attach)
    elif maintype == 'message' and subtype <>
"rfc822":
        attach = email.message_from_file(fp)
    elif maintype == 'image':
        attach = MIMEImage(fp.read(),_subtype=subtype)
    elif maintype == 'audio':
        attach = MIMEAudio(fp.read(),_subtype=subtype)
    else:
        #print maintype, subtype  #if it does not
equal any of the
        #above we print to screen and encode and
return
        attach = MIMEBase(maintype, subtype) #the
encoded value
        attach.set_payload(fp.read())
        encode_base64(attach)

    fp.close()
    filename = os.path.basename(filename)
    attach.add_header('Content-Disposition',
'attachment',filename=filename)
    return attach

#This function base I try to build email message
def create_mail(domain, user, form, from_name):
    to =  cc =  bcc = subject =  body = ''
    attachments = []
    send_addresses = ''
    if form.has_key('to'):
        to = form['to'].value.strip()
        send_addresses = to
    if form.has_key('cc'):
        cc = form['cc'].value.strip()
        send_addresses = to + ',' + cc
    if form.has_key('bcc'):
        bcc = form['bcc'].value.strip()
        send_addresses = to + ',' + cc + ',' + bcc
    if form.has_key('subject'): subject =
form['subject'].value
    if form.has_key('body'): body = form['body'].value
    if form.has_key('attachments[]'): attachments =
form.getlist("attachments[]")

    if not len(attachments) > 0:
    # This header is for non Multipart message
    # I want to know reduce below redundant code    
        msg = MIMEBase('text','html')
        msg['Return-Path'] = user+'@'+domain
        msg['Date'] = formatdate(localtime=1)
        msg['Subject'] =  subject
        msg['From'] = from_name
        msg['To'] = to
        msg["Cc"] = cc
        msg.set_payload(body)
        # Guarantees the message ends in a newline
        msg.epilogue = ''
    else:
        msg = MIMEMultipart()
        msg['Return-Path'] = user+'@'+domain
        msg['Date'] = formatdate(localtime=1)
        msg['Subject'] =  subject
        msg['From'] = from_name
        msg['To'] = to
        msg["Cc"] = cc
        body = MIMEText(body) #Here is the bod
        msg.attach(body)
        # Guarantees the message ends in a newline
        msg.epilogue = ''
        for eachfile in attachments:
            row = eachfile.split(':')
            att_name = row[0]
            att_type = row[1]
            filename =  domaindir + '/' + domain + '/'
+ user + '/temp/upload/' + att_name
            if addAttachment(filename,att_type):
                 attach =
addAttachment(filename,att_type)
                 msg.attach(attach)

    # To send message to all the send_addresses
    for eachid in send_addresses.split(","):
       fh = os.popen('/bin/sendmail  %s'%
(eachid),'w')
       fh.write(msg.as_string())
       fh.close()



	

	
		
__________________________________________________________
Free antispam, antivirus and 1GB to save all your messages
Only in Yahoo! Mail: http://in.mail.yahoo.com



More information about the Python-list mailing list