Skip to content

Instantly share code, notes, and snippets.

@abelevtsov
Last active March 20, 2023 12:46
Show Gist options
  • Save abelevtsov/3991f53590693878a4f72d896341f0a0 to your computer and use it in GitHub Desktop.
Save abelevtsov/3991f53590693878a4f72d896341f0a0 to your computer and use it in GitHub Desktop.
port25 PowerMTA bounce accounting with send to MongoDB
[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
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