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.
* 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>
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>
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>
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>
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>
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>
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>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:8>
* owner: nobody => Emad Mokhtar
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27465#comment:9>