Open pupattan opened 4 years ago
I agree that currently support for inlined image is a bit fiddly.
Any chance we can improve the feature (or docs) so that it can be used via mail.send()
@jrief ?
The reason it "is a bit fiddy" is because of the way Django handles templates and Python handles EmailMultiAlternatives
.
What I can offer is a utility which wraps the example code into one function.
Shall I add that to the project?
Can't we port this portion of the code and put it in prepare_email_message()
here? https://github.com/ui/django-post_office/blob/master/post_office/models.py#L96
I'm trying to EmailMultiAlternatives too. I have successfully used the documented:
mail.send(
['recipient@example.com'],
'from@example.com',
template='morning_greeting',
context={'name': 'alice'},
)
but need inline images. Hence I use this snippet and configure as per the docs:
from django.core.mail import EmailMultiAlternatives
subject, body = "Hello", "Plain text body"
from_email, to_email = "no-reply@example.com", "john@example.com"
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
template = get_template('email-template-name.html', using='post_office')
context = {...}
html = template.render(context)
email_message.attach_alternative(html, 'text/html')
template.attach_related(email_message)
email_message.send()
The final email_message.send() returns nothing and silently fails to send. I have tried
settings.EMAIL_BACKEND = 'post_office.EmailBackend'
explicitly to no result.settings.EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend
which correctly sends the emailThe email send, templating and inlining work ok with the django.core.mail.backends.smtp.EmailBackend
.
Am I missing something? It seems post_office never even tries to send the email. Perhaps I need to instruct post_office to send the email or is email_message.send()
expected to do this?
Thanks!
Me again. In exciting news, I've run a debugger and found the problem. The default send priority of DPO is 'medium'. So my mail never got sent in testing. Configuring like this sends the mail
POST_OFFICE = {
# ...
'DEFAULT_PRIORITY' : 'now'
}
Sets the get_default_priority
to now and sends as expected. See #381 for suggestion to set priority via code and/or update docs. See #380 for why priority defaults to medium.
In README you have given example of using inline image with EmailMultiAlternatives. But how to use post_office.mail functionality with inline image ?