I'm trying to send an email with an excel file attached using smtplib in python. I'm getting an error saying: TypeError: 'str' object is not callable
Here is my code:
email_message = email.MIMEMultipart.MIMEMultipart('mixed')
excel_attach = email.mime.base.MIMEBase('application','vnd.ms-excel')
# Gives error "TypeError: 'str' object is not callable"
excel_attach.set_payload(file(absolute_file_location).read())
email.encoders.encode_base64(excel_attach)
excel_attach.add_header('Content-Disposition','attachment;filename={0}'.format(file))
email_message.attach(excel_attach)
Why isn't this working?
EDIT : This appears to be the source of error.... file(absolute_file_location).read()
. I've determined this by putting it on a different line.
There was a problem with file(absolute_file_location).read()
I don't know what the problem was, but getting the contents by doing something like...
content = None
with open(file, 'r') as f:
content = f.read()
f.close()
excel_attach.set_payload(content)
worked as intended.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.