467,923 Members | 1,853 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Sample code to build rfc822 mail message building

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
Aug 6 '05 #1
  • viewed: 2179
Share:
1 Reply
On Sat, 6 Aug 2005 08:45:30 +0100 (BST), praba kar <pr*********@yahoo.co.in> wrote:
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

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Aug 7 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dafella | last post: by
reply views Thread by Chris McKeever | last post: by
reply views Thread by Michael B Allen | last post: by
1 post views Thread by Malcolm Dew-Jones | last post: by
4 posts views Thread by Nemesis | last post: by
7 posts views Thread by dm_dal | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.