Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming you're using Python 2.x, remember: there are two types of strings: <code>str</code> and <code>unicode</code>. <code>str</code> are byte strings, whereas <code>unicode</code> are unicode strings. <code>unicode</code> strings can be used to represent text in any language, but to store text in a computer or to send it via email, you need to represent that text using bytes. To represent text using bytes, you need an coding format. There are many coding formats, Python uses <em>ascii</em> by default, but <em>ascii</em> can only represent a few characters, mostly english letters. If you try to encode a text with other letters using <em>ascii</em>, you will get the famous "outside ordinal 128". For example:</p> <pre><code>&gt;&gt;&gt; u'Cerón'.encode('ascii') Traceback (most recent call last): File "&lt;input&gt;", line 1, in &lt;module&gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 3: ordinal not in range(128) </code></pre> <p>The same happens if you use <code>str(u'Cerón')</code>, because Python uses <em>ascii</em> by default to convert <code>unicode</code> to <code>str</code>. </p> <p>To make this work, you have to use a different coding format. <em>UTF-8</em> is a coding format that can express any unicode text as bytes. To convert the <code>u'Cerón'</code> unicode string to bytes you have to use:</p> <pre><code>&gt;&gt;&gt; u'Cerón'.encode('utf-8') 'Cer\xc3\xb3n' </code></pre> <p>No errors this time.</p> <p>Now, back to your email problem. I can see that you're using <code>MIMEText</code>, which accepts an already encoded <code>str</code> argument, in your case is the <code>html</code> variable. <code>MIMEText</code> also accepts an argument specifying what kind of encoding is being used. So, in your case, if <code>html</code> is a unicode string, you have to encode it as <code>utf-8</code> and pass the charset parameter too (because <code>HTMLText</code> uses ascii by default):</p> <pre><code>part1 = MIMEText(html.encode('utf-8'), 'html', 'utf-8') </code></pre> <p>But be careful, because if <code>html</code> is already a <code>str</code> instead of <code>unicode</code>, then the encoding will fail. This is one of the problems of Python 2.x, it allows you to encode an already encoded string but it throws an error.</p> <p>Another problem to add to the list is that <em>utf-8</em> is compatible with <em>ascii</em> characters, and Python will always try to automatically encode/decode strings using <em>ascii</em>. If you're not properly encoding your strings, but you only use <em>ascii</em> characters, things will work fine. However, if for some reason some <em>non-ascii</em> characters slips into your message, you will get the error, this makes errors harder to detect.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload