Last active
March 14, 2016 16:05
-
-
Save speters/5386c0548faa65fe725b to your computer and use it in GitHub Desktop.
A simple utility to forward mailcatch.com temporary mailboxes via SMTP
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
#! /usr/bin/env python | |
import urllib2 | |
from xml.dom import minidom, Node | |
from sys import argv | |
import smtplib | |
import ConfigParser, os | |
config = ConfigParser.SafeConfigParser({'smtphost': 'localhost', 'smtpuser':'', 'smtppass':'', 'forwardto':''}) | |
config.read(['/etc/smtpclient.ini', os.path.expanduser('~/.smtpclient.ini')]) | |
if config.has_section('mailcatch'): | |
section = 'mailcatch' | |
else: | |
try: | |
section=config.sections()[0] | |
except IndexError: | |
section = 'DEFAULT' | |
smtphost = config.get(section, 'smtphost') | |
smtpuser = config.get(section, 'smtpuser') | |
smtppass = config.get(section, 'smtppass') | |
try: | |
""" simple test if first arg is an email address for forwarding""" | |
if '@' in argv[1] and '.' in argv[1]: | |
forwardto = argv.pop(1) | |
else: | |
forwardto = config.get(section, 'forwardto') | |
except IndexError: | |
forwardto = config.get(section, 'forwardto') | |
if (len(argv) < 2 or forwardto == ''): | |
print "mailcatch.py [[email protected]] mailcatchmboxname1 [mboxname2 ...]" | |
print "" | |
print "A simple utility to forward mailcatch.com temporary mailboxes via SMTP." | |
print "Downloaded mails get deleted from mailcatch.com." | |
print "See sources for info about optional smtpclient.ini config file" | |
exit(0) | |
mboxurls = [] | |
for arg in argv[1:]: | |
mboxname = arg | |
""" Get the XML """ | |
url_info = urllib2.urlopen('http://mailcatch.com/en/temporary-inbox-rss?box='+mboxname) | |
#url_info = urllib2.urlopen('file:mailcatch_test.rss') | |
mboxurls = [] | |
if (url_info): | |
""" We have the RSS XML lets try to parse it up """ | |
xmldoc = minidom.parse(url_info) | |
if (xmldoc): | |
"""We have the Doc, get the root node""" | |
rootNode = xmldoc.documentElement | |
""" Iterate the <channel> child nodes """ | |
for channel in rootNode.childNodes: | |
for node in channel.childNodes: | |
""" We only care about "item" entries""" | |
if (node.nodeName == "item"): | |
""" Now iterate through all of the <item>'s children """ | |
for item_node in node.childNodes: | |
if (item_node.nodeName == "link"): | |
""" Loop through the link Text nodes to get | |
the actual link""" | |
link = "" | |
for text_node in item_node.childNodes: | |
if (text_node.nodeType == node.TEXT_NODE): | |
link += text_node.nodeValue | |
""" Now print the title if we have one """ | |
if (len(link)>0): | |
mboxurls.append(link) | |
# remove dupes | |
mboxurls = set(mboxurls) | |
for mboxurl in set(mboxurls): | |
mboxurlsrc = mboxurl.replace('temporary-inbox', 'temporary-mail-content').replace('show=', 'show_source=') | |
message = urllib2.urlopen(mboxurlsrc) | |
if (message): | |
msg = message.read() | |
server = smtplib.SMTP(smtphost) | |
server.ehlo() | |
server.starttls() | |
server.ehlo() | |
if (smtpuser!='' and smtppass != ''): | |
server.login(smtpuser, smtppass) | |
server.sendmail('mailcatch-forwarder@localhost', [forwardto.replace('REPLACETHIS', mboxname)], msg) | |
server.quit() | |
# finally delete message on mailcatch.com | |
urllib2.urlopen(mboxurl.replace('show=', 'del=')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment