Sample code to build rfc822 mail message building

P

praba kar

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
 
J

Jorgen Grahn

Dear All,

I am new to python world. I have pasted my code
which I used it to build rfc822 format mails for ....
Still What way I can improve this code. If anyone find
error kindly let me know how to correct it. ....
#This function base I try to build email message
def create_mail(domain, user, form, from_name):
to = cc = bcc = subject = body = ''
attachments = []

Document methods with proper grammar, as doc strings, and use more direct
wording:

def create_mail(domain, user, form, from_name):
"""Build and return an RFC 2822 message with (something about
what the parameters do) and an empty body. (More details.)
"""
to = cc = bcc = subject = body = ''
attachments = []

If you feed your code to the 'pydoc' utility, the output should be readable.

Somewhere you also might need to describe what subset of RFC 2822 and MIME
you implement, or what your "mail metaphor" is. Different people think
differently about mails. I, for example, don't know what you mean by
"attachment" -- the MIME RFCs use much richer concepts.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top