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