root/trunk/twisted/mail/bounce.py

Revision 11450, 1.5 KB (checked in by radix, 6 years ago)

MIT LICENSE: new LICENSE file and new preambles for all .py files. This is not all.

Line 
1# -*- test-case-name: twisted.mail.test.test_bounce -*-
2#
3# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
4# See LICENSE for details.
5
6
7import StringIO
8import rfc822
9import string
10import time
11import os
12
13
14from twisted.mail import smtp
15
16BOUNCE_FORMAT = """\
17From: postmaster@%(failedDomain)s
18To: %(failedFrom)s
19Subject: Returned Mail: see transcript for details
20Message-ID: %(messageID)s
21Content-Type: multipart/report; report-type=delivery-status;
22    boundary="%(boundary)s"
23
24--%(boundary)s
25
26%(transcript)s
27
28--%(boundary)s
29Content-Type: message/delivery-status
30Arrival-Date: %(ctime)s
31Final-Recipient: RFC822; %(failedTo)s
32"""
33
34def generateBounce(message, failedFrom, failedTo, transcript=''):
35    if not transcript:
36        transcript = '''\
37I'm sorry, the following address has permanent errors: %(failedTo)s.
38I'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()
Note: See TracBrowser for help on using the browser.