how to add the html file as an attachment in python? -


matches = [] root, dirnames, filenames in os.walk('c:\users\desktop\adi\new folder'):     filename in fnmatch.filter(filenames, '*.html'):         matches.append(os.path.join(root, filename))         page = filename         #print filename          server.quit() 

in above code : firstly, finding *.html files in directory. finding it, working fine me. later want send html file attached email person. failing in that. can suggest me how attach file email , send concerned person ? above program working fine in sending email person, prints name of file in email not able attach email , send it.

error :

traceback (most recent call last):   file ".\task.py", line 39, in <module>     server.sendmail(fromaddress,toaddress,msg.as_string())   file "c:\python27_3\lib\email\message.py", line 137, in as_string     g.flatten(self, unixfrom=unixfrom)   file "c:\python27_3\lib\email\generator.py", line 83, in flatten     self._write(msg)   file "c:\python27_3\lib\email\generator.py", line 115, in _write     self._write_headers(msg)   file "c:\python27_3\lib\email\generator.py", line 164, in _write_headers     v, maxlinelen=self._maxheaderlen, header_name=h).encode()   file "c:\python27_3\lib\email\header.py", line 410, in encode     value = self._encode_chunks(newchunks, maxlinelen)   file "c:\python27_3\lib\email\header.py", line 370, in _encode_chunks     _max_append(chunks, s, maxlinelen, extra)   file "c:\python27_3\lib\email\quoprimime.py", line 97, in _max_append     l.append(s.lstrip()) attributeerror: 'tuple' object has no attribute 'lstrip' 

need attach file content email data.

e.g.

with open(filetosend) fp:     attachment = mimetext(fp.read(), _subtype=subtype) attachment.add_header("content-disposition", "attachment",\                       filename=os.path.basename(filename))  msg.attach(attachment) 

do following in code(replace msg.attach(mimetext(text)) line following code):

ctype, encoding = mimetypes.guess_type(filename) if ctype none or encoding not none:     ctype = "application/octet-stream" maintype, subtype = ctype.split("/", 1)  open(filename) fp:     attachment = mimetext(fp.read(), _subtype=subtype) attachment.add_header("content-disposition", "attachment",\                       filename=os.path.basename(filename))  msg.attach(attachment) 

note: filename should complate path of file want attach in email. e.g. /var/opt/html/report_email.html


if add multiple receiver email id not working.

  1. if toaddress string each email id separated comma i.e. , e.g. "abc@gmail.com,xyz@gmail.com" server.sendmail(fromaddress,toaddress.split(','),msg.as_string())

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -