ui / django-post_office

A Django app that allows you to send email asynchronously in Django. Supports HTML email, database backed templates and logging.
MIT License
1.01k stars 268 forks source link

How to use EmailMultiAlternatives with post_office.mail #346

Open pupattan opened 4 years ago

pupattan commented 4 years ago

In README you have given example of using inline image with EmailMultiAlternatives. But how to use post_office.mail functionality with inline image ?

selwin commented 3 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 ?

jrief commented 3 years ago

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?

selwin commented 3 years ago

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 CleanShot 2021-01-19 at 17 17 35@2x

brettbeeson commented 2 years ago

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

The 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!

brettbeeson commented 2 years ago

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.