'Called by scheduler to receive emails from this EMail account using POP3/IMAP.'

def receive(self, test_mails=None):

    def get_seen(status):
        if (not status):
            return None
        seen = (1 if (status == u'SEEN') else 0)
        return seen
    if self.enable_incoming:
        uid_list = []
        exceptions = []
        seen_status = []
        uid_reindexed = False
        if frappe.local.flags.in_test:
            incoming_mails = test_mails
        else:
            email_sync_rule = self.build_email_sync_rule()
            email_server = self.get_incoming_server(in_receive=True, email_sync_rule=email_sync_rule)
            if (not email_server):
                return
            emails = email_server.get_messages()
            if (not emails):
                return
            incoming_mails = emails.get(u'latest_messages', [])
            uid_list = emails.get(u'uid_list', [])
            seen_status = emails.get(u'seen_status', [])
            uid_reindexed = emails.get(u'uid_reindexed', False)
        for (idx, msg) in enumerate(incoming_mails):
            try:
                uid = (None if (not uid_list) else uid_list[idx])
                args = {u'uid': uid, u'seen': (None if (not seen_status) else get_seen(seen_status.get(uid, None))), u'uid_reindexed': uid_reindexed}
                communication = self.insert_communication(msg, args=args)
            except SentEmailInInbox:
                frappe.db.rollback()
            except Exception:
                frappe.db.rollback()
                log(u'email_account.receive')
                if self.use_imap:
                    self.handle_bad_emails(email_server, msg[1], msg[0], frappe.get_traceback())
                exceptions.append(frappe.get_traceback())
            else:
                frappe.db.commit()
                if communication:
                    attachments = [d.file_name for d in communication._attachments]
                    communication.notify(attachments=attachments, fetched_from_email_account=True)
        if ((len(incoming_mails) > 0) and (not frappe.local.flags.in_test)):
            frappe.publish_realtime(u'new_email', {u'account': self.email_account_name, u'number': len(incoming_mails)})
        if exceptions:
            raise Exception(frappe.as_json(exceptions))
