[mail] Partial fix for trashing emails
This commit is contained in:
@ -18,36 +18,36 @@ def extract_text(body_parts):
|
||||
|
||||
def walk(parts):
|
||||
for part in parts:
|
||||
content_type = part.get('content-type', '')
|
||||
content = part.get('content')
|
||||
content_type = part.get("content-type", "")
|
||||
content = part.get("content")
|
||||
if isinstance(content, list):
|
||||
walk(content)
|
||||
elif content:
|
||||
if 'text/plain' in content_type:
|
||||
if "text/plain" in content_type:
|
||||
plain.append(content)
|
||||
elif 'text/html' in content_type:
|
||||
elif "text/html" in content_type:
|
||||
html.append(content)
|
||||
|
||||
walk(body_parts)
|
||||
return '\n'.join(plain) if plain else '\n'.join(html)
|
||||
return "\n".join(plain) if plain else "\n".join(html)
|
||||
|
||||
|
||||
def send_ntfy(headers, payload):
|
||||
data = payload.encode('utf-8')
|
||||
data = payload.encode("utf-8")
|
||||
for attempt in range(3):
|
||||
conn = http.client.HTTPSConnection(NTFY_HOST, timeout=15)
|
||||
try:
|
||||
conn.putrequest("POST", NTFY_PATH, skip_host=True)
|
||||
conn.putheader(b"Host", NTFY_HOST.encode('utf-8'))
|
||||
conn.putheader(b"Host", NTFY_HOST.encode("utf-8"))
|
||||
for name, value in headers.items():
|
||||
conn.putheader(name.encode('utf-8'), value.encode('utf-8'))
|
||||
conn.putheader(name.encode("utf-8"), value.encode("utf-8"))
|
||||
conn.putheader("Content-Type", b"text/plain; charset=utf-8")
|
||||
conn.putheader("Content-Length", str(len(data)))
|
||||
conn.endheaders(data)
|
||||
resp = conn.getresponse()
|
||||
resp.read()
|
||||
if resp.status == 429:
|
||||
retry = resp.getheader('Retry-After', '5') or '5'
|
||||
retry = resp.getheader("Retry-After", "5") or "5"
|
||||
time.sleep(float(retry))
|
||||
continue
|
||||
if resp.status < 200 or resp.status >= 300:
|
||||
@ -64,18 +64,18 @@ def main():
|
||||
new_mail = json.loads(
|
||||
subprocess.check_output(
|
||||
["notmuch", "show", "--format=json", "tag:new"],
|
||||
).decode('utf-8')
|
||||
).decode("utf-8")
|
||||
)
|
||||
|
||||
for thread in new_mail:
|
||||
for message_pair in thread:
|
||||
headers = message_pair[0]['headers']
|
||||
subject = headers.get('Subject', '')
|
||||
sender = headers.get('From', '')
|
||||
date = headers.get('Date', '')
|
||||
body = extract_text(message_pair[0].get('body', []))[:MAX_BODY]
|
||||
headers = message_pair[0]["headers"]
|
||||
subject = headers.get("Subject", "")
|
||||
sender = headers.get("From", "")
|
||||
date = headers.get("Date", "")
|
||||
body = extract_text(message_pair[0].get("body", []))[:MAX_BODY]
|
||||
|
||||
payload = f"{sender}\n\n{body}\n\n{date}"
|
||||
payload = f"{sender}\n\n{body}\n{date}"
|
||||
send_ntfy(
|
||||
{
|
||||
"Title": subject,
|
||||
|
||||
Reference in New Issue
Block a user