I am using email.message
and smtplib
to send emails using python. When an image is sent as an attachment, it raises this error:
AttributeError: 'bytes' object has no attribute 'tell'
Here is the code for the image attachment:
if filetype.lower() in ['jpg','jpeg','png','gif']:
with open(filename, 'rb') as file:
file_data = file.read()
image_type = imghdr.what(file_data)
actual_filename = filename.split('/')[-1]
msg.add_attachment(file_data, maintype='image', subtype=image_type, filename=actual_filename)
Rather doing
you might do
as
imghdr.what(file, h=None)
does