@@ -431,21 +431,30 @@ def encoded_or_raw(text):
431431 if bcc :
432432 if not isinstance (bcc , (list , tuple )):
433433 bcc = [bcc ]
434+ # body can be None
434435 if body is None :
435436 text = html = None
437+ # body can be (text, html)
436438 elif isinstance (body , (list , tuple )):
437439 text , html = body
438- if body is not None :
439- body = to_unicode (body , "utf8" )
440+ # body can be just text
441+ else :
442+ text = body
443+ html = None
444+ # make sure that, if provided, both text and html bodies are inn unicode
440445 if text is not None :
441446 text = to_unicode (text , "utf8" )
442- if text is None and body is not None :
443- if body .lstrip ().startswith ("<html" ) and body .rstrip ().endswith ("</html>" ):
444- text = self .settings .server == "gae" and body or None
445- html = body
447+ if html is not None :
448+ html = to_unicode (html , "utf8" )
449+ # in case html is provided but text is not
450+ if text is None and html is not None :
451+ # check that html is indeed html
452+ if html .lstrip ().startswith ("<html" ) and html .rstrip ().endswith ("</html>" ):
453+ # on gae also duplicated it into text because that's how GAE email works
454+ text = self .settings .server == "gae" and html or None
446455 else :
447- text = body
448- html = None
456+ # if the html does not contain <html...</html> than it is not html and move it to text
457+ text , html = html , None
449458
450459 if (text is not None or html is not None ) and (not raw ):
451460 # Construct mime part only if needed
0 commit comments