sam
sam

Reputation: 203

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

matches = []
for root, dirnames, filenames in os.walk('C:\Users\Desktop\ADI\New folder'):
    for filename in fnmatch.filter(filenames, '*.html'):
        matches.append(os.path.join(root, filename))
        page = filename
        #print filename

        server.quit()

In the above code : Firstly, I am finding the *.html files in a directory. I am finding it, it is working fine for me. Later I want to send that html file as an attached email to some person. I am failing in that. Can someone suggest me how to attach an file to the email and send it to the concerned person ? The above program is WORKING FINE in sending the email to the person, which just prints the name of the file in the email but not able to attach that email and 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'

Upvotes: 0

Views: 1326

Answers (1)

Vivek Sable
Vivek Sable

Reputation: 10223

Need to attach file content to email data.

e.g.

with open(fileToSend) as fp:
    attachment = MIMEText(fp.read(), _subtype=subtype)
attachment.add_header("Content-Disposition", "attachment",\
                      filename=os.path.basename(filename))

msg.attach(attachment)

Do following in your code(replace msg.attach(MIMEText(text)) line with following code):

ctype, encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
    ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)

with open(filename) as fp:
    attachment = MIMEText(fp.read(), _subtype=subtype)
attachment.add_header("Content-Disposition", "attachment",\
                      filename=os.path.basename(filename))

msg.attach(attachment)

Note: filename should be complate path of file which we want to attach in email. e.g. /var/opt/html/report_email.html


If I add multiple receiver email id then it is not working.

  1. if toaddress is string where each email id separated by comma i.e. , e.g. "[email protected],[email protected]" then do like server.sendmail(fromaddress,toaddress.split(','),msg.as_string())

Upvotes: 1

Related Questions