|
15 | 15 | import smtplib |
16 | 16 | from email import message_from_string |
17 | 17 | from email.encoders import encode_base64 |
| 18 | +from email.mime.multipart import MIMEMultipart |
| 19 | +from email.mime.text import MIMEText |
| 20 | +from email.mime.base import MIMEBase |
18 | 21 |
|
19 | 22 | from pydal._compat import * |
20 | 23 |
|
@@ -47,6 +50,31 @@ class Settings: |
47 | 50 | pass |
48 | 51 |
|
49 | 52 |
|
| 53 | +def to_bytes(text, encoding="utf8"): |
| 54 | + """Converts a text (str, bytes, file-like) to encoded bytes""" |
| 55 | + # if the text is not a string or bytes, maybe it is a file |
| 56 | + if not isinstance(text, (str, bytes)): |
| 57 | + text = text.read() |
| 58 | + # if it is a unicode string encode it |
| 59 | + if isinstance(text, str): |
| 60 | + text = text.encode("utf-8") |
| 61 | + # if it is not a unicode string |
| 62 | + elif isinstance(text, bytes) and encoding != "utf-8": |
| 63 | + text = text.decode(encoding).encode("utf-8") |
| 64 | + return text |
| 65 | + |
| 66 | + |
| 67 | +def to_unicode(text, encoding="utf8"): |
| 68 | + """Converts a body (str, bytes, file-like) to unicode""" |
| 69 | + # if the text is not a string or bytes, maybe it is a file |
| 70 | + if not isinstance(text, (str, bytes)): |
| 71 | + text = text.read() |
| 72 | + # if it is a bytes string encode it |
| 73 | + if isinstance(text, bytes): |
| 74 | + text = text.decode(encoding) |
| 75 | + return text |
| 76 | + |
| 77 | + |
50 | 78 | class Mailer: |
51 | 79 | """ |
52 | 80 | Class for configuring and sending emails with alternative text / html |
@@ -379,15 +407,7 @@ def encoded_or_raw(text): |
379 | 407 | # Use multipart/mixed if there is attachments |
380 | 408 | payload_in = MIMEMultipart("mixed") |
381 | 409 | elif raw: |
382 | | - # No encoding configuration for raw messages |
383 | | - if not isinstance(body, basestring): |
384 | | - body = body.read() |
385 | | - if isinstance(body, unicodeT): |
386 | | - text = body.encode("utf-8") |
387 | | - elif not encoding == "utf-8": |
388 | | - text = body.decode(encoding).encode("utf-8") |
389 | | - else: |
390 | | - text = body |
| 410 | + text = to_bytes(body, encoding) |
391 | 411 | # No charset passed to avoid transport encoding |
392 | 412 | # NOTE: some unicode encoded strings will produce |
393 | 413 | # unreadable mail contents. |
@@ -421,19 +441,9 @@ def encoded_or_raw(text): |
421 | 441 |
|
422 | 442 | if (text is not None or html is not None) and (not raw): |
423 | 443 | if text is not None: |
424 | | - if not isinstance(text, basestring): |
425 | | - text = text.read() |
426 | | - if isinstance(text, unicodeT): |
427 | | - text = text.encode("utf-8") |
428 | | - elif not encoding == "utf-8": |
429 | | - text = text.decode(encoding).encode("utf-8") |
| 444 | + text = to_bytes(body, encoding) |
430 | 445 | if html is not None: |
431 | | - if not isinstance(html, basestring): |
432 | | - html = html.read() |
433 | | - if isinstance(html, unicodeT): |
434 | | - html = html.encode("utf-8") |
435 | | - elif not encoding == "utf-8": |
436 | | - html = html.decode(encoding).encode("utf-8") |
| 446 | + html = to_bytes(html, encoding) |
437 | 447 |
|
438 | 448 | # Construct mime part only if needed |
439 | 449 | if text is not None and html: |
|
0 commit comments