I am setting up a Django / Celery / Redis system. And I used EmailMultiAlternatives to send my HTML email and text.
When I send an email as part of the request process, the email is sent in HTML format. Everything works well, and it wraps around the function. Here is the code:
def send_email(email, email_context={}, subject_template='', body_text_template='', body_html_template='', from_email=settings.DEFAULT_FROM_EMAIL): # render content subject = render_to_string([subject_template], context).replace('\n', ' ') body_text = render_to_string([body_text_template], context) body_html = render_to_string([body_html_template], context) # send email email = EmailMultiAlternatives(subject, body_text, from_email, [email]) email.attach_alternative(body_html, 'text/html') email.send()
However, when I tried to run it as a Celery task, as shown below, it just sent as "text / plain". What could be the problem? Or what can I do to find out more? Any hints or solutions are welcome.
@task(name='tasks.email_features', ignore_result=True) def email_features(user): email.send_email(user.email, email_context={'user': user}, subject_template='emails/features_subject.txt', body_text_template='emails/features_body.txt', body_html_template='emails/features_body.html')
source share