Created
April 6, 2017 07:52
-
-
Save Neo-Oli/c56bb216a83e5a9ac7f6e453050b8fbc to your computer and use it in GitHub Desktop.
getmail postprocessor to move emails to specific folders according to ruls
This file contains 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
From:sony@email.sonyentertainmentnetwork.com=f:spam | |
From:rundschreiben@abload.de=f:spam | |
To:wettbewerb@oliverse.ch=f:spam | |
From:no-reply@jetpack.com=f:spam | |
From:no-reply@airdroidmail.com=f:spam | |
From:mtvmobile@newsletter.sunrise.ch=f:spam | |
From:news@quickline.com=f:spam | |
From:no-reply@mail.instagram.com=f:spam | |
From:pinbot@explore.pinterest.com=f:spam | |
From:news@siroop.ch=f:spam | |
From:notification|From:facebookmail=f:spam | |
From:newsletter@bernhard-fishing.ch=f:spam | |
From:refer@booking.com=f:spam | |
From:survey@sbb.ch=f:spam | |
To:swiss-chaos@chaostreff.ch=f:mailinglists | |
Cc:swiss-chaos@chaostreff.ch=f:mailinglists | |
From:sbb.newsletter@sbb.ch=f:spam | |
From:info@siroop.ch=f:spam | |
From:Coveralls <notifications@github.com>=f:spam | |
From:elob@elob.cz=f:spam |
This file contains 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
#!/data/data/com.termux/files/usr/bin/python3 | |
import sys | |
import email | |
import os | |
import time | |
import re | |
from email.header import decode_header | |
def savetofolder(message,foldername): | |
filename=str(time.time()) | |
filename="~/mail/.{}/new/{}".format(foldername,filename) | |
filename=os.path.expanduser(filename) | |
with open (filename,"w") as mailfile: | |
mailfile.write(message) | |
sys.exit(99) | |
def realdecode_header(string): | |
if not string: | |
string="" | |
d=decode_header(string) | |
header="" | |
for val,enc in d: | |
if not type(val) == str: | |
if not enc: | |
enc="utf-8" | |
val=val.decode(enc) | |
header="{}{}".format(header,val) | |
return header | |
message=[] | |
subject="" | |
sender="" | |
message=sys.stdin.read() | |
msg=email.message_from_string(message) | |
mailrulesfilepath=os.path.expanduser("~/.mailrules") | |
with open (mailrulesfilepath,"r") as mailrulesfile: | |
mailrules=mailrulesfile.read() | |
# print(mailrules) | |
mailrules=mailrules.split("\n") | |
# print(mailrules) | |
for block in mailrules: | |
block=block.strip() | |
if len(block)>0: | |
if block[0]!="#": | |
filters,action=block.split("=") | |
filters=filters.split("|") | |
match=True | |
for fil in filters: | |
attr,val=fil.split(":",1) | |
if msg[attr]: | |
if val not in realdecode_header(msg[attr]): | |
match=False | |
else: | |
match=False | |
if match: | |
print("Message maches rule: {}".format(block),file=sys.stderr) | |
func,arg=action.split(":",1) | |
if func=="f": | |
savetofolder(message,arg) | |
print(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment