Package conduit :: Package datatypes :: Module Email
[hide private]

Source Code for Module conduit.datatypes.Email

 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"
21 - def __init__(self, **kwargs):
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
32 - def has_attachments(self):
33 if len(self.attachments) > 0: 34 return True 35 return False
36
37 - def add_attachment(self, path):
38 #Create a multipart message and each attachment gets a part 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 #We should handle calculating the charset 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 # Encode the payload using Base64 71 Encoders.encode_base64(msg) 72 # Set the filename parameter 73 msg.add_header('Content-Disposition', 'attachment', filename=filename) 74 self.email.attach(msg) 75 self.attachments.append(path)
76
77 - def set_from_email_string(self, text_source):
78 self.email = email.message_from_string(text_source)
79
80 - def get_email_string(self):
81 return self.email.as_string()
82
83 - def get_subject(self):
84 return self.email['Subject']
85
86 - def __getstate__(self):
87 data = DataType.DataType.__getstate__(self) 88 data['email'] = self.get_email_string() 89 return data
90
91 - def __setstate__(self, data):
92 self.set_from_email_string(data['email']) 93 DataType.DataType.__setstate__(self, data)
94
95 - def get_hash(self):
96 return hash( self.get_email_string() )
97