00001 import sys 00002 import types 00003 00004 try: 00005 from cStringIO import StringIO 00006 except ImportError: 00007 from StringIO import StringIO 00008 00009 from vyperlogix import misc 00010 from vyperlogix.misc import ObjectTypeName 00011 from vyperlogix.classes.CooperativeClass import Cooperative 00012 00013 ## 00014 # 00015 # Represents an email message for sending. 00016 # 00017 # Creates full text implementation on initialization as well as storing 00018 # all fields as member variables. 00019 # 00020 # fromAdd - single address of message sender 00021 # toAdds - single address or list of addresses of primary recipients 00022 # body - Text of message body as string 00023 # subject - optional subject string 00024 # ccAdds - optional single address or list of addresses of copy recipients 00025 # bccAdds - optional single address or list of addresses of 00026 # blindcopy recipients 00027 # 00028 class Message(Cooperative): 00029 ## 00030 # passThru set to True tells the system to refrain from formatting the message. 00031 def __init__(self, fromAdd, toAdds, body, subject="", ccAdds=None, bccAdds=None, passThru=False): 00032 self.fromAdd = fromAdd 00033 00034 self.toAddList = self.stringOrIterable(toAdds) 00035 self.ccAddList = self.stringOrIterable(ccAdds) 00036 self.bccAddList = self.stringOrIterable(bccAdds) 00037 00038 self.subject = subject 00039 self.body = body 00040 00041 self.message = body 00042 if (not passThru): 00043 self.buildMsg() 00044 return 00045 00046 def __str__(self): 00047 return "%s" %self.message 00048 00049 def __repr__(self): 00050 return "Message('%s', %s, '''%s''', %s, %s, %s)" %(self.fromAdd, 00051 self.toAddList, 00052 self.body, 00053 self.subject, 00054 self.ccAddList, 00055 self.bccAddList) 00056 00057 ## 00058 # 00059 # if value is not a list or tuple, packs value as one item list 00060 # before returning it 00061 # 00062 def stringOrIterable(self, value): 00063 if value is None: 00064 return value 00065 elif type(value) == types.TupleType or type(value) == types.ListType: 00066 return value 00067 else: 00068 return [value] 00069 00070 ## 00071 # 00072 # Builds and stores message text based on the member components. 00073 # Call this if any of the data (recipients, subject, body) has changed 00074 # after the object has been instantiated. 00075 # 00076 def buildMsg(self, noSubject=False): 00077 msgBuf = StringIO() 00078 00079 # build the header 00080 msgBuf.write('From: %s\r\n' % self.fromAdd) 00081 00082 msgBuf.write('To: %s\r\n' %','.join(self.toAddList)) 00083 00084 if self.ccAddList is not None: 00085 msgBuf.write('CC: %s\r\n' %','.join(self.ccAddList)) 00086 00087 if self.bccAddList is not None: 00088 msgBuf.write('BCC: %s\r\n' %','.join(self.bccAddList)) 00089 00090 if (len(self.subject) > 0): 00091 msgBuf.write('Subject: %s\r\n' %self.subject.encode('ascii','replace')) 00092 00093 msgBuf.write('\r\n') # Separator break btwn hdr and msg 00094 00095 # Add the body 00096 00097 msgBuf.write(self.body) # .encode('ascii','replace') 00098 00099 self.message = msgBuf.getvalue() 00100 00101 return self.message 00102 00103 ## 00104 # 00105 # Represents an email message as HTML for sending. 00106 # 00107 # Creates full text implementation on initialization as well as storing 00108 # all fields as member variables. 00109 # 00110 # fromAdd - single address of message sender 00111 # toAdds - single address or list of addresses of primary recipients 00112 # body - Text of message body as string 00113 # subject - optional subject string 00114 # ccAdds - optional single address or list of addresses of copy recipients 00115 # bccAdds - optional single address or list of addresses of 00116 # blindcopy recipients 00117 # 00118 class HTMLMessage(Message): 00119 def __init__(self, fromAdd, toAdds, body, subject="", ccAdds=None, bccAdds=None): 00120 from vyperlogix.mail import html 00121 content = self.renderHTML(subject,body) 00122 body = body if (isinstance(body,list)) else body.split('\r\n') if (body.find('\r\n') > -1) else body.split('\r') if (body.find('\r') > -1) else body.split('\n') if (body.find('\n') > -1) else [body] 00123 self.__text_body__ = '<br/>'.join(body) 00124 self.__html_body__ = html.asHTMLEmail(self.__text_body__,content,subject) 00125 super(HTMLMessage, self).__init__(fromAdd, toAdds, self.__html_body__, subject=subject, ccAdds=ccAdds, bccAdds=bccAdds, passThru=True) 00126 00127 def html_body(): 00128 doc = "html_body" 00129 def fget(self): 00130 return self.__html_body__ 00131 return locals() 00132 html_body = property(**html_body()) 00133 00134 def text_body(): 00135 doc = "text_body" 00136 def fget(self): 00137 return self.__text_body__ 00138 return locals() 00139 text_body = property(**text_body()) 00140 00141 def renderHTML(self,subj,body): 00142 from vyperlogix.html import myOOHTML as oohtml 00143 h_html = oohtml.Html() 00144 00145 if (misc.isString(subj)) and (misc.isList(body)): 00146 h_html.text(oohtml.oohtml.DOCTYPE_40_TRANSITIONAL) 00147 00148 html_html = h_html.tag(oohtml.oohtml.HTML) 00149 head_html = html_html.tag(oohtml.oohtml.HEAD) 00150 head_html.tagOp(oohtml.oohtml.META, http_equiv=oohtml.oohtml.CONTENT_TYPE, content=oohtml.oohtml.TEXT_HTML_CHARSET_ISO_8859_1) 00151 head_html.metas( 00152 (oohtml.oohtml.AUTHOR, "Ray C Horn (rhorn@magma-da.com)"), 00153 (oohtml.oohtml.KEYWORDS, "SMTP Email Message"), 00154 (oohtml.oohtml.DESCRIPTION, "The contents of this email are considered to be confidential unless otherwise specified."), 00155 (oohtml.oohtml.ROBOTS, oohtml.oohtml.ALL)) 00156 head_html.tagTITLE('SMTP Email Message') 00157 body_html = html_html.tag(oohtml.oohtml.BODY) 00158 idContent = body_html.tag(oohtml.oohtml.DIV, id="content", style="background-color: white") 00159 00160 idContent.html_simple_table(body) 00161 pass 00162 else: 00163 print >>sys.stderr, '%s :: "subj" parm must be of type str and "body" parm must be of type list rather than of types "%s" and "%s", respectively.' % (ObjectTypeName.objectSignature(self),type(subj),type(body)) 00164 return h_html.toHtml() 00165 00166 00167
© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."
Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.
122,241 lines of code and growing...