Celery Error Using EmailMultiAlternatives

I have a wrapper around EmailMultiAlternatives to make the interface a little cleaner (taken almost verbatim from here ):

 class Email(object): ''' Wrapper around the Django core EmailMultiAlternatives that makes it simpler to render txt and html templates. ''' def __init__(self, to, subject): self.to = to self.subject = subject self.html = None self.text = None def _render(self, template, context): return render_to_string(template, context) def render_html(self, template, context): self.html = self._render(template, context) def render_text(self, template, context): self.text = self._render(template, context) def send(self, from_addr=None, fail_silently=False): if isinstance(self.to, basestring): self.to = [self.to] if not from_addr: from_addr = settings.EMAIL_HOST_USER msg = EmailMultiAlternatives( self.subject, self.text, from_addr, self.to ) if self.html: msg.attach_alternative(self.html, 'text/html') msg.send(fail_silently) 

I call it this way:

  if not self.email: warnings.warn('uid:%s has no email address' % self.id) else: context = Context({ 'first_name': self.first_name, 'uid': int_to_base36(self.id), 'token': default_token_generator.make_token(self), 'domain': Site.objects.get_current().domain }) from_email = settings.EMAIL_HOST_USER subject = "Password Reset" email = Email(to=self.email, subject=subject) email.render_text('email/reset_password_email.txt', context) email.render_html('email/reset_password_email.html', context) email.send() 

I use django-celery-email, which simply provides a wrapper around Django, a built-in email sending function to turn it into a celery worker task. However, when I try to run the code, I get the following error:

 TypeError: <django.core.mail.message.EmailMultiAlternatives object at 0x10c20f3d0> is not JSON serializable 

This happens in the context of creating a celery task. I'm not sure what is going on here. When I don't use EmailMultiAlternatives and just use the Django built-in to send_mail (which also wraps up as a celery django-celery-email task), I don't get an error. Thoughts?

+6
source share

All Articles