Saturday, September 1, 2012

Postfix bounces email handler by python script

#!/usr/bin/python
# -*- coding: utf-8 -*-
# export PYTHONIOENCODING=utf-8
import MySQLdb
import string
import email
from cStringIO import StringIO
from email import message_from_string
from flufl.bounce import scan_message
import re, sys, os

class DB:
conn = None

def connect(self):
self.conn = MySQLdb.connect(host='10.0.0.1', user='xx_rw', passwd='123456', db='xxx', use_unicode=True, charset="utf8")

def query(self, sql, string):
try:
cursor = self.conn.cursor()
cursor.execute(sql,string)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql,string)
return cursor


def main():
db = DB()
sender=sys.argv[2]
recipient=sys.argv[1]
email_input = sys.stdin.readlines()
parser = email.FeedParser.FeedParser()
msg = None
for msg_line in email_input:
msg = parser.feed(msg_line)
msg = parser.close()

#filter_recipient = scan_message(message_from_string(string.join(message, ' ')))
filter_recipient = scan_message(msg)
if (len(filter_recipient)>0):
db.query("""INSERT INTO email_bounces (`sender`, `recipient`) VALUES(%s, %s)""", (filter_recipient, recipient))

if __name__ == "__main__":
main()

4 comments:

  1. Hi, I found your article by mistake when i was searching bing for this issue, I have to say your article is really helpful I also love the theme, its amazing!. I dont have that much time to read all your post at the moment but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thank you for a huge site.

    ReplyDelete
  2. By using your guide I have tried and it was very easy to install and configure postfix.

    ReplyDelete
  3. hello Nice site wayfair coupon code http://www.prweb.com/releases/Wayfair/Coupon/prweb10751023.htm

    ReplyDelete
  4. czacdgsfe/psbdmf2/dpn, Buy viagra uk, CiETFOo, [url=http://www.poitin-still.com/]Buy viagra sydney[/url], ZlZjmBQ, http://www.poitin-still.com/ Buy viagra online at, asGUMqH.

    ReplyDelete