import email


def fetch_emails(connection, search_string = '(UNSEEN)'):
    """Fetch emails on a connection, specified by a search

    Usage:
    fetch_emails(connection[, search_string])
        connection -- an IMAP connection, which should be logged in and
                      pointed at a particular mailbox
        search_string -- an IMAP search string that specifies the
                         messages to be fetched from the connection. 
                         Defaults to "(UNSEEN)", which fetches unread emails.
    
    Returns a list of email.message objects
    """
    
    matching_emails = connection.search(None,search_string)[1]
    matching_emails = matching_emails[0].decode().split()
    messages = [connection.fetch(email, '(RFC822)') for email in
                matching_emails]
    messages = [email.message_from_bytes(msg[1][0][1]) for msg in messages]
    return messages