emailrelay-list-verify.plΒΆ

#!/usr/bin/env perl
#
# 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.pl
#
# An example E-MailRelay address verifier script that uses
# a hard-coded list of recipient addresses.
#

use strict ;

my ( $to, $from, $ip, $domain, $mechanism, $id ) = @ARGV ;

# list of recognised recipient addresses
#
my %users = (
    alice => '' , # valid
    bob => '' , # valid
    carol => 'carol.smith@'.$domain , # valid, with mapping
    dave => _local("david") , # local mailbox
    erol => _local() , # local mailbox
    malory => undef , # abort
) ;
sub _local { return \$_[0] }

# split the recipient address and check the domain part
my ( $user_part , $domain_part ) = ( $to =~ m/([^@]+)@(.*)/ ) ;
if( lc($domain_part) ne lc($domain) )
{
    print "invalid address\n" ;
    print "invalid domain-part [$domain_part]\n" ;
    exit( 2 ) ;
}

# case-insensitive match of the user part
my ($user) = grep { lc($user_part) eq lc($_) } keys %users;

if( ref($users{$user}) )
{
    # valid local recipient address with a mailbox
    my $mailbox = ${$users{$user}} ;
    $mailbox ||= $user_part ;
    print $to , "\n" ;
    print $mailbox , "\n" ;
    exit 0 ;
}
elsif( defined($users{$user}) )
{
    # valid recipient address
    my $address = $users{$user} || "$user_part\@$domain" ;
    print "\n" ;
    print $address , "\n" ;
    exit 1 ;
}
elsif( defined($user) )
{
    # abort
    exit 100 ;
}
else
{
    print "invalid address\n" ;
    print "invalid user-part [$user_part]\n" ;
    exit 2 ;
}