3955bd0bd31132c8ac6d439a2d0fffbc6d12b79e
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

1) #!/usr/bin/python
Benjamin Renard MantisMail : Added decode()...

Benjamin Renard authored 12 years ago

2) # -*- coding: utf-8 -*-
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

3) 
4) import re
5) import string
6) import smtplib
7) import logging
Benjamin Renard MantisMail.get_text() : do...

Benjamin Renard authored 12 years ago

8) import html2text
Benjamin Renard MantisMail : Added decode()...

Benjamin Renard authored 12 years ago

9) from email.MIMEText import MIMEText
10) import email
11) import email.header
Benjamin Renard MantisMail : fix mimetypes...

Benjamin Renard authored 12 years ago

12) import mimetypes
Benjamin Renard MantisMail : fix sys import...

Benjamin Renard authored 12 years ago

13) import sys
Benjamin Renard Mail & WS : Decode base64 a...

Benjamin Renard authored 12 years ago

14) import base64
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

15) 
16) class MantisMail(object):
17) 
18) 	def __init__(self,mailfrom,mailto,data):
19) 		self.mailfrom = mailfrom
20) 		self.mailto = mailto
21) 		self.msg = email.message_from_string(data)
22) 
23) 	def get_issue_id_from_subject(self):
24) 		res=re.search('\[[^\]]* ([0-9]*)\]',self.subject())
25) 		if res:
26) 			issue_id=res.group(1)
27) 			return string.atoi(issue_id)
28) 		return None
29) 
30) 	def subject(self):
Benjamin Renard MantisMail.subject() : deco...

Benjamin Renard authored 12 years ago

31) 		return self.decode_header('Subject')
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

32) 	
33) 	def get_text(self):
Benjamin Renard MantisMail.get_text() : do...

Benjamin Renard authored 12 years ago

34) 		content=u'From : %s' % self.decode_header('From')
35) 		texts={}
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

36) 		for part in self.msg.walk():
37) 			if part.get_content_maintype() == 'text':
Benjamin Renard MantisMail.get_text() : do...

Benjamin Renard authored 12 years ago

38) 				type=part.get_content_type()
39) 				if type not in texts:
40) 					texts[type]=[]
41) 				text=self.decode(part.get_payload(decode=True), prefer_encoding=part.get_content_charset())
42) 				if part.get_content_type() == 'text/html':
43) 					text=html2text.html2text(text)
44) 				texts[type].append(unicode(text))
45) 
46) 		if 'text/plain' in texts:
47) 			for text in texts['text/plain']:
48) 				content=u'%s\n\n---------------------------\n\n%s' % (content,text)
49) 		else:
50) 			for type in texts:
51) 				for text in texts[type]:
52) 					content=u'%s\n\n---------------------------\n\n%s' % (content,text)
53) 		return unicode(content)
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

54) 	
55) 	def get_attachments(self):
56) 		attachments=[]
57) 		counter=1
58) 		for part in self.msg.walk():
Benjamin Renard Mail & WS : Decode base64 a...

Benjamin Renard authored 12 years ago

59) 			part_maintype = part.get_content_maintype()
60) 			if part_maintype == 'text' or part_maintype == 'multipart':
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

61) 				continue
Benjamin Renard Mail & WS : Decode base64 a...

Benjamin Renard authored 12 years ago

62) 			part_type = part.get_content_type()
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

63) 			filename = part.get_filename()
64) 			if not filename:
65) 				ext = mimetypes.guess_extension(part.get_content_type())
66) 				if not ext:
67) 					ext = '.bin'
68) 				filename = 'part-%03d%s' % (counter, ext)
69) 			counter += 1
70) 			content=part.get_payload(decode=False)
Benjamin Renard Mail & WS : Decode base64 a...

Benjamin Renard authored 12 years ago

71) 			try:
72) 				content=base64.b64decode(content)
73) 			except Exception, e:
74) 				logging.error("Error decoding base64 content of file %s" % filename)
75) 				raise MantisMailError('base64_decode',"Error decoding base64 content of file %s" % filename)
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

76) 			attachments.append({
77) 				'filename':	filename,
78) 				'type':		part_type,
79) 				'content':	content
80) 			})
81) 		return attachments
82) 
Benjamin Renard MantisMail : Added decode()...

Benjamin Renard authored 12 years ago

83) 	def decode(self,text, prefer_encoding=None):
84) 		try_enc=['iso-8859-15', 'utf-8']
85) 		if prefer_encoding:
86) 			try_enc.insert(0,prefer_encoding)
87) 		for i in try_enc:
88) 			try:
89) 				return unicode(text.decode(i))
90) 			except Exception, e:
91) 				continue
92) 		return unicode(text.decode('utf-8', errors = 'replace'))
93) 
94) 	def decode_header(self,header):
95) 		s=email.Header.decode_header(self.msg[header])[0]
96) 		return self.decode(s[0],prefer_encoding=s[1])
97) 
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

98) def send(mail_from,to,subject,content,smtp_host='127.0.0.1',smtp_port=25):
99) 	msg = MIMEText(content)
100) 	msg['To'] = to
101) 	msg['From'] = mail_from
102) 	msg['Subject'] = subject
103) 
104) 	server = smtplib.SMTP(smtp_host,smtp_port)
105) 
106) 	try:
107) 		server.sendmail(mail_from, [to], msg.as_string())
108) 	except:
109) 		logging.error('Error sending mail to %s - %s : %s' % (to,subject,sys.exc_info()[0]))
110)                 return
111)