Name

Mail:Alias::LocalFile

Resolve email aliases from a locally maintained aliases file in addition to the system-wide aliases file 

Version

version 0.01

Synopsis

    use Mail::Alias::LocalFile;

    my $resolver   = Mail::Alias::LocalFile->new(aliases => $aliases);
    my $result     = $resolver->resolve_recipients($intended_recipients);
    my $recipients = $result->{recipients};
    my $warning    = $result->{warning};

    - $aliases is a hashref of a local aliases file

    - $intended_recipients is a list or arrayref of addressees 
      (i.e. mary@example.com, joe, group2)

    - $recipients is the comma separated list of email addressees 
      needed by you email client code

    - $warning identifies problems encountered (if any)

    See below and use perldoc LocalFile.pm for additional documentation

Description

This module allows the use of a locally maintained aliases file in addition to the aliases file provided by the Mail Transfer Agent (MTA). This module reduces dependence on the system wide MTA aliases file. You can avoid the use of the system aliases entirely or you can use some of them to supplement your local maintained aliases. This module is useful when you want to maintain your own email aliases file, with limited use of the system wide aliases file.

The type of local file holding the alias definitions is up to your application. File types such as JSON, YAML, XML, INI and many others could be used, provided the file loads an appropriately structured hashref.

Rational

This module allows the use of a locally maintained aliases file in addition to using the aliases file used by the MTA. This is beneficial for several reasons:

Input

In your application, create a new resolver object - using Moo style named parameters

Where inputs are:

Output

    my $recipients           = $result->{recipients};
    my $warning              = $result->{warning};
    my $alias_file_contents  = $result->{aliases};
    my $original_input       = $result->{original_input};
    my $processed_aliases    = $result->{processed_aliases};
    my $uniq_email_addresses = $result->{uniq_email_addresses};
    my $expanded_addresses   = $result->{expanded_addresses};

Output includes all of the following to use as desired:

$recipients is the same content as $uniq_email_addresses, except it is held as the comma separated string most likely desired by your email code

Dependencies

The Mail::Alias::LocalFile.pm package uses:
- use Moo; 
- use namespace::autoclean;
- use Email::Valid;
- use Scalar::Util qw(reftype);
- use Types::Standard qw(ArrayRef HashRef Str);

This module is NOT dependent on the (excellent) Mail::Alias module. Mail::Alias is designed to read, write, update and convert between the Sendmail, Ucbmail and Binmail MTA alias file formats. This module has a diffferent purpose, which is to avoid reliance on the MTA alias file entirely or just to the extent desired.

INSTALLATION

To install this module:
   perl Makefile.PL
   make
   make test
   make install

Assumptions:

Sample Aliases Files:

Sample configuration files (YAML and JSON formatted) are provided as examples of locally maintained aliases files that load as a hash_ref containing acceptable keys and values. They intentionally use various (unlikely) types of comma and space separation to demonstrate the flexibility allowed in value formatting. They hold examples of the 'mta_' prefix used to include aliases from the system MTA aliases file. Some sample files intentionally contain circular references for aliases, for illustrative purposes.

Sample scripts are included to demonstrate usage and capabilities. Use perldoc for POD descriptions.

Limitations and Applicability

Regular aliases and mta_ prefix aliases

Functionality:

Methods are described within the POD. (perldoc LocalFile.pm)

Method provided functionality includes:

Detect and report circular references

LocalFile.pm is generally tolerant of circular alias references within the local aliases file. An attempt is made to avoid a loop by only expanding each alias once. Nevertheless, good practice necessitates removing circular references whenever possible. Screen for circular references in the local aliases file is follows:

$resolver = Mail::Alias::LocalFile->new(aliases => $aliases);
@circular_refs = $resolver->detect_circular_references($aliases);

if ($circular_refs[0]) {
    print "WARNING: Circular references detected:\n";
    foreach my $circ_ref_item (@circular_refs) {
        print "  $circ_ref_item\n";
    }
}

Issues

Copyright

Copyright 2025, Dwight R. Brewer (Russ), All Rights Reserved

LICENSE

GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999. See included LICENSE file.

Author

Russ Brewer rbrew@cpan.org