2012-07-15 67 views
4

我设置了一个系统Django /芹菜/ Redis。我用EmailMultiAlternatives发送我的HTML和文本电子邮件。如何通过Celery发送HTML邮件?它不断发送文本/平原

当我发送电子邮件作为请求过程的一部分时,电子邮件以HTML格式发送。所有的运行都很好,它包裹了一个函数。下面的代码:

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') 

回答

4

芹菜不影响任务的执行结果。改变任务之后,你是否重新启动了芹菜?芹菜重新加载Python代码很重要。

当你使用EmailMultiAlternativesemail.attach_alternative(body_html, 'text/html'),电子邮件在Content-Type: multipart/alternative;发送和text/html是一个可供选择的一个,这取决于邮件收据渲染过程中选择邮件的内容类型。那么查看程序和芹菜程序之间的收据是否一样?

您可以通过python -m smtpd -n -c DebuggingServer localhost:25直接输出发送邮件来查找实际消息。我已经在我的Mac上测试过了,支持Redis的Celery,从the official doc获得的例子的输出与预期相同。

+0

谢谢。重启似乎有帮助。 – 2012-07-27 00:03:22