2012-07-15 66 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