'Returns new email messages in a list.'

def get_messages(self):

    if (not self.check_mails()):
        return
    frappe.db.commit()
    if (not self.connect()):
        return
    uid_list = []
    try:
        self.errors = False
        self.latest_messages = []
        self.seen_status = {}
        self.uid_reindexed = False
        uid_list = email_list = self.get_new_mails()
        if (not email_list):
            return
        num = num_copy = len(email_list)
        if (num > 50):
            num = 50
        self.total_size = 0
        self.max_email_size = cint(frappe.local.conf.get(u'max_email_size'))
        self.max_total_size = (5 * self.max_email_size)
        for (i, message_meta) in enumerate(email_list):
            if ((i + 1) > num):
                break
            try:
                self.retrieve_message(message_meta, (i + 1))
            except (TotalSizeExceededError, EmailTimeoutError, LoginLimitExceeded):
                break
        num = num_copy
        if (not cint(self.settings.use_imap)):
            if ((num > 100) and (not self.errors)):
                for m in range(101, (num + 1)):
                    self.pop.dele(m)
    except Exception as e:
        if self.has_login_limit_exceeded(e):
            pass
        else:
            raise
    finally:
        if cint(self.settings.use_imap):
            self.imap.logout()
        else:
            self.pop.quit()
    out = {u'latest_messages': self.latest_messages}
    if self.settings.use_imap:
        out.update({u'uid_list': uid_list, u'seen_status': self.seen_status, u'uid_reindexed': self.uid_reindexed})
    return out
