2 #I had to rewrite this class because the python MIME email.mime (version 2.5)
3 #are buggy, they use \n instead \r\n for new line which is not compliant
5 # http://bugs.python.org/issue5525
7 #TODO do not load all the message in memory stream it from the disk
17 _width = len(repr(sys.maxint-1))
18 _fmt = '%%0%dd' % _width
28 def makeBoundary(self):
31 msgparts.append(self._xmlMessage)
33 msgparts.append(i.read())
34 #this sucks, all in memory
35 alltext = NL.join(msgparts)
36 self._boundary = _make_boundary(alltext)
37 #maybe I can save some memory
40 self._startCID = "<" + (_fmt % random.randrange(sys.maxint)) + (_fmt % random.randrange(sys.maxint)) + ">"
44 '''it return a string with the MIME message'''
45 if len(self._boundary) == 0:
46 #the makeBoundary hasn't been called yet
48 #ok we have everything let's start to spit the message out
50 returnstr = NL + "--" + self._boundary + NL
51 returnstr += "Content-Type: text/xml; charset=\"us-ascii\"" + NL
52 returnstr += "Content-Transfer-Encoding: 7bit" + NL
53 returnstr += "Content-Id: " + self._startCID + NL + NL
54 returnstr += self._xmlMessage + NL
56 for file in self._files:
57 returnstr += "--" + self._boundary + NL
58 returnstr += "Content-Type: application/octet-stream" + NL
59 returnstr += "Content-Transfer-Encoding: binary" + NL
60 returnstr += "Content-Id: <" + str(id(file)) + ">" + NL + NL
62 returnstr += file.read() + NL
64 returnstr += "--" + self._boundary + "--" + NL
67 def attachFile(self, file):
69 it adds a file to this attachment
71 self._files.append(file)
73 def addXMLMessage(self, xmlMessage):
75 it adds the XML message. we can have only one XML SOAP message
77 self._xmlMessage = xmlMessage
79 def getBoundary(self):
81 this function returns the string used in the mime message as a
82 boundary. First the write method as to be called
86 def getStartCID(self):
88 This function returns the CID of the XML message
93 def _make_boundary(text=None):
94 #some code taken from python stdlib
95 # Craft a random boundary. If text is given, ensure that the chosen
96 # boundary doesn't appear in the text.
97 token = random.randrange(sys.maxint)
98 boundary = ('=' * 10) + (_fmt % token) + '=='
104 cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
105 if not cre.search(text):
107 b = boundary + '.' + str(counter)