Opened 8 years ago

Last modified 10 months ago

#27465 new New feature

Allow EmailMessage to load attachments from the Storage API

Reported by: Thomas Güttler Owned by: nobody
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.

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (8)

comment:1 by Tim Graham, 8 years ago

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 by Thomas Güttler, 8 years ago

Yes, I have seen the attach_file() method.

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

comment:3 by Tim Graham, 8 years ago

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 by Thomas Güttler, 8 years ago

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 8 years ago by Thomas Güttler (previous) (diff)

comment:5 by Tim Graham, 8 years ago

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 by Aymeric Augustin, 8 years ago

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 by Thomas Güttler, 8 years ago

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 by Tim Graham, 8 years ago

Summary: Allow EmailMessage(attachments=[...]) to load from the filesystemAllow EmailMessage to load attachments from the Storage API
Triage Stage: UnreviewedAccepted
Note: See TracTickets for help on using tickets.
Back to Top