mirror of
https://gitlab.com/simple-nixos-mailserver/nixos-mailserver.git
synced 2025-05-17 08:00:49 +05:00
scripts/mail-check: fix typing issues
Replaces the body payload parsing with proper handling for multipart messages.
This commit is contained in:
parent
313f94ed8f
commit
1615c93511
@ -7,6 +7,7 @@ from datetime import datetime, timedelta
|
||||
import email
|
||||
import email.utils
|
||||
import time
|
||||
from typing import cast
|
||||
|
||||
RETRY = 100
|
||||
|
||||
@ -84,7 +85,7 @@ def _read_mail(
|
||||
for _ in range(0, RETRY):
|
||||
print("Retrying")
|
||||
obj.select()
|
||||
typ, data = obj.search(None, '(SINCE %s) (SUBJECT "%s")'%(dt, subject))
|
||||
_, data = obj.search(None, '(SINCE %s) (SUBJECT "%s")' % (dt, subject))
|
||||
if data == [b'']:
|
||||
time.sleep(1)
|
||||
continue
|
||||
@ -99,12 +100,21 @@ def _read_mail(
|
||||
if delete:
|
||||
obj.store(uid, '+FLAGS', '\\Deleted')
|
||||
obj.expunge()
|
||||
message = email.message_from_bytes(raw[0][1])
|
||||
assert raw[0] and raw[0][1]
|
||||
message = email.message_from_bytes(cast(bytes, raw[0][1]))
|
||||
print("Message with subject '%s' has been found" % message['subject'])
|
||||
if show_body:
|
||||
for m in message.get_payload():
|
||||
if m.get_content_type() == 'text/plain':
|
||||
print("Body:\n%s" % m.get_payload(decode=True).decode('utf-8'))
|
||||
if message.is_multipart():
|
||||
for part in message.walk():
|
||||
ctype = part.get_content_type()
|
||||
if ctype == "text/plain":
|
||||
body = cast(bytes, part.get_payload(decode=True)).decode()
|
||||
print(f"Body:\n{body}")
|
||||
else:
|
||||
print(f"Body with content type {ctype} not printed")
|
||||
else:
|
||||
body = cast(bytes, message.get_payload(decode=True)).decode()
|
||||
print(f"Body:\n{body}")
|
||||
break
|
||||
|
||||
if message is None:
|
||||
@ -164,7 +174,7 @@ def send_and_read(args):
|
||||
def read(args):
|
||||
_read_mail(imap_host=args.imap_host,
|
||||
imap_port=args.imap_port,
|
||||
to_addr=args.imap_username,
|
||||
imap_username=args.imap_username,
|
||||
to_pwd=args.imap_password,
|
||||
subject=args.subject,
|
||||
ignore_dkim_spf=args.ignore_dkim_spf,
|
||||
|
Loading…
x
Reference in New Issue
Block a user