Skip to content

Commit

Permalink
Support different payload types when reading emails
Browse files Browse the repository at this point in the history
Bug fix on sender address not having a name attached
Update release_notes.rst
Release beta version
  • Loading branch information
dormant-user committed Oct 26, 2023
1 parent ea84932 commit 9181cec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gmailconnector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
from .validator.address import EmailAddress # noqa: F401
from .validator.validate_email import validate_email # noqa: F401

version = "1.0a"
version = "1.0b"
28 changes: 27 additions & 1 deletion gmailconnector/read_email.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import base64
import binascii
import email
import imaplib
import socket
import warnings
from collections.abc import Generator
from datetime import datetime, timedelta, timezone
from email.header import decode_header, make_header
from email.message import Message
from typing import Iterable, Union

import pytz
Expand Down Expand Up @@ -175,7 +179,29 @@ def get_info(self, response_part: tuple, dt_flag: bool) -> Email:
else:
body = ""
for payload in original_email.get_payload():
body += payload.as_string()
if isinstance(payload, Message):
body += payload.as_string()
elif isinstance(payload, str):
body += payload
elif isinstance(payload, bytes):
try:
decoded = base64.b64decode(payload)
except binascii.Error:
try:
decoded = payload.decode() # encoding is unknown at this point so default to UTF-8
except UnicodeDecodeError:
warnings.warn(
"Unknown encoding type for payload"
)
continue
body += decoded
else:
warnings.warn(
f"Unsupported payload type: {type(payload)}"
)
if len(from_) == 1:
return Email(dictionary=dict(sender=None, sender_email=from_[0].lstrip('<').rstrip('>'),
subject=sub, date_time=receive, body=body))
return Email(dictionary=dict(sender=from_[0], sender_email=from_[1].rstrip('>'),
subject=sub, date_time=receive, body=body))

Expand Down
8 changes: 8 additions & 0 deletions release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Release Notes
=============

v1.0b (10/26/2023)
------------------
- Release beta version for v1

v1.0a (10/26/2023)
------------------
- Prerelease for v1

0.9.1 (08/30/2023)
------------------
- Includes some minor modifications in type hinting
Expand Down

0 comments on commit 9181cec

Please sign in to comment.