Email::Find

By eric

Description: Email::Find is a module for finding a subset of RFC 822 email addresses in arbitrary text. The addresses it finds are not guaranteed to exist or even actually be email addresses at all, but they will be valid RFC 822 syntax. Email::Find will perform some heuristics to avoid some of the more obvious red herrings and false addresses, but there’s only so much which can be done without a human. (Note: Taken from the author’s description)

CPAN: Email::Find

Example 1:
I often find myself with a list of email address in an Excel spreadsheet or on an email that someone sent to me with a list of names. Normally I have to take this list and resend a message to the recipients without allowing all the addresses to be seen. Since according to RFC 2822, there are multiple implementations of how a message with BCC recipients can be sent, I find it is easier to break the recipients down myself and have them presented to me in a way that I can use them in a quantity in which they are useful to me.
Excerpt from RFC 2822:

The “Bcc:” field (where the “Bcc” means “Blind Carbon Copy”) contains addresses of recipients of the message whose addresses are not to be revealed to other recipients of the message. There are three ways in which the “Bcc:” field is used. In the first case, when a message containing a “Bcc:” field is prepared to be sent, the “Bcc:” line is removed even though all of the recipients (including those specified in the “Bcc:” field) are sent a copy of the message. In the second case, recipients specified in the “To:” and “Cc:” lines each are sent a copy of the message with the “Bcc:” line removed as above, but the recipients on the “Bcc:” line get a separate copy of the message containing a “Bcc:” line. (When there are multiple recipient addresses in the “Bcc:” field, some implementations actually send a separate copy of the message to each recipient with a “Bcc:” containing only the address of that particular recipient.) Finally, since a “Bcc:” field may contain no addresses, a “Bcc:” field can be sent without any addresses indicating to the recipients that blind copies were sent to someone. Which method to use with “Bcc:” fields is implementation dependent, but refer to the “Security Considerations” section of this document for a discussion of each.

To accomplish this, I use variations of the following simple script:

# Always use these
use strict;
use warnings;
# Use the module itself
use Email::Find;

# Declare your variables before using them
my (@emails, $file, $counter);
# Limit the number of email addresses per line
my $max = 20;

# Code block to slurp the file on $ARGV[0]
{
  # Slurp the file by changing the Perl special variable
  local $/ = undef;
  local *FILE;
  open FILE, '<', "$ARGV[0]"
    or die "File Open Error: $!";
  $file  = <FILE>;
  close FILE
    or die "File Close Error: $!";
}

  # Prototype the pushing of all the addresses
  #  found onto an array leaving the original text
  my $finder = Email::Find->new(sub {
        my ($email, $orig_email) = @_;
        push @emails, $email->format;
        return $orig_email;
        });
  # Actually push the addresses found in $file
  $finder->find(\$file);

  # Loop and print $max email addresses per line
  #  separated by a semi-colon ';' and then each
  #  line separated by two carriage returns '\n'
  for my $address (@emails) {
    $counter++;
    print "$address;";
    print "\n\n"
      if $counter == $max;
    $counter = 0
      if $counter == $max;
  }

Follow My Travels

Buy My Book

Archives

  • 2020
  • 2019
  • 2017
  • 2014
  • 2013
  • 2012
  • 2011
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006

New Posts By Email

writing