Closed sidmitra closed 7 years ago
I ended up using MimeBase
instance instead of the list [name, content, mimetype]
here's an example if someone else comes across this.
# Attach files
for attachment in attachments:
name = attachment['name']
file_path = attachment['file_path'] # relative path
docfile = default_storage.open(file_path, 'rb') # if using S3 then we need to open the file using the same storage as it was saved with(in my case default_storage is S3Boto)
if docfile:
part = MIMEBase('application', 'octet-stream')
part.set_payload(docfile.read())
part.add_header(
'Content-Disposition',
'attachment; filename="{0}"'.format(name))
message.attach(part)
Thanks for the followup!
I get the following error when i try to attach files.
I've tried a number of ways to attach files. But essentially it's this
I have to do all the default_storage stuff because my files live on S3 and I need to open it via the storage and then call read(). But i don't think it matters what storage i use. Also docfile.read() returns a bytes string. Note i'm using
python 3.6+
and version2.0.0
of the module.It seems calling
attach()
converts it tostr
. After some debugging, it seems to happen here https://github.com/django/django/blob/master/django/core/mail/message.py#L324It seems here we're expecting contents to be binary(does that mean a bytes string?)
This is where i get the TypeError. Note my attached files are txt.
There could probably be something obvious i'm missing here, on how to attach files. I will explore some more(and try other files) and add some more info later. But any help would be greatly appreciated.