2014-12-19 158 views
0

我创建了一个电子邮件模板在HTML中即时通讯发送电子邮件使用下面的视图工作正常。但是,当我收到电子邮件的RAW格式的desplaying。我可否知道错误是什么?django电子邮件模板

def confirmed_email_notification(sender, **kwargs): 
    """ 
    Sends an email notification to the shop owner when a new order is 
    completed. 
    """ 
    print "EMAIL NOTIFICATION " 
    subject_template_name = 'shop_simplenotifications/confirmed_subject.txt' 
    body_template_name = 'shop_simplenotifications/confirmed_body.html' 
    request = kwargs.get('request') 
    order = kwargs.get('order') 
    subject = loader.render_to_string(
     subject_template_name, 
     RequestContext(request, {'order': order}) 
    ) 
    subject = subject.join(subject.splitlines()) 
    body = loader.render_to_string(
     body_template_name, 
     RequestContext(request, {'order': order}) 
    ) 
    from_email = getattr(settings, 'SN_FROM_EMAIL', 
         settings.DEFAULT_FROM_EMAIL) 
    owners = getattr(settings, 'SN_OWNERS', settings.ADMINS) 
    send_mail(subject, body, from_email, 
       [owner[1] for owner in owners], fail_silently=False) 
    print body 
    print [owner[1] for owner in owners] 
confirmed.connect(confirmed_email_notification) 

回答

0

如果你使用Django 1.7,你可以通过邮件的HTML版本send_mail()html_message说法,CF https://docs.djangoproject.com/en/1.7/topics/email/#send-mail

否则,您可以使用EmailMultiAlternatives类如下记载: https://docs.djangoproject.com/en/1.6/topics/email/#sending-alternative-content-types

或使用EmailMessage类,并将content_subtype属性设置为“html”。

只是一个侧面说明:我真的怀疑subject.join(subject.splitlines())做你所期望的:

>>> subject = "hello Huston\nWe have a\nproblem" 
>>> print subject.join(subject.splitlines()) 
hello Hustonhello Huston 
We have a 
problemWe have ahello Huston 
We have a 
problemproblem 
>>>