Skip to content

Instantly share code, notes, and snippets.

@mizlan
Last active April 25, 2024 03:16
Show Gist options
  • Save mizlan/e22da032d8f47369bb6fc4d0508d2063 to your computer and use it in GitHub Desktop.
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.
'''
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