python - Error when i send HTML mail template on Django -


i trying send email using template html on django, having problem encode error

my code

# -*- coding: utf-8 -*- django.core.mail import emailmultialternatives django.template.loader import get_template 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 run it, 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 simple test: change template html lot style simple html, test , worked! so, believe problem template. block piece of 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 idea do?

*sorry terrible english :(

tks )

encode rendered template utf-8 before attaching email.

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

if don't encode it, django try encode ascii, giving error.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -