[Django] #27465: Simpler API for EmailMessage(attach=[...])

12 views
Skip to first unread message

Django

unread,
Nov 9, 2016, 7:40:52 AM11/9/16
to django-...@googlegroups.com
#27465: Simpler API for EmailMessage(attach=[...])
------------------------------------------+------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27465>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 9, 2016, 8:11:52 AM11/9/16
to django-...@googlegroups.com
#27465: Allow EmailMessage(attachments=[...]) to load from the filesystem
--------------------------------+--------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => New feature
* component: Uncategorized => Core (Mail)


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:1>

Django

unread,
Nov 9, 2016, 8:32:09 AM11/9/16
to django-...@googlegroups.com
#27465: Allow EmailMessage(attachments=[...]) to load from the filesystem
--------------------------------+--------------------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (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 :-)

--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:2>

Django

unread,
Nov 9, 2016, 12:55:37 PM11/9/16
to django-...@googlegroups.com
#27465: Allow EmailMessage(attachments=[...]) to load from the filesystem
--------------------------------+--------------------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (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()`?

--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:3>

Django

unread,
Nov 10, 2016, 10:45:53 AM11/10/16
to django-...@googlegroups.com
#27465: Allow EmailMessage(attachments=[...]) to load from the filesystem
--------------------------------+--------------------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (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 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).

--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:4>

Django

unread,
Nov 10, 2016, 10:55:38 AM11/10/16
to django-...@googlegroups.com
#27465: Allow EmailMessage(attachments=[...]) to load from the filesystem
--------------------------------+--------------------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:5>

Django

unread,
Nov 10, 2016, 11:23:34 AM11/10/16
to django-...@googlegroups.com
#27465: Allow EmailMessage(attachments=[...]) to load from the filesystem
--------------------------------+--------------------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:6>

Django

unread,
Nov 11, 2016, 2:59:15 AM11/11/16
to django-...@googlegroups.com
#27465: Allow EmailMessage(attachments=[...]) to load from the filesystem
--------------------------------+--------------------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:7>

Django

unread,
Nov 22, 2016, 3:19:38 PM11/22/16
to django-...@googlegroups.com
#27465: Allow EmailMessage to load attachments from the Storage API
--------------------------------+------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:8>

Django

unread,
Apr 14, 2019, 10:34:56 AM4/14/19
to django-...@googlegroups.com
#27465: Allow EmailMessage to load attachments from the Storage API
--------------------------------+----------------------------------------
Reporter: Thomas Güttler | Owner: Emad Mokhtar
Type: New feature | Status: assigned

Component: Core (Mail) | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+----------------------------------------
Changes (by Emad Mokhtar):

* owner: nobody => Emad Mokhtar
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:9>

Reply all
Reply to author
Forward
0 new messages