python - 'InMemoryUploadedFile' object has no attribute 'encode' -


i'm trying send email attachment in django. file request.file['file']object (inmemoryuploadedfile type). create message emailmessage(...) , attach file message.attach(f.name, f.read(), f.content_type).

sending email fails given error: 'inmemoryuploadedfile' object has no attribute 'encode'

this worked me inside form definition in forms.py

from django.core.mail import emailmultialternatives  email = emailmultialternatives(     subject='some subject',     from_email='from_address@some_domain.com',     to=['recipient1@another_domain.com'],     body='some html content') email.content_subtype = "html" if hasattr(self.files, 'getlist'):     files = self.files.getlist('document[]')     _file in files:         _file.open()         email.attach(_file.name, _file.read(), _file.content_type)         _file.close() email.send() 

where documents[] name of input html tag:

<input name="document[]" id="file" type="file">


Comments