1 import email
2 from email import Encoders
3 from email.MIMEAudio import MIMEAudio
4 from email.MIMEBase import MIMEBase
5 from email.MIMEMultipart import MIMEMultipart
6 from email.MIMEImage import MIMEImage
7 from email.MIMEText import MIMEText
8
9 import logging
10 log = logging.getLogger("dataproviders.Email")
11
12 import conduit
13 from conduit.datatypes import DataType, File
14
15 -class Email(DataType.DataType):
16 """
17 Basic email representation.
18 Based on: http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/python/lib/node597.html
19 """
20 _name_ = "email"
22 DataType.DataType.__init__(self)
23 self.attachments = []
24
25 self.email = MIMEText(kwargs.get("content", ""))
26 self.email['Subject'] = kwargs.get("subject", "")
27 self.email['To'] = kwargs.get("to", "")
28 self.email['From'] = kwargs.get("from", "")
29 self.email.preamble = ''
30 self.email.epilogue = ''
31
33 if len(self.attachments) > 0:
34 return True
35 return False
36
38
39 if not self.email.is_multipart():
40 newemail = MIMEMultipart()
41 newemail['Subject'] = self.email['Subject']
42 newemail['To'] = self.email['To']
43 newemail['From'] = self.email['From']
44 newemail.preamble = 'There are attachments\n'
45 newemail.epilogue = ''
46 self.email = newemail
47
48 f = File.File(path)
49 filename = f.get_filename()
50 mt = f.get_mimetype()
51 maintype, subtype = mt.split('/', 1)
52 if maintype == 'text':
53 fp = open(path)
54
55 msg = MIMEText(fp.read(), _subtype=subtype)
56 fp.close()
57 elif maintype == 'image':
58 fp = open(path, 'rb')
59 msg = MIMEImage(fp.read(), _subtype=subtype)
60 fp.close()
61 elif maintype == 'audio':
62 fp = open(path, 'rb')
63 msg = MIMEAudio(fp.read(), _subtype=subtype)
64 fp.close()
65 else:
66 fp = open(path, 'rb')
67 msg = MIMEBase('application', 'octet-stream')
68 msg.set_payload(fp.read())
69 fp.close()
70
71 Encoders.encode_base64(msg)
72
73 msg.add_header('Content-Disposition', 'attachment', filename=filename)
74 self.email.attach(msg)
75 self.attachments.append(path)
76
78 self.email = email.message_from_string(text_source)
79
81 return self.email.as_string()
82
84 return self.email['Subject']
85
90
94
97