[Commits] (bkirsch) First pass at smtp logic not ready for prime time yet

commits at osafoundation.org commits at osafoundation.org
Tue Aug 17 18:33:22 PDT 2004


Commit by: bkirsch
Modified files:
chandler/parcels/osaf/mail/MailTasks.py 1.3 1.4
chandler/parcels/osaf/mail/imap.py 1.10 1.11
chandler/parcels/osaf/mail/message.py 1.11 1.12
chandler/parcels/osaf/mail/parcel.xml 1.15 1.16
chandler/parcels/osaf/mail/smtp.py 1.1 1.2

Log message:
First pass at smtp logic not ready for prime time yet

ViewCVS links:
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/MailTasks.py.diff?r1=text&tr1=1.3&r2=text&tr2=1.4
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/imap.py.diff?r1=text&tr1=1.10&r2=text&tr2=1.11
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/message.py.diff?r1=text&tr1=1.11&r2=text&tr2=1.12
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/parcel.xml.diff?r1=text&tr1=1.15&r2=text&tr2=1.16
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/smtp.py.diff?r1=text&tr1=1.1&r2=text&tr2=1.2

Index: chandler/parcels/osaf/mail/MailTasks.py
diff -u chandler/parcels/osaf/mail/MailTasks.py:1.3 chandler/parcels/osaf/mail/MailTasks.py:1.4
--- chandler/parcels/osaf/mail/MailTasks.py:1.3	Tue Aug 17 11:07:29 2004
+++ chandler/parcels/osaf/mail/MailTasks.py	Tue Aug 17 18:33:21 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.3 $"
-__date__      = "$Date: 2004/08/17 18:07:29 $"
+__revision__  = "$Revision: 1.4 $"
+__date__      = "$Date: 2004/08/18 01:33:21 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -41,4 +41,4 @@
     def Execute(self, task):
 
        logging.info("SENDING STMP MAIL")
-       smtp.SMTPSender().sendmail()
+       #smtp.SMTPSender().sendmail()

Index: chandler/parcels/osaf/mail/imap.py
diff -u chandler/parcels/osaf/mail/imap.py:1.10 chandler/parcels/osaf/mail/imap.py:1.11
--- chandler/parcels/osaf/mail/imap.py:1.10	Tue Aug 17 14:47:57 2004
+++ chandler/parcels/osaf/mail/imap.py	Tue Aug 17 18:33:21 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.10 $"
-__date__      = "$Date: 2004/08/17 21:47:57 $"
+__revision__  = "$Revision: 1.11 $"
+__date__      = "$Date: 2004/08/18 01:33:21 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -111,8 +111,8 @@
         @return: C{None}
         """
 
-        if account is None:
-            raise IMAPMailException("You must pass in a Mail Account instance")
+        if account is None or not account.isItemOf(Mail.MailParcel.getIMAPAccountKind()):
+            raise IMAPMailException("You must pass in a IMAPAccount instance")
 
         viewName = "%s_%s" % (account.displayName, str(UUID.UUID()))
         super(IMAPDownloader, self).__init__(Globals.repository, viewName)
@@ -152,15 +152,12 @@
             if __debug__:
                 self.printCurrentView("__getMail")
 
-            self.account = self.__getAccount()
-            assert self.account is not None, "Account is None"
-
-            self.account.setPinned()
+            self.__getAccount()
 
             host    = self.account.host
             port    = self.account.port
-            useSSL  = self.account.useSSL
             portSSL = self.account.portSSL
+            useSSL  = self.account.useSSL
 
             if __debug__:
                 self.printAccount()
@@ -375,12 +372,13 @@
     def __getAccount(self):
 
         accountKind = Mail.MailParcel.getIMAPAccountKind()
-        account = accountKind.findUUID(self.accountUUID)
+        self.account = accountKind.findUUID(self.accountUUID)
+
+        if self.account is None: 
+            raise IMAPException("No Account for UUID: %s"% self.account.itsUUID)
 
-        if account is None: 
-            self.log.error("No Account for UUID: %s"% self.account.itsUUID)
+        self.account.setPinned()
 
-        return account
 
     def __printInfo(self, info):
 

Index: chandler/parcels/osaf/mail/message.py
diff -u chandler/parcels/osaf/mail/message.py:1.11 chandler/parcels/osaf/mail/message.py:1.12
--- chandler/parcels/osaf/mail/message.py:1.11	Tue Aug 17 14:47:57 2004
+++ chandler/parcels/osaf/mail/message.py	Tue Aug 17 18:33:21 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.11 $"
-__date__      = "$Date: 2004/08/17 21:47:57 $"
+__revision__  = "$Revision: 1.12 $"
+__date__      = "$Date: 2004/08/18 01:33:21 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -66,6 +66,9 @@
 
     return emailAddressOne.lower() == emailAddressTwo.lower()
 
+def dateTimeToRFC2882Date(dateTime):
+    return Utils.formatdate(dateTime.ticks(), True)
+
 def format_addr(emailAddress):
     if not isinstance(emailAddress, Mail.EmailAddress):
         return None

Index: chandler/parcels/osaf/mail/parcel.xml
diff -u chandler/parcels/osaf/mail/parcel.xml:1.15 chandler/parcels/osaf/mail/parcel.xml:1.16
--- chandler/parcels/osaf/mail/parcel.xml:1.15	Tue Aug 17 14:47:57 2004
+++ chandler/parcels/osaf/mail/parcel.xml	Tue Aug 17 18:33:21 2004
@@ -54,7 +54,7 @@
     </task:Schedule>
     <!--task:Action itsName="sendmail" itemClass="osaf.mail.MailTasks.SMTPSendAction"/>
     <task:schedule itemref="mail:SMTPClient/MailSchedule"/>
-    <task:actions itemref="mail:SMTPClient/sendmail"/-->
+    <task:actions itemref="mail:SMTPClient/sendMail"/-->
   </task:Task>
 
 </Parcel>

Index: chandler/parcels/osaf/mail/smtp.py
diff -u chandler/parcels/osaf/mail/smtp.py:1.1 chandler/parcels/osaf/mail/smtp.py:1.2
--- chandler/parcels/osaf/mail/smtp.py:1.1	Mon Aug  2 17:11:43 2004
+++ chandler/parcels/osaf/mail/smtp.py	Tue Aug 17 18:33:21 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.1 $"
-__date__      = "$Date: 2004/08/03 00:11:43 $"
+__revision__  = "$Revision: 1.2 $"
+__date__      = "$Date: 2004/08/18 01:33:21 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -8,8 +8,14 @@
 import twisted.mail.smtp as smtp
 import twisted.internet.reactor as reactor
 import twisted.internet.defer as defer
+import twisted.internet.ssl as ssl
 import email.Message as Message
 import logging as logging
+import common as common
+import message as message
+import osaf.contentmodel.mail.Mail as Mail
+import repository.util.UUID as UUID
+import mx.DateTime as DateTime
 
 try:
     from cStringIO import StringIO
@@ -17,47 +23,143 @@
     from StringIO import StringIO
 
 
+class SMTPMailException(common.MailException):
+    pass
+
 class SMTPSender(RepositoryView.AbstractRepositoryViewManager):
 
-    def __init__(self):
-        super(SMTPSender, self).__init__(Globals.repository, None)
+    def __init__(self, account, mailMessage):
+        if account is None or not account.isItemOf(Mail.MailParcel.getSMTPAccountKind()):
+            raise SMTPMailException("You must pass in a SMTPAccount instance")
 
-    def sendmail(self):
-        username = None 
-        password = None 
-        smtphost = "localhost"
-        from_addr = "brian"
-        to_addrs = ["brian"]
+        if mailMessage is None or not mailMessage.isItemOf(Mail.MailParcel.getMailMessageKind()):
+            raise SMTPMailException("You must pass in a mailParcel instance")
 
-        msg = Message.Message()
+        viewName = "%s_%s" % (account.displayName, str(UUID.UUID()))
 
-        if len(to_addrs) > 1:
-            msg['To'] = ', '.join(to_addrs)
-        else:
-            msg['To'] = to_addrs[0]
+        super(SMTPSender, self).__init__(Globals.repository, viewName)
+
+        self.accountUUID = account.itsUUID
+        self.account = None
+        self.mailMessageUUID = mailMessage.itsUUID
+        self.mailMessage = None
+
+    #in thread
+    def sendMail(self):
+        if __debug__:
+            self.printCurrentView("sendmail")
+
+        reactor.callFromThread(self.__sendMail)
+
+    #IN Twisted
+    def __sendMail(self):
+        self.setViewCurrent()
+
+        try:
+            if __debug__:
+                self.printCurrentView("__sendMail")
+
+            self.__getMessageAndAccount()
+
+            """ Refresh our view before adding items to our mail Message
+                and commiting. Will not cause merge conflicts since
+                no data changed in view in yet """
+            self.view.commit()
+
+            username  = self.account.username
+            passsword = self.account.password
+            host    = self.account.host
+            port    = self.account.port
+            portSSL = self.account.portSSL
+            useSSL  = self.account.useSSL
+            useAuth = self.account.useAuth
+            authRequired = True
+            sslRequired = False
+
+            if useSSL = 'SSL':
+                sslRequired = True
+
+            if not useAuth:
+                authRequired = False
+                username = None
+                password = None
+
+            self.mailMessage.outgoingMessage(account=self.account)
+
+            messageObject = message.kindToMessageObject(self.mailMessage)
+            messageText = messageObject.as_string()
+            self.mailMessage.rfc2882Message = message.strToText("rfc2822Message", messageText)
+
+            msg = StringIO(messageText)
+            d = defer.Deferred().addCallbacks(self.__mailSuccess, self.__mailFailure)
 
-        msg['From'] = from_addr
-        msg['Subject'] = "ESMTPTest"
-        msg.set_payload("This is body text")
+            #XXX: perhaps commit here
 
-        msg = StringIO(msg.as_string())
+            #XXX: Look in to Bcc Cc
+            to_addrs = messageObject['To']
+            from_addr = messageObject['From']
 
-        d = defer.Deferred().addCallbacks(self.mailSuccess, self.mailFailure)
+        finally:
+           self.restorePreviousView()
 
-        factory = smtp.ESMTPSenderFactory(username, password, from_addr, to_addrs, msg, d, 
-                                          0, requireAuthentication=False, requireTransportSecurity=False)
+        if useSSL = 'SSL':
+            factory = smtp.ESMTPSenderFactory(username, password, from_addr, to_addrs, msg, d,
+                                              0, requireAuthentication=authRequired, requireTransportSecurity=False)
 
-        reactor.callFromThread(reactor.connectTCP, smtphost, 25, factory)
+             """Won't see StartTLS here"""
+            reactor.connectSSL(host, portSSL, factory,
+                               ssl.ClientContextFactory(useM2=1))
 
-    def mailSuccess(self, result):
+        else:
+            #pass the context Factory in trySSL
+            factory = smtp.ESMTPSenderFactory(username, password, from_addr, to_addrs, msg, d,
+                                              0, requireAuthentication=authRequired, requireTransportSecurity=False)
+
+            reactor.connectTCP(host, port, factory)
+
+
+    """ 
+        Set the mail as sent 
+        set dateSent perhaps (need a string api for date sent)
+        commit mail
+    """
+    def __mailSuccess(self, result):
         addrs = []
 
         for address in result[1]:
             addrs.append(address[0])
 
-        str = "SMTP Message sent to %d recipients[%s]" % (result[0], ", ".join(addrs))
-        self.log.info(str)
+        self.log.info("SMTP Message sent to %d recipients[%s]" % (result[0], ", ".join(addrs))
+
+        date = DateTime.now()
+        self.mailMessage.dateSent = date
+        self.mailMessage.dateSentString = message.dateTimeToRFC2882Date(date)
+
+        self.mailMessage.deliveryExtension.sendSucceeded()
+
+        ### NOW Commit the message in a viewThread
 
     #TODO: Figure out what exc is for all cases
-    def mailFailure(self, exc):
+    def __mailFailure(self, exc):
         self.log.error("SMTP send failed: %s" % exc)
+
+        self.mailMessage.deliveryExtension.sendFailed()
+        ### Now Commit then post a event back to Don to Display
+
+    def __getMessageAndAccount(self):
+
+        accountKind = Mail.MailParcel.getSMTPAccountKind()
+        self.account = accountKind.findUUID(self.accountUUID)
+
+        mailMessageKind = Mail.MailParcel.getMailMessageKind()
+        self.mailMessage = mailMessageKind.findUUID(self.mailMessageUUID)
+
+        if self.account is None:
+            raise SMTPException("No Account for UUID: %s" % self.account.itsUUID)
+
+        if self.mailMessage is None:
+            raise SMTPException("No MailMessage for UUID: %s" % self.mailMessage.itsUUID)
+
+        self.account.setPinned()
+        self.mailMessage.setPinned()
+



More information about the Commits mailing list