Opened 4 years ago

Last modified 16 months ago

#27465 assigned New feature

Allow EmailMessage to load attachments from the Storage API

Reported by: Thomas Güttler Owned by: Emad Mokhtar
Component: Core (Mail) Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

From the docs: https://docs.djangoproject.com/en/dev/topics/email/#emailmessage-objects

The constructor of EmailMessage takes this as parameter attachments

attachments: A list of attachments to put on the message.
These can be either email.MIMEBase.MIMEBase instances, or (filename, content, mimetype) triples.

Thank you for exposing this API, but I think it could be simpler.

A team mate and I think that it would be nice if you could pass in the file name to the file in the file-system: Example /tmp/random123/xyz.pdf

Maybe optional with a verbose name ('/tmp/random123/xyz.pdf', 'invoice.pdf')

Before starting to code, I want to know if patches for this would get accepted.

Change History (9)

comment:1 Changed 4 years ago by Tim Graham

Component: UncategorizedCore (Mail)
Summary: Simpler API for EmailMessage(attach=[...])Allow EmailMessage(attachments=[...]) to load from the filesystem
Type: UncategorizedNew feature

Did you see the EmailMessage.attach_file() method? It "creates a new attachment using a file from your filesystem. Call it with the path of the file to attach and, optionally, the MIME type to use for the attachment. If the MIME type is omitted, it will be guessed from the filename."

Adding more ways of how __init__()'s attachments parameter is interpreted seems more complex rather than simpler.

comment:2 Changed 4 years ago by Thomas Güttler

Yes, I have seen the attach_file() method.

I don't understand why the most simple use case is not supported :-)

comment:3 Changed 4 years ago by Tim Graham

Unfortunately, the person who committed the original API (2d082a34dc61a832710d98a933858fd2c0059644), Malcolm, has passed away so we can't ask him.

Why can't you use attach_file()?

comment:4 Changed 4 years ago by Thomas Güttler

Yes, I can use attach_file(), but this would mean I have to use a new useless variable.

I would like to use it like this:

EmailMessage(...).send()

BTW, we had difficulties to implement sending of HTML mails which contain inline images AND attached pdf files.

See: http://stackoverflow.com/questions/40389103/create-html-mail-with-inline-image-and-pdf-attachment/40420648#40420648

This is toilet paper programming: wrapping, wrapping, wrapping, but inside still the same dirty stuff.

Don't get me wrong it is not your fault.

Why does this code (creating mails) exist in django at all? Since the API in python is not user friendly. I wanted
to use a different word first, but Python folks use different language (unlike linux kernel hackers).

Back to topic. Please have a look at the ascii art in the stackoverflow answer.

Up to now you can't easily create mails with this structure in Django. It would be even better if you could
create them easily in python (without django).

Last edited 4 years ago by Thomas Güttler (previous) (diff)

comment:5 Changed 4 years ago by Tim Graham

I don't think adding more complexity to how the attachments argument works is a good idea, so you can either make another proposal (not sure what else could be done besides adding another __init__() argument which isn't much of a simplification) or you can ask for other opinions on the DevelopersMailingList.

comment:6 Changed 4 years ago by Aymeric Augustin

If we want to provide a better API here, we should allow passing Django File objects backed by an arbitrary Storage class, not restrict to loading from the filesystem.

Often you want to send dynamically generated documents, and those are in S3, not in the filesystem.

comment:7 Changed 4 years ago by Thomas Güttler

Hi Aymeric, thank you for being interested in this topic.

I think there are many improvements possible in this area (like accessing storages, not only files).

The python api for creating and parsing mails is too low level for me. Like the request library calls himself "http lib for human being", it would
be nice to have a mail library in the same spirit: easy to use.

Maybe there is already good reusable code available. I don't know. Up to now I only use the standard library with Python 2.7.

But is django the best place to solve this? For me django provides: wsgi connection, http request/response, form library, and (I love it) ORM.

comment:8 Changed 4 years ago by Tim Graham

Summary: Allow EmailMessage(attachments=[...]) to load from the filesystemAllow EmailMessage to load attachments from the Storage API
Triage Stage: UnreviewedAccepted

comment:9 Changed 16 months ago by Emad Mokhtar

Owner: changed from nobody to Emad Mokhtar
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top