|
Revision 11450, 1.5 kB
(checked in by radix, 5 years ago)
|
MIT LICENSE: new LICENSE file and new preambles for all .py files. This is not all.
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
import StringIO |
|---|
| 8 |
import rfc822 |
|---|
| 9 |
import string |
|---|
| 10 |
import time |
|---|
| 11 |
import os |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
from twisted.mail import smtp |
|---|
| 15 |
|
|---|
| 16 |
BOUNCE_FORMAT = """\ |
|---|
| 17 |
From: postmaster@%(failedDomain)s |
|---|
| 18 |
To: %(failedFrom)s |
|---|
| 19 |
Subject: Returned Mail: see transcript for details |
|---|
| 20 |
Message-ID: %(messageID)s |
|---|
| 21 |
Content-Type: multipart/report; report-type=delivery-status; |
|---|
| 22 |
boundary="%(boundary)s" |
|---|
| 23 |
|
|---|
| 24 |
--%(boundary)s |
|---|
| 25 |
|
|---|
| 26 |
%(transcript)s |
|---|
| 27 |
|
|---|
| 28 |
--%(boundary)s |
|---|
| 29 |
Content-Type: message/delivery-status |
|---|
| 30 |
Arrival-Date: %(ctime)s |
|---|
| 31 |
Final-Recipient: RFC822; %(failedTo)s |
|---|
| 32 |
""" |
|---|
| 33 |
|
|---|
| 34 |
def generateBounce(message, failedFrom, failedTo, transcript=''): |
|---|
| 35 |
if not transcript: |
|---|
| 36 |
transcript = '''\ |
|---|
| 37 |
I'm sorry, the following address has permanent errors: %(failedTo)s. |
|---|
| 38 |
I've given up, and I will not retry the message again. |
|---|
| 39 |
''' % vars() |
|---|
| 40 |
|
|---|
| 41 |
boundary = "%s_%s_%s" % (time.time(), os.getpid(), 'XXXXX') |
|---|
| 42 |
failedAddress = rfc822.AddressList(failedTo)[0][1] |
|---|
| 43 |
failedDomain = string.split(failedAddress, '@', 1)[1] |
|---|
| 44 |
messageID = smtp.messageid(uniq='bounce') |
|---|
| 45 |
ctime = time.ctime(time.time()) |
|---|
| 46 |
|
|---|
| 47 |
fp = StringIO.StringIO() |
|---|
| 48 |
fp.write(BOUNCE_FORMAT % vars()) |
|---|
| 49 |
orig = message.tell() |
|---|
| 50 |
message.seek(2, 0) |
|---|
| 51 |
sz = message.tell() |
|---|
| 52 |
message.seek(0, orig) |
|---|
| 53 |
if sz > 10000: |
|---|
| 54 |
while 1: |
|---|
| 55 |
line = message.readline() |
|---|
| 56 |
if len(line)<=1: |
|---|
| 57 |
break |
|---|
| 58 |
fp.write(line) |
|---|
| 59 |
else: |
|---|
| 60 |
fp.write(message.read()) |
|---|
| 61 |
return '', failedFrom, fp.getvalue() |
|---|