Python 如何通过芹菜发送HTML电子邮件?它一直以文本/纯文本发送
我设置了一个系统Django/芹菜/Redis。我使用EmailMultiAlternatives发送HTML和文本电子邮件 当我在请求过程中发送电子邮件时,电子邮件以HTML格式发送。所有这些都运行良好,并围绕一个函数展开。代码如下:Python 如何通过芹菜发送HTML电子邮件?它一直以文本/纯文本发送,python,django,redis,celery,django-celery,Python,Django,Redis,Celery,Django Celery,我设置了一个系统Django/芹菜/Redis。我使用EmailMultiAlternatives发送HTML和文本电子邮件 当我在请求过程中发送电子邮件时,电子邮件以HTML格式发送。所有这些都运行良好,并围绕一个函数展开。代码如下: def send_email(email, email_context={}, subject_template='', body_text_template='', body_html_template='', from_email=settings.
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()
然而,当我尝试将其作为芹菜任务运行时,如下图所示,它只是以“text/plain”的形式发送。有什么问题吗?或者我能做些什么来了解更多?任何提示或解决方案都将不胜感激
@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')
芹菜不会影响任务的执行结果。更改任务后是否重新启动celeryd?芹菜重新加载Python代码非常重要 当您使用
EmailMultiAlternatives
和email.attach_alternative(正文_html,'text/html')
时,电子邮件以内容类型发送:multipart/alternative
和text/html
是另一个选项,它在呈现期间根据邮件收据选择邮件的内容类型。那么,查看过程和芹菜过程之间的收据是相同的吗
您可以通过python-msmtpd-n-c DebuggingServer localhost:25直接输出发送邮件,以了解实际的消息。我已经在我的Macw/redis支持的芹菜上进行了测试,从中获取的示例的输出与预期的相同