[Django]-'InMemoryUploadedFile' object has no attribute 'encode'

0👍

This worked for me inside a 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[]')
    for _file in files:
        _file.open()
        email.attach(_file.name, _file.read(), _file.content_type)
        _file.close()
email.send()

Where documents[] is the name of the input html tag:

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

Leave a comment