Commits

Josh VanderLinden committed 8f69d5e

Moved some of the mailbox connection commands

Comments (0)

Files changed (1)

articles/management/commands/check_for_articles_from_email.py

 
         import imaplib
 
+        M = None
         try:
             if (self.keyfile and self.certfile) or self.ssl:
                 M = imaplib.IMAP4_SSL(self.host, self.port, self.keyfile, self.certfile)
             else:
                 M = imaplib.IMAP4(self.host, self.port)
+
+            M.login(self.username, self.password)
+            M.select()
         except socket.error, err:
             raise
         else:
-            M.login(self.username, self.password)
-            M.select()
             return M
 
     def fetch(self):
 
         import poplib
 
+        M = None
         try:
             if (self.keyfile and self.certfile) or self.ssl:
                 M = poplib.POP3_SSL(self.host, self.port, self.keyfile, self.certfile)
             else:
                 M = poplib.POP3(self.host, self.port)
+
+            M.user(self.username)
+            M.pass_(self.password)
         except socket.error, err:
             raise
         else:
-            M.user(self.username)
-            M.pass_(self.password)
             return M
 
     def fetch(self):
 
             self.log('Deleting consumed messages')
             handle.delete_messages(created)
+        except socket.error:
+            self.log('Failed to communicate with mail server.  Please verify your settings.', 0)
         finally:
             if handle:
                 handle.disconnect()