PKtxK1bbmg2dsn/__init__.py""" Generate rfc6522 delivery status notifications from the Mailgun API. """ __version__ = '0.2' PKqxKC$!!mg2dsn/_impl.py#!/usr/bin/env python3.6 """ Script to generate rfc6522 DSN messages from mailgun. """ import sys import os from io import BytesIO from textwrap import dedent from secretly import secretly import argparse import datetime import dateutil.parser import itertools import json import pytz from email.utils import make_msgid from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email import message_from_string from email.message import Message from email.mime.nonmultipart import MIMEBase from email.utils import formatdate import treq from twisted.internet.defer import inlineCallbacks, succeed from random import SystemRandom choice = SystemRandom().choice class UnexpectedResponse(Exception): """ The response wasn't expected. """ @inlineCallbacks def getAllEvents(domain, secret): eventsURL = "https://api.mailgun.net/v3/{domain}/events".format( domain=domain ) pageURL = eventsURL while True: thisPage = (yield treq.json_content( (yield treq.get(pageURL, auth=("api", secret), params={ "event": "failed", "severity": "permanent", })) )) if not thisPage['items']: break for item in thisPage['items']: if ((item['flags'].get('is-authenticated') or item['flags'].get('is-delayed-bounce') # delayed bounces are (obviously) not authenticated. has # mailgun already validated this bounce? if not, we might # need to check to make sure the message ID originated from a # delayed bounce. ) and item['reason'] in ('bounce', 'suppress-bounce')): bounced_at = pytz.utc.localize( datetime.datetime.utcfromtimestamp(item['timestamp']) ) print("found a bounce for", item['recipient'], 'on', bounced_at) bounce_uri = ( "https://api.mailgun.net/v3/{domain}/bounces/{recipient}" .format(domain=domain, recipient=item['recipient'].lower()) ) bounced = yield treq.get(bounce_uri, auth=("api", secret)) if bounced.code == 200: bouncedata = yield treq.json_content(bounced) suppression_created = dateutil.parser.parse( bouncedata.get('created_at') ) if item['flags'].get('is-delayed-bounce'): original = (yield treq.json_content( (yield treq.get( eventsURL, auth=("api", secret), params={ "message-id": item['message']['headers']['message-id'], })) ))['items'][-1] item['storage'] = original['storage'] item['envelope'] = original['envelope'] yield writeone(secret, item, domain) delta = abs( (bounced_at - suppression_created).total_seconds() ) if item['reason'] == 'bounce': print(" clearing (original) bounce created", suppression_created, "with delta", delta) yield treq.delete(bounce_uri, auth=("api", secret)) else: print(" not clearing (suppression) bounce; delta:", delta, 'reason:', item['reason']) else: # drain the response treq.content(bounced) print(" no suppression; not sending bounce.") pageURL = thisPage['paging']['next'] @inlineCallbacks def writeone(secret, blob, domain, counter=itertools.count()): msg = MIMEMultipart("report", **{"report-type": "delivery-status"}) to_address = blob['envelope']['sender'] msg['to'] = to_address msg['from'] = '"Bounce Generator" '.format( domain=domain ) msg['subject'] = 'Delivery Status Notification: Deferred Bounce' msg['message-id'] = make_msgid("mg2dsn", domain=domain) originalMessageID = blob['message']['headers']['message-id'] msg['in-reply-to'] = originalMessageID msg['references'] = originalMessageID jsonified = json.dumps(blob, indent=2) statthing = MIMEBase("message", "delivery-status") storurl = blob['storage']['url'] apiresponse = yield treq.get(storurl, auth=("api", secret), headers={"accept": ["message/rfc2822"]}) msgobj = yield treq.json_content(apiresponse) if apiresponse.code == 200: original_bytes = msgobj['body-mime'] else: original_bytes = dedent( """ From: no-user@no-host.invalid Subject: original message deleted Content-Type: text/plain Sorry, the mail administrator didn't run the script soon enough and the original message got garbage-collected in the meanwhile. """ ).strip() original = message_from_string(original_bytes) inner1 = Message() inner1['Reporting-MTA'] = 'dns;{domain}'.format(domain=domain) inner1['Arrival-Date'] = formatdate(localtime=True) inner1['Original-Envelope-Id'] = originalMessageID inner2 = Message() inner2['Original-Recipient'] = 'rfc822;' + blob['envelope']['targets'] inner2['Final-Recipient'] = 'rfc822;' + blob['envelope']['targets'] inner2['Action'] = 'failed' inner2['Status'] = '5.1.0 (Remote SMTP server has rejected address)' inner2['Remote-MTA'] = 'dns;' + blob['delivery-status'].get( 'mx-host', 'no-host.invalid' ) inner2['Diagnostic-Code'] = 'smtp;' + str(blob['delivery-status']['code']) statthing.attach(inner1) statthing.attach(inner2) msg.attach(MIMEText( dedent(""" This is the mail system at {domain}. We are sorry to inform you that the following message, which we believe you sent, could not be delivered. --- From: {sender} To: {recipient} Subject: {subject} --- """.format( domain=domain, sender=to_address, recipient=blob['envelope']['targets'], subject=blob['message']['headers'].get('subject', '(no subject)'), )) )) msg.attach(statthing) msg.attach(original) msg.attach(MIMEText( "\n---\n\noriginal mailgun delivery status failure follows:\n\n{}\n\n" .format(jsonified)) ) response = yield treq.post( "https://api.mailgun.net/v3/{domain}/messages.mime".format( domain=domain ), auth=("api", secret), params={ "to": to_address, }, files={ "message": ("message", BytesIO(msg.as_bytes())), } ) print("bounce sent:", response.code) print((yield treq.json_content(response))) def main(reactor, argv): from twisted.python.filepath import FilePath cfg = FilePath(os.path.expanduser("~/.config/mg2dsn")) cfg.makedirs(True) defaultsPath = cfg.child("defaults.json") if defaultsPath.exists(): defaults = json.loads(defaultsPath.getContent().decode('utf-8')) else: defaults = {} unspecified = object() parser = argparse.ArgumentParser() parser.add_argument("domain", nargs="?", default=defaults.get("domain", unspecified)) namespace = parser.parse_args(argv[1:]) if namespace.domain is unspecified: print("Please specify a domain.") return succeed(1) defaultsPath.setContent(json.dumps(dict(defaults, domain=namespace.domain), indent=2).encode()) def action(secret): return getAllEvents(namespace.domain, secret) return secretly(reactor, action=action, system='api.mailgun.net', username=namespace.domain) def script(): from twisted.internet.task import react react(main, [sys.argv]) PK!HV'.%mg2dsn-0.2.dist-info/entry_points.txtN+I/N.,()M7J)γPz9VI..PK%K$/00mg2dsn-0.2.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2017 Glyph Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H0RRmg2dsn-0.2.dist-info/WHEEL1 W% 0n1JBZS`ޝ/Qf:3&.~l" kW*дY00?PK!H> }*mg2dsn-0.2.dist-info/METADATAmQN0+@*8XQmZQaINu!|=N[@nޝyF52伲FqV$%jeQ{|#GYɐC&p(Vm9x0JUQanfZ ,e4k;/\*nkVY˶|b*2>rT&u1O)x^Vyo3٩brGtGzt~6J.[>FxlB|9q9zg/dt2hdubx~|b"SpώvfCS)j?OPK!H 0Xmg2dsn-0.2.dist-info/RECORDuv0}%2."@d(T7 𾾛zN(kTdu[XHeMߥow! (FZ%m0fQ6QޚzXf'c3;{7:(U  MHH~ B'XKzfӰ$bӸ4 ب΄j݂}YΆ#klo6 +vޮ:Y#:?X&[ 5x\s2==Zxr+>o]"j)P`Z,kC?pCޱjY싸Qu ]kkbH>y>E@H}S PKtxK1bbmg2dsn/__init__.pyPKqxKC$!!mg2dsn/_impl.pyPK!HV'.%r"mg2dsn-0.2.dist-info/entry_points.txtPK%K$/00"mg2dsn-0.2.dist-info/LICENSEPK!H0RRF'mg2dsn-0.2.dist-info/WHEELPK!H> }*'mg2dsn-0.2.dist-info/METADATAPK!H 0X5)mg2dsn-0.2.dist-info/RECORDPK*