def imap_operation(search_str, criteria_field='from', logic_operator='or', previous_operation=''):
    """
    Layers IMAP logic operators.
    For example, you want to search for emails FROM 'grandma' OR FROM 'grandpa@example.com', do this:
    
    
    x = imap_operation('grandma', 'from', 'or')

    x = imap_operation('grandpa@example.com', 'from', 'or', x)
    
    :param search_str: A string of criteria to search for.
    :param criteria_field: Can be an IMAP search criteria field such as 'from', 'to', 'subject', etc.
    :param logic_operator: Can be 'or' or 'and'.
    :param previous_operation: Use to chain multiple criteria.
    :return: A string of IMAP search criteria.
    """
    
    if logic_operator.upper() not in ['OR', 'AND']:
        print('Invalid logic operator')
        raise ValueError
    
    if previous_operation != '':
        previous_operation += ' '
    
    return previous_operation + logic_operator.upper() + ' ' + criteria_field.upper() + ' ' + search_str