Skip to content

Instantly share code, notes, and snippets.

@zmej-serow
Created March 27, 2020 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmej-serow/4d6292591c690563e7dd205964f8b8ee to your computer and use it in GitHub Desktop.
Save zmej-serow/4d6292591c690563e7dd205964f8b8ee to your computer and use it in GitHub Desktop.
IMAP multiple FROM query builder
def build_multiple_from_query(emails: List[str]) -> str:
# IMAP query composition: (OR (OR (FROM a@a.ru) (FROM b@b.ru)) (FROM c@c.ru))
def gather(a: str, b: str) -> str:
if 'FROM' not in a:
return f'OR (FROM "{a}") (FROM "{b}")'
else:
return f'OR ({a}) (FROM "{b}")'
return f'({reduce(gather, emails)})' if len(emails) > 1 else f'FROM {emails[0]}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment