i'm creating emails in python , i'd have html, text, , attachment. code 'working', though outputs shown outlook either html or text, while showing other 'part' (email or txt) attachment. i'd have robust-ness of both email , text versions along file attachment.
is there fundamental limitation or making mistake?
#!/usr/bin/env python3 import smtplib,email,email.encoders,email.mime.text,email.mime.base email.mime.multipart import mimemultipart email.mime.text import mimetext # me == email address # == recipient's email address me = "me@me.com" = "you@you.com" # create message container - correct mime type multipart/alternative. msg = mimemultipart('mixed') msg['subject'] = "msg" msg['from'] = me msg['to'] = # create body of message (a plain-text , html version). text = "hi\nthis text-only" html = """\ <html> email</html> """ part1 = mimetext(text, 'plain') part2 = mimetext(html, 'html') #attach excel file: fp = open('excelfile.xlsx', 'rb') file1=email.mime.base.mimebase('application','vnd.ms-excel') file1.set_payload(fp.read()) fp.close() email.encoders.encode_base64(file1) file1.add_header('content-disposition','attachment;filename=anexcelfile.xlsx') # attach parts message container. # according rfc 2046, last part of multipart message, in case # html message, best , preferred. msg.attach(part2) msg.attach(part1) msg.attach(file1) composed = msg.as_string() fp = open('msgtest.eml', 'w') fp.write(composed) fp.close()
i found has in fact been answered. strange how search feature less effective 'related' boxes.
Comments
Post a Comment