我正在尝试使用Django上的模板HTML发送电子邮件,但我遇到编码错误的问题
我的代码
# -*- 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', 'no-reply@teste.com', ['guilherme@teste.com'])
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 :(得分:0)
在将渲染模板附加到电子邮件之前,将其编码为utf-8。
C:\Program Files\Java\jdk1.8.0_91\jre\bin
如果你不编码,Django会尝试将其编码为ascii,给你错误。