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(f ilename,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.rea d(),_subtype=su btype)
elif maintype == 'message' and subtype ==
"rfc822":
attach = email.message_f rom_file(fp)
attach = MIMEMessage(att ach)
elif maintype == 'message' and subtype <>
"rfc822":
attach = email.message_f rom_file(fp)
elif maintype == 'image':
attach = MIMEImage(fp.re ad(),_subtype=s ubtype)
elif maintype == 'audio':
attach = MIMEAudio(fp.re ad(),_subtype=s ubtype)
else:
#print maintype, subtype #if it does not
equal any of the
#above we print to screen and encode and
return
attach = MIMEBase(mainty pe, subtype) #the
encoded value
attach.set_payl oad(fp.read())
encode_base64(a ttach)
fp.close()
filename = os.path.basenam e(filename)
attach.add_head er('Content-Disposition',
'attachment',fi lename=filename )
return attach
#This function base I try to build email message
def create_mail(dom ain, user, form, from_name):
to = cc = bcc = subject = body = ''
attachments = []
send_addresses = ''
if form.has_key('t o'):
to = form['to'].value.strip()
send_addresses = to
if form.has_key('c c'):
cc = form['cc'].value.strip()
send_addresses = to + ',' + cc
if form.has_key('b cc'):
bcc = form['bcc'].value.strip()
send_addresses = to + ',' + cc + ',' + bcc
if form.has_key('s ubject'): subject =
form['subject'].value
if form.has_key('b ody'): body = form['body'].value
if form.has_key('a ttachments[]'): attachments =
form.getlist("a ttachments[]")
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(loca ltime=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(loca ltime=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(f ilename,att_typ e):
attach =
addAttachment(f ilename,att_typ e)
msg.attach(atta ch)
# 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