emailrelay-list-verify.pyΒΆ

#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: 2026 Graeme Walker <graeme_walker@users.sourceforge.net>
# SPDX-License-Identifier: FSFAP
#
# Copyright (c) 2026 Graeme Walker <graeme_walker@users.sourceforge.net>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.
# ===
#
# emailrelay-list-verify.py
#
# An example E-MailRelay address verifier script that uses
# a hard-coded list of recipient addresses.
#

import sys

try:
    to = sys.argv[1]
    domain = sys.argv[4]

    # list of recognised recipient addresses
    #
    users = {
            "alice" : "" , # valid
            "bob" : "" , # valid
            "carol" : "carol.smith@"+domain , # valid, with mapping
            "dave" : "@david" , # local mailbox
            "erol" : "@" , # local mailbox
            "malory" : None , # abort
    }

    # split the recipient address and check the domain part
    user_part = to.rsplit("@")[0]
    domain_part = to.rsplit("@")[1]
    if domain_part.lower() != domain.lower():
            print("invalid address")
            print("invalid domain-part ["+domain_part+"]")
            sys.exit(2)

    # case-insensitive match of the user part
    user_key = ""
    user_value = ""
    for user in users:
            if user.lower() == user_part.lower():
                    user_key,user_value = user,users[user]

    if user_value is not None and len(user_value) and user_value[0] == "@":
            # valid local recipient address with a mailbox
            mailbox = user_value[1:]
            if len(mailbox) == 0:
                     mailbox = user_part
            print(to)
            print(mailbox)
            sys.exit(0)

    elif len(user_key) and user_value is not None:
            # valid recipient address
            address = user_value
            if len(address) == 0:
                    address = user_part + "@" + domain
            print("")
            print(address)
            sys.exit(1)

    elif user_value is None:
            # abort
            sys.exit(100)

    else:
            print("invalid address")
            print("invalid user-part ["+user_part+"]")
            sys.exit(2)

except Exception as e:
    t,o,tb = sys.exc_info()
    print("error")
    print("exception: ["+str(e)+"] on line "+str(tb.tb_lineno))
    sys.exit(3)