def handle(self, *args, **options): if (settings.PROD_EMAIL.lower() == "true" and Mailbox.objects.filter(name="default").exists()): mbx = Mailbox(active=True, name="default", uri=settings.IMAP_URI, from_email=settings.EMAIL_HOST_USER) mbx.save()
def test_protocol_info(self): mailbox = Mailbox() mailbox.uri = 'alpha://test.com' expected_protocol = 'alpha' actual_protocol = mailbox._protocol_info.scheme self.assertEqual( expected_protocol, actual_protocol, )
def checkMail(): """ Celery task: checks mailbox for new emails with links. If found, adds links to db. """ logger.debug("Checking for new mail") boxes = Mailbox.objects.filter(name=MAIL_CONFIG["name"]) if boxes.count() == 0: mailBox = Mailbox(name=MAIL_CONFIG["name"], uri=MAIL_CONFIG["uri"]) mailBox.save() else: mailBox = boxes[0] logger.debug("Retrieving new mail from mailbox: " + mailBox.name) newMessages = mailBox.get_new_mail() if newMessages: logger.info('[' + mailBox.name + '] Got new messages: ' + str(newMessages)) for msg in newMessages: # find user for given email address try: user = User.objects.get(email=msg.from_address[0]) except User.DoesNotExist: logger.warning('Got email from unknown user: '******'. Ignoring.') continue # extract url from message url = re.search("(?P<url>https?://[^\s]+)", msg.get_text_body()) if url: url = url.group("url") else: logger.warning('Got email without url from user: '******'. Ignoring.') continue # extract description from message description = re.sub(r'https?:\/\/[^\s]+', '', msg.get_text_body(), flags=re.MULTILINE) link = Link(user=user, title=msg.subject, link=url, description=description) # don't let mezzanine screw up our description link.gen_description = False link.save() logger.info('User ' + user.username + ' added new link by email: ' + url)
def test_last_polling_field_exists(self): mailbox = Mailbox() self.assertTrue(hasattr(mailbox, 'last_polling'))
def handle(self, *args, **kwargs): if connected_to_internet() is False: return None received_registries = [] received_data = [] email = SYNCDATA_FETCHING_EMAIL_ID.replace('@','%40') metabox = Mailbox() metabox.name = 'Metabox' metabox.uri = 'imap+ssl://' + email + ':' + SYNCDATA_FETCHING_EMAIL_ID_PASSWORD + '@' + SYNCDATA_FETCHING_IMAP_SERVER_ADDRESS + '?archive=METABOX_SYNCDATA' try: metabox.get_connection() except IMAP4.error as error_obj: print "Syncdata emailid, password or imap server address improperly configured! Please check settings.py" print str(error_obj) return 'Syncdata emailid, password or imap server address improperly configured! Please check settings.py' except Exception as error_obj: print 'Some error occured while trying to establish connection with : %s for fetching syncdata', SYNCDATA_FETCHING_EMAIL_ID print str(error_obj) return str(error_obj) metabox.save() try: if connected_to_internet: syncdata_mails_list = metabox.get_new_mail() if syncdata_mails_list: for mail in syncdata_mails_list: if mail.subject.find('SYNC') != '-1': serverdir = process_mails(mail) #received_registries += serverdir if serverdir not in received_registries: received_registries.append(serverdir) result=chain_and_check_registry(received_registries) print result if result: server_sync(result) print "Data Inserted. Deleting Json Files" delete(result) else: print 'No new mails received' metabox.delete() except Exception as error: #delete temporary mailbox from database metabox.delete() print "The following error occured while fetching syncdata mails: " print str(error) traceback.print_exc()
def handle(self, *args, **kwargs): if connected_to_internet() is False: return None received_registries = [] received_data = [] email = SYNCDATA_FETCHING_EMAIL_ID.replace('@', '%40') metabox = Mailbox() metabox.name = 'Metabox' metabox.uri = 'imap+ssl://' + email + ':' + SYNCDATA_FETCHING_EMAIL_ID_PASSWORD + '@' + SYNCDATA_FETCHING_IMAP_SERVER_ADDRESS + '?archive=METABOX_SYNCDATA' try: metabox.get_connection() except IMAP4.error as error_obj: print "Syncdata emailid, password or imap server address improperly configured! Please check settings.py" print str(error_obj) return 'Syncdata emailid, password or imap server address improperly configured! Please check settings.py' except Exception as error_obj: print 'Some error occured while trying to establish connection with : %s for fetching syncdata', SYNCDATA_FETCHING_EMAIL_ID print str(error_obj) return str(error_obj) metabox.save() try: if connected_to_internet: syncdata_mails_list = metabox.get_new_mail() if syncdata_mails_list: for mail in syncdata_mails_list: if mail.subject.find('SYNC') != '-1': serverdir = process_mails(mail) #received_registries += serverdir if serverdir not in received_registries: received_registries.append(serverdir) result = chain_and_check_registry(received_registries) print result if result: server_sync(result) print "Data Inserted. Deleting Json Files" delete(result) else: print 'No new mails received' metabox.delete() except Exception as error: #delete temporary mailbox from database metabox.delete() print "The following error occured while fetching syncdata mails: " print str(error) traceback.print_exc()