pmclanahan / django-celery-email

A Django email backend that uses a celery task for sending the email.
BSD 3-Clause "New" or "Revised" License
477 stars 108 forks source link

Extra args on EmailMessage no longer working #25

Closed chripede closed 8 years ago

chripede commented 8 years ago

I have my configuration set up like this

EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'
CELERY_EMAIL_BACKEND = "djrill.mail.backends.djrill.DjrillBackend"

meaning that I use Mandrill to send mails.

In 1.0.4 I was able to set extra args on the EmailMessage object to use templates in Mandrill

email.template_name = 'report-{}'.format(get_language())
email.template_content = {}
email.global_merge_vars = {
    'CLIENT': Client.objects.get(slug=client_slug).name,
}

All versions after 1.0.4 no longer sends these arguments all the way to Mandrill

pmclanahan commented 8 years ago

This is a side-effect of the switch from pickling the email object to converting it to JSON for adding to the queue. We could provide hook for custom data that would be serialized and sent to the workers. Would something like the following work for you?

email.extra_data = {
    'template_name': 'report-{}'.format(get_language()),
    'template_content': {},
    'global_merge_vars': {
        'CLIENT': Client.objects.get(slug=client_slug).name,
    },
}

You'd have to change how you get the data slightly, but it would be an easy thing to implement if it'd work.

chripede commented 8 years ago

It would solve my problem sure, but one would need to change the codebase if the email backend is changed, for instance in a test environment. Then it's no longer plug'n'play which would be a shame.

pmclanahan commented 8 years ago

I don't think so. It should work the same in both cases. My solution just moves it to one specific extra attribute on the email object, which would be there in any backend that doesn't replace the object. But my backend would special case that attribute to make sure it's carried through since we're manually transmitting the data via JSON.

chripede commented 8 years ago

Wouldn't I need to change the parameters on EmailMessage back if I switched the backend from celery-email to djrill (mandrill)?

pmclanahan commented 8 years ago

I don't know anything about djrill. I was assuming you could use any extra parameters you wanted, but if you need those specific params from your original question then yes, you're right. Will it only ever be those 3 extra params?

chripede commented 8 years ago

There's a lot of different params that you can use https://djrill.readthedocs.org/en/latest/usage/sending_mail/#mandrill-specific-options

Perhaps it's easier to make it configurable wether to pickle or convert to JSON

btx commented 8 years ago

I've also needed this so I've put together some solution. Hope it helps. Made a pull request https://github.com/pmclanahan/django-celery-email/pull/31

pmclanahan commented 8 years ago

Fixed in #31