Skip to content

Commit 9181cec

Browse files
committed
Support different payload types when reading emails
Bug fix on sender address not having a name attached Update release_notes.rst Release beta version
1 parent ea84932 commit 9181cec

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

gmailconnector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
from .validator.address import EmailAddress # noqa: F401
1212
from .validator.validate_email import validate_email # noqa: F401
1313

14-
version = "1.0a"
14+
version = "1.0b"

gmailconnector/read_email.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import base64
2+
import binascii
13
import email
24
import imaplib
35
import socket
6+
import warnings
47
from collections.abc import Generator
58
from datetime import datetime, timedelta, timezone
69
from email.header import decode_header, make_header
10+
from email.message import Message
711
from typing import Iterable, Union
812

913
import pytz
@@ -175,7 +179,29 @@ def get_info(self, response_part: tuple, dt_flag: bool) -> Email:
175179
else:
176180
body = ""
177181
for payload in original_email.get_payload():
178-
body += payload.as_string()
182+
if isinstance(payload, Message):
183+
body += payload.as_string()
184+
elif isinstance(payload, str):
185+
body += payload
186+
elif isinstance(payload, bytes):
187+
try:
188+
decoded = base64.b64decode(payload)
189+
except binascii.Error:
190+
try:
191+
decoded = payload.decode() # encoding is unknown at this point so default to UTF-8
192+
except UnicodeDecodeError:
193+
warnings.warn(
194+
"Unknown encoding type for payload"
195+
)
196+
continue
197+
body += decoded
198+
else:
199+
warnings.warn(
200+
f"Unsupported payload type: {type(payload)}"
201+
)
202+
if len(from_) == 1:
203+
return Email(dictionary=dict(sender=None, sender_email=from_[0].lstrip('<').rstrip('>'),
204+
subject=sub, date_time=receive, body=body))
179205
return Email(dictionary=dict(sender=from_[0], sender_email=from_[1].rstrip('>'),
180206
subject=sub, date_time=receive, body=body))
181207

release_notes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Release Notes
22
=============
33

4+
v1.0b (10/26/2023)
5+
------------------
6+
- Release beta version for v1
7+
8+
v1.0a (10/26/2023)
9+
------------------
10+
- Prerelease for v1
11+
412
0.9.1 (08/30/2023)
513
------------------
614
- Includes some minor modifications in type hinting

0 commit comments

Comments
 (0)