Last active
April 25, 2024 03:16
-
-
Save mizlan/e22da032d8f47369bb6fc4d0508d2063 to your computer and use it in GitHub Desktop.
Unsubscribe from a mailing list that implements the one-click List-Unsubscribe RFC 8058, in Python with no external libraries. Takes raw email text as stdin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
automatically trigger a one-click unsubscribe | |
in an email, according to RFC 8058 | |
''' | |
from email.header import make_header, decode_header | |
from email.parser import HeaderParser | |
import sys | |
import re | |
import urllib.request | |
text = sys.stdin.read() | |
post = 'List-Unsubscribe=One-Click' | |
z = HeaderParser().parsestr(text) | |
if z["List-Unsubscribe-Post"] != post: | |
print("Does not support (RFC 8058)", file=sys.stderr) | |
sys.exit(1) | |
data = str(make_header(decode_header(z["List-Unsubscribe"]))) | |
url = re.search("<(https://.+?)>", data).group(1) | |
print(url, file=sys.stderr) | |
with urllib.request.urlopen(url, data=post.encode('ascii')) as f: | |
print(f.read().decode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment