Python Question

Error when i send HTML mail template on Django

I am trying to send email using a template HTML on Django, but i having a problem with encode error

My code

# -*- 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()


When i run it, i get it:

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)


I did a simple test: i change my template html with a lot style to a simple html, just a test and it worked! So, i believe this problem is with my template.
A block piece of my 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;
}


Anyone have any idea what i do?

*Sorry for my terrible English :(

tks )

Answer

Encode the rendered template as utf-8 before attaching it to the email.

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

If you don't encode it, Django will try to encode it as ascii, giving you the error.

Comments