Skip to content

Commit

Permalink
fix: adjust decoding for amazon emails (#1025)
Browse files Browse the repository at this point in the history
* fix: adjust decoding for amazon emails

* adjust ssl context

* attempt alternative processing method

* attempt to get_payload with no value

* adjust processing for amazon hub emails as well

* formatting

* update tests to py3.13
  • Loading branch information
firstof9 authored Dec 4, 2024
1 parent b6c90de commit dd73594
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
24 changes: 18 additions & 6 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ async def _test_login(
context = ssl.client_context()
account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context)
elif security == "startTLS":
context = ssl.client_context()
if not verify:
context = ssl.client_context_no_verify()
else:
context = ssl.client_context()
account = imaplib.IMAP4(host=host, port=port)
account.starttls(context)
else:
Expand Down Expand Up @@ -464,7 +467,10 @@ def login(
context = ssl.client_context()
account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context)
elif security == "startTLS":
context = ssl.client_context()
if not verify:
context = ssl.client_context_no_verify()
else:
context = ssl.client_context()
account = imaplib.IMAP4(host=host, port=port)
account.starttls(context)
else:
Expand Down Expand Up @@ -1185,6 +1191,7 @@ def get_amazon_image(
break

if img_url is not None:
_LOGGER.debug("Attempting to download Amazon image.")
# Download the image we found
hass.add_job(download_img(hass, img_url, image_path, image_name))

Expand Down Expand Up @@ -1277,9 +1284,10 @@ def amazon_hub(account: Type[imaplib.IMAP4_SSL], fwds: Optional[str] = None) ->

# Get combo number from message body
try:
email_msg = quopri.decodestring(
str(msg.get_payload(0))
) # msg.get_payload(0).encode('utf-8')
if msg.is_multipart():
email_msg = quopri.decodestring(str(msg.get_payload(0)))
else:
email_msg = quopri.decodestring(str(msg.get_payload()))
except Exception as err:
_LOGGER.debug("Problem decoding email message: %s", str(err))
continue
Expand Down Expand Up @@ -1445,11 +1453,15 @@ def get_items(
order_number.append(found[0])

try:
email_msg = quopri.decodestring(str(msg.get_payload(0)))
if msg.is_multipart():
email_msg = quopri.decodestring(str(msg.get_payload(0)))
else:
email_msg = quopri.decodestring(str(msg.get_payload()))
except Exception as err:
_LOGGER.debug(
"Problem decoding email message: %s", str(err)
)
_LOGGER.error("Unable to process this email. Skipping.")
continue
email_msg = email_msg.decode("utf-8", "ignore")

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.12
python_version = 3.13
show_error_codes = true
ignore_errors = true
follow_imports = silent
Expand Down
1 change: 1 addition & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ async def test_amazon_search_delivered(
"testfilename.jpg",
"amazon.com",
)
await hass.async_block_till_done()
assert (
"Amazon email search address: ['order-update@amazon.com', 'update-bestelling@amazon.com', 'versandbestaetigung@amazon.com']"
in caplog.text
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[tox]
skipsdist = true
envlist = py312, lint, mypy
envlist = py312, py313, lint, mypy
skip_missing_interpreters = True
ignore_basepython_conflict = True

[gh-actions]
python =
3.12: py312, lint, mypy
3.12: py312
3.13: py313, lint, mypy

[testenv]
pip_version = pip>=21.0,<22.1
Expand Down

0 comments on commit dd73594

Please sign in to comment.