Last active
March 20, 2023 12:46
-
-
Save abelevtsov/3991f53590693878a4f72d896341f0a0 to your computer and use it in GitHub Desktop.
port25 PowerMTA bounce accounting with send to MongoDB
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
[DEFAULT] | |
[headers] | |
headers = type,timeLogged,timeQueued,orig,rcpt,orcpt,dsnAction,dsnStatus,dsnDiag,dsnMta,bounceCat,srcType,srcMta,dlvType,dlvSourceIp,dlvDestinationIp,dlvEsmtpAvailable,dlvSize,vmta,jobId,envId,queue,vmtaPool | |
[mongo] | |
host = 127.0.0.1 | |
port = 27017 | |
queueName = pmta-events |
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
import sys | |
import datetime | |
import configparser | |
import pymongo | |
import csv | |
import io | |
config = configparser.ConfigParser() | |
config.read("./bounce.conf") | |
# read accounting headers | |
headers = config.get("headers", "headers") | |
keys = ("_t," + headers).split(",") | |
# read mongo config | |
mongoSection = config["mongo"] | |
host = mongoSection.get("host") | |
port = mongoSection.getint("port") | |
queueName = mongoSection.get("queueName") | |
# connect to mongo | |
try: | |
c = pymongo.MongoClient(host, port) | |
except ConnectionFailure, e: | |
sys.stderr.write("Could not connect to MongoDB: %s" % e) | |
sys.exit(1) | |
db = c.Scheduler | |
queue = db.Queue | |
# read first line - unused | |
line = sys.stdin.readline() | |
while line: | |
line = sys.stdin.readline() | |
if line: | |
# write accounting data to queue | |
values = list(csv.reader(io.StringIO('EmailStatusReceived,' + line)))[0] | |
message = dict(zip(keys, values)) | |
queue.insert({ | |
"QueueName": queueName, | |
"Message": message, | |
"NextVisibleTime": datetime.datetime.utcnow(), | |
"DequeueCount": 0 | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment