Created
February 14, 2012 21:41
-
-
Save farhaven/1830729 to your computer and use it in GitHub Desktop.
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
#!/usr/local/bin/python2.7 | |
import getopt | |
import os | |
import os.path | |
import smtplib | |
import sys | |
import tempfile | |
import socket | |
import time | |
import subprocess | |
import email.parser | |
import pynotify | |
check_interval = 60 | |
smtp_host = "localhost:2525" | |
# account data {{{ | |
smtp_user = "bar" | |
smtp_pass = "foo" | |
# }}} | |
droppath = os.environ["HOME"] + "/.md" | |
def htmlsafe(x): | |
if x is None: | |
return "" | |
return x.replace("&", "&").replace("<", "<").replace(">", ">") | |
def drop(): | |
(fd, fname) = tempfile.mkstemp(dir=droppath) | |
f = os.fdopen(fd, "w") | |
mail_to = None | |
mail_subject = None | |
for line in sys.stdin: | |
f.write(str(line)) | |
if line.startswith("To:") and mail_to is None: | |
mail_to = line.split(None, 1)[1].strip() | |
if line.startswith("Subject:") and mail_subject is None: | |
mail_subject = line.split(None, 1)[1].strip() | |
f.close() | |
os.rename(fname, fname + ".done") | |
release(True) | |
sys.exit(0) | |
def send_file(f): | |
print("sending file " + f) | |
fd = open(f, 'r') | |
mail_msg = "" | |
for line in fd: | |
mail_msg = mail_msg + line | |
h = email.parser.HeaderParser().parsestr(mail_msg) | |
mail_to = [] | |
try: | |
mail_to.extend(h['To'].split(", ")) | |
except: | |
pass | |
try: | |
mail_to.extend(h['CC'].split(", ")) | |
except: | |
pass | |
try: | |
mail_to.extend(h['BCC'].split(", ")) | |
except: | |
pass | |
mail_from = h['From'] | |
mail_subject = h['Subject'] | |
if len(mail_to) == 0: | |
raise Exception(f + ": No mail recipient") | |
if mail_from is None: | |
raise Exception(f + ": No mail sender") | |
s = smtplib.SMTP(smtp_host) | |
s.starttls() | |
s.ehlo() | |
s.login(smtp_user, smtp_pass) | |
s.sendmail(mail_from, mail_to, mail_msg) | |
os.unlink(f) | |
# pynotify.Notification("Mail sent", "To: <b>" + htmlsafe(str(mail_to)) + "</b>\nSubject: <b>" + htmlsafe(mail_subject) + "</b>").show() | |
print("done sending " + f) | |
def release(once=False): | |
while True: | |
files = os.listdir(droppath) | |
for f in files: | |
if not f.endswith(".done"): | |
continue | |
f = os.path.join(droppath, f) | |
try: | |
send_file(f) | |
except Exception as err: | |
print(str(err)) | |
print("file sent") | |
if not once: | |
time.sleep(600) | |
else: | |
return | |
def usage(): | |
print("Usage:") | |
print(sys.argv[0] + " [-h|-d|-r|-R]") | |
print("-h Show help") | |
print("-d Drop mail") | |
print("-r Release mail in a loop") | |
print("-R try releasing mail once") | |
sys.exit(-1) | |
if __name__ == "__main__": | |
pynotify.init("maildrop") | |
try: | |
(opt, argv) = getopt.getopt(sys.argv[1:], "hdrR") | |
except Exception as err: | |
print(str(err)) | |
usage() | |
if not os.path.isdir(droppath): | |
os.makedirs(droppath, 0o700) | |
for (k, v) in opt: | |
if k == '-d': | |
drop() | |
elif k == "-r": | |
release() | |
elif k == "-R": | |
release(True) | |
sys.exit(0) | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment