def _callFUT(self, *args, **kw): from repoze.sendmail.encoding import best_charset return best_charset(*args, **kw)
def to_message(self): """ Figures out all the required steps to finally craft the message you need and return it. The resulting message is also available as a self.base attribute. What is returned is a Python email API message you can use with those APIs. The self.base attribute is the raw lamson.encoding.MailBase. """ del self.base.parts[:] if isinstance(self.Body, MailBase): self.Body.body = encoding.best_charset(self.Body.body)[1] self.Body.content_encoding['Content-Type'] = ('text/plain', {}) if isinstance(self.Html, MailBase): self.Html.body = encoding.best_charset(self.Html.body)[1] self.Html.content_encoding['Content-Type'] = ('text/html', {}) part = self.base if self.Body and self.Html: self.multipart = True if self.attachments: part = MailBase() self.base.parts.append(part) part.content_encoding['Content-Type'] = ( 'multipart/alternative', {}) if self.multipart: self.base.body = None if isinstance(self.Body, MailBase): part.parts.append(self.Body) elif self.Body: part.attach_text(self.Body, 'text/plain') # Per RFC2046, HTML part is last in multipart/alternative if isinstance(self.Html, MailBase): part.parts.append(self.Html) elif self.Html: part.attach_text(self.Html, 'text/html') for args in self.attachments: self._encode_attachment(**args) elif self.Body: if isinstance(self.Body, MailBase): self.base.body = self.Body.body self.base.content_encoding.update(**self.Body.content_encoding) else: self.base.body = self.Body self.base.content_encoding['Content-Type'] = ('text/plain', {}) elif self.Html: if isinstance(self.Html, MailBase): self.base.body = self.Html.body self.base.content_encoding.update(**self.Html.content_encoding) else: self.base.body = self.Html self.base.content_encoding['Content-Type'] = ('text/html', {}) return to_message(self.base)
def test_best_charset_utf_8(self): from repoze.sendmail import encoding value = self.utf_8 best, encoded = encoding.best_charset(value) self.assertEqual(encoded, self.utf_8_encoded) self.assertEqual(best, 'utf_8')
def test_best_charset_latin_1(self): from repoze.sendmail import encoding value = self.latin_1 best, encoded = encoding.best_charset(value) self.assertEqual(encoded, self.latin_1_encoded) self.assertEqual(best, 'latin_1')
def test_best_charset_ascii(self): from repoze.sendmail import encoding value = 'foo' best, encoded = encoding.best_charset(value) self.assertEqual(encoded, b('foo')) self.assertEqual(best, 'ascii')