+

Mail & WS : Decode base64 attachments from mail and encode in base64 again for WS

Benjamin Renard [2012-04-20 14:46:09]
Mail & WS : Decode base64 attachments from mail and encode in base64 again for WS
Filename
src/usr/share/mantis-smtp/MantisMail.py
src/usr/share/mantis-smtp/MantisWS.py
diff --git a/src/usr/share/mantis-smtp/MantisMail.py b/src/usr/share/mantis-smtp/MantisMail.py
index 95048c6..e66bc48 100755
--- a/src/usr/share/mantis-smtp/MantisMail.py
+++ b/src/usr/share/mantis-smtp/MantisMail.py
@@ -11,6 +11,7 @@ import email
 import email.header
 import mimetypes
 import sys
+import base64

 class MantisMail(object):

@@ -55,9 +56,10 @@ class MantisMail(object):
 		attachments=[]
 		counter=1
 		for part in self.msg.walk():
-			part_type = part.get_content_maintype()
-			if part_type == 'text' or part_type == 'multipart':
+			part_maintype = part.get_content_maintype()
+			if part_maintype == 'text' or part_maintype == 'multipart':
 				continue
+			part_type = part.get_content_type()
 			filename = part.get_filename()
 			if not filename:
 				ext = mimetypes.guess_extension(part.get_content_type())
@@ -66,6 +68,11 @@ class MantisMail(object):
 				filename = 'part-%03d%s' % (counter, ext)
 			counter += 1
 			content=part.get_payload(decode=False)
+			try:
+				content=base64.b64decode(content)
+			except Exception, e:
+				logging.error("Error decoding base64 content of file %s" % filename)
+				raise MantisMailError('base64_decode',"Error decoding base64 content of file %s" % filename)
 			attachments.append({
 				'filename':	filename,
 				'type':		part_type,
@@ -102,3 +109,8 @@ def send(mail_from,to,subject,content,smtp_host='127.0.0.1',smtp_port=25):
 		logging.error('Error sending mail to %s - %s : %s' % (to,subject,sys.exc_info()[0]))
                 return

+class MantisMailError(Exception):
+
+        def __init__(self,type,msg):
+                Exception.__init__(self, msg)
+                self.type = type
diff --git a/src/usr/share/mantis-smtp/MantisWS.py b/src/usr/share/mantis-smtp/MantisWS.py
index d6765f2..29dbb3e 100755
--- a/src/usr/share/mantis-smtp/MantisWS.py
+++ b/src/usr/share/mantis-smtp/MantisWS.py
@@ -4,6 +4,7 @@ import suds
 import logging
 import re
 import MantisMail
+import base64

 class MantisWS(object):

@@ -60,7 +61,7 @@ class MantisWS(object):
 				issue_id,
 				attachment['filename'],
 				attachment['type'],
-				attachment['content']
+				base64.b64encode(attachment['content'])
 			)
 			return res
 		except suds.WebFault, e:
ViewGit