2016-11-10 68 views
0
發送HTML格式的郵件模板

我想用在Django的模板HTML發送電子郵件,但與編碼錯誤錯誤,當我在Django的

我的代碼

# -*- coding: utf-8 -*- 
from django.core.mail import EmailMultiAlternatives 
from django.template.loader import get_template 
from django.template import Context 


d = Context({'nome' : 'Gerento'}) 
htmly = get_template('teste_template.html') 
html_content = htmly.render(d) 
msg = EmailMultiAlternatives('dhfusafi', 'teste', '[email protected]', ['[email protected]']) 
msg.attach_alternative(html_content, 'html/text') 
msg.send() 

一個問題,我當我運行它,我明白了:

Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "teste.py", line 12, in <module> 
    msg.send() 
    File "/home/guilherme/.virtualenvs/martizi-api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send 
    return self.get_connection(fail_silently).send_messages([self]) 
    File "/home/guilherme/.virtualenvs/martizi-api/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 107, in send_messages 
    sent = self._send(message) 
    File "/home/guilherme/.virtualenvs/martizi-api/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 121, in _send 
    message = email_message.message() 
    File "/home/guilherme/.virtualenvs/martizi-api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 256, in message 
    msg = self._create_message(msg) 
    File "/home/guilherme/.virtualenvs/martizi-api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 444, in _create_message 
    return self._create_attachments(self._create_alternatives(msg)) 
    File "/home/guilherme/.virtualenvs/martizi-api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 454, in _create_alternatives 
    msg.attach(self._create_mime_attachment(*alternative)) 
    File "/home/guilherme/.virtualenvs/martizi-api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 387, in _create_mime_attachment 
    Encoders.encode_base64(attachment) 
    File "/usr/lib/python2.7/email/encoders.py", line 45, in encode_base64 
    encdata = _bencode(orig) 
    File "/usr/lib/python2.7/email/encoders.py", line 32, in _bencode 
    value = base64.encodestring(s) 
    File "/usr/lib/python2.7/base64.py", line 315, in encodestring 
    pieces.append(binascii.b2a_base64(chunk)) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 11: ordinal not in range(128) 

我做了一個簡單的測試:我有很多風格改變我的HTML模板,以一個簡單的HTML,只是一個測試,它的工作!所以,我相信這個問題是與我的模板。 塊一張我的html:

<!doctype html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"> 
    <head> 
     <!-- NAME: 1 COLUMN --> 
     <!--[if gte mso 15]> 
     <xml> 
      <o:OfficeDocumentSettings> 
      <o:AllowPNG/> 
      <o:PixelsPerInch>96</o:PixelsPerInch> 
      </o:OfficeDocumentSettings> 
     </xml> 
     <![endif]--> 
     <meta charset="UTF-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <title>*|MC:SUBJECT|*</title> 

    <style type="text/css"> 
     p{ 
      margin:10px 0; 
      padding:0; 
     } 
     table{ 
      border-collapse:collapse; 
     } 
     h1,h2,h3,h4,h5,h6{ 
      display:block; 
      margin:0; 
      padding:0; 
     } 
     img,a img{ 
      border:0; 
      height:auto; 
      outline:none; 
      text-decoration:none; 
     } 
     body,#bodyTable,#bodyCell{ 
      height:100%; 
      margin:0; 
      padding:0; 
      width:100%; 
     } 
     #outlook a{ 
      padding:0; 
     } 
     img{ 
      -ms-interpolation-mode:bicubic; 
     } 
     table{ 
      mso-table-lspace:0pt; 
      mso-table-rspace:0pt; 
     } 
     .ReadMsgBody{ 
      width:100%; 
     } 
     .ExternalClass{ 
      width:100%; 
     } 
     p,a,li,td,blockquote{ 
      mso-line-height-rule:exactly; 
     } 
     a[href^=tel],a[href^=sms]{ 
      color:inherit; 
      cursor:default; 
      text-decoration:none; 
     } 

任何人有任何想法我做什麼?

*對不起,我糟糕的英國:(

TKS)

+0

嘗試編碼呈現的模板ast utf-8:'html_content = htmly.render(d).encode('utf-8')' – Alasdair

+0

謝謝! 顯然它的代碼工作,編碼錯誤不再出現,但郵件沒有到我的郵箱。任何想法? 謝謝! @Alasdair –

+0

對不起,我無法分辨你的郵件爲什麼被髮送,這聽起來像是一個單獨的問題。這取決於您的電子郵件設置和提供商。確保您的電子郵件提供商允許您發送電子郵件作爲您正在使用的地址('no-reply @ teste.com')。 – Alasdair

回答

0

編碼呈現模板爲UTF-8將其連接到電子郵件之前。

html_content = htmly.render(d).encode('utf-8') 

如果你不編碼它,Django會嘗試將它編碼爲ascii,給你錯誤。

+0

謝謝! 這個庫EmailMultiAlternatives不工作。 我試試這個: '# - * - 編碼:UTF-8 - * - 從django.template進口語境 主題django.core.mail進口EmailMessage 從django.template.loader進口get_template = 'EU SOU HTML' 到= [ '[email protected]'] FROM_EMAIL = '[email protected]' CTX = { '諾姆': 'Gerento'} 消息= get_template('confirma_cadastro。 ()()()'。'render。(Context(ctx)) msg = EmailMessage(subject,message,to = to,from_email = from_email) msg.content_subtype ='html' msg.send –