2014-11-25 28 views
1

send_mail()我們有一個新參數 - html_messageDocs用Django中的html發送電子郵件1.7

我有email.html文件,我想發送我的消息的html版本。我找不到Django 1.7的任何示例。

你能告訴我一個方法,怎麼做?我是否需要使用os.open()我的html文件?

謝謝!

回答

4

render_to_string:加載一個模板,渲染它並返回結果stringhtml_message:如果提供了html_message,則默認消息將替換爲Html消息。

郵件/ HTML的message.html

Hi {{ first_name }}. 

    This is your {{ email }} 

Thank you 

views.py

def mail_function(request): 
    subject = 'Test Mail' 
    from = '[email protected]' 
    to = '[email protected]' 
    c = Context({'email': email, 
       'first_name': first_name}) 
    html_content = render_to_string('mail/html-message.html', c) 
    txtmes = render_to_string('mail/text-message.html', c) 
    send_mail(subject, 
       txtmes, 
       from, 
       [to], 
       fail_silently=False, 
       html_message=html_content)