def purgeThisMessageFromTheAttachments(aMessage):
	""" this function is recursive, if the message contains
	multipart message the function is recursively called to
	purge all the attachments"""

	partsToDelete = [] #empty list
	index = 0

	list = aMessage.get_payload()

	#end of recursion, the payload is a string
	if type(list) == type(""):
		return

	for part in list:
		maintype =  part.get_content_maintype()
		print maintype

		#I mark this part for delete, because it is not text
		if ( maintype != "text" and maintype != "multipart" and
			maintype != "message"):
			# Also the message type is a kind of multipart
			partsToDelete.append(index)

		if (maintype == "multipart" or maintype == "message"):
			#recursive call
			purgeThisMessageFromTheAttachments(part)
			
		index = index + 1

	#I can now delete the parts
	listParts = aMessage.get_payload()
	offset = 0

	#DEBUG prints
	#print "I should delete these parts"
	#print partsToDelete

	#print "The message has these parts"
	#print listParts

	for indexToDelete in partsToDelete:
		#print indexToDelete
		indexToDelete = indexToDelete - offset
		#let's save the part that we wish to delete.
		filename = listParts[indexToDelete].get_filename()
		if not filename:
			ext = mimetypes.guess_extension(part.get_type())
			if not ext:
				#generic extension
				ext = ".bin"
			filename = "part-%03d%s" % (indexToDelete, ext)

		fp = open (ATTACHMENTS_DIR + filename, "wb")
		fp.write(listParts[indexToDelete].get_payload(decode=1))
		fp.close()
		del listParts[indexToDelete]
		offset = offset + 1
		#print listParts
 this function is recursive, if the message contains
	multipart message the function is recursively called to
	purge all the attachments
recipes/Python/252173_Extract_Kill_mail_attachments/recipe-252173.py