Email::Address(3)
β¨ Email::Address(3) β¨
okay so bestie, this is the manual page for Email::Address β like, the Perl module that parses and creates RFC 2822 email addresses. it's giving "user contributed perl documentation" energy and honestly? iconic. π
πΈ NAME πΈ
Email::Address β RFC 2822 Address Parsing and Creation. no cap, that's literally what it does. π
π VERSION π
version 1.913 β yes bestie, we're on 1.913, keep up. β¨
π¦ SYNOPSIS π¦
okay so here's the vibe:
use Email::Address;
my @addresses = Email::Address->parse($line);
my $address = Email::Address->new(Casey => 'casey@localhost');
print $address->format;
slay. that's the whole flex. π«Ά
π DESCRIPTION π
so this class implements a regex-based RFC 2822 parser that locates email addresses in strings and returns a list of Email::Address objects it found. alternatively you can construct objects manually. the goal of this software is to be correct, and very very fast. periodt. π₯
β οΈ lowkey important warning bestie: version1.909and earlier of this module had vulnerabilities (CVE-2015-7686 and CVE-2015-12558) which allowed specially constructed email to cause a denial of service. the reported vulnerabilities and some other pathalogical cases (meaning they really shouldn't occur in normal email) have been addressed in version1.910and newer. if you're running version1.909or older, you should update! π
alternatively, you could switch to Email::Address::XS which has a backward compatible API. why not just use that? genuinely asking. π€
π§ Package Variables π§
ACHTUNG! email isn't easy (if even possible) to parse with a regex, at least if you're on a perl prior to 5.10.0. providing regular expressions for use by other programs isn't a great idea, because it makes it hard to improve the parser without breaking the "it's a regex" feature. using these regular expressions is not encouraged, and methods like Email::Address->is_addr_spec should be provided in the future. π
several regular expressions used in this package are useful to others. for convenience, these variables are declared as package variables that you may access from your program. π
these regular expressions conform to the rules specified in RFC 2822. β¨
you can access these variables using the full namespace. if you want short names, define them yourself.
my $addr_spec = $Email::Address::addr_spec;
$Email::Address::addr_specβ this regular expression defined what an email address is allowed to look like. π$Email::Address::angle_addrβ this regular expression defines an$addr_specwrapped in angle brackets. π¦$Email::Address::name_addrβ this regular expression defines what an email address can look like with an optional preceding display name, also known as the "phrase". πΈ$Email::Address::mailboxβ this is the complete regular expression defining an RFC 2822 email address with an optional preceding display name and optional following comment. π
π Class Methods π
β parse β
my @addrs = Email::Address->parse(
q[me@local, Casey <me@local>, "Casey" <me@local> (West)]
);
this method returns a list of Email::Address objects it finds in the input string. please note that it returns a list, and expects that it may find multiple addresses. the behavior in scalar context is undefined. π
the specification for an email address allows for infinitely nestable comments. that's nice in theory, but a little over done. by default this module allows for one (1) level of nested comments. if you think you need more, modify the $Email::Address::COMMENT_NEST_LEVEL package variable to allow more.
$Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep
the reason for this hardly-limiting limitation is simple: efficiency. no cap. β‘
long strings of whitespace can be problematic for this module to parse, a bug which has not yet been adequately addressed. the default behavior is now to collapse multiple spaces into a single space, which avoids this problem. to prevent this behavior, set $Email::Address::COLLAPSE_SPACES to zero. this variable will go away when the bug is resolved properly. π«Ά
in accordance with RFC 822 and its descendants, this module demands that email addresses be ASCII only. any non-ASCII content in the parsed addresses will cause the parser to return no results. π
π new π
my $address = Email::Address->new(undef, 'casey@local');
my $address = Email::Address->new('Casey West', 'casey@local');
my $address = Email::Address->new(undef, 'casey@local', '(Casey)');
constructs and returns a new Email::Address object. takes four positional arguments: phrase, email, and comment, and original string. π
the original string should only really be set using parse. that's the vibe. β¨
π₯ purge_cache π₯
Email::Address->purge_cache;
one way this module stays fast is with internal caches. caches live in memory and there is the remote possibility that you will have a memory problem. on the off chance that you think you're one of those people, this class method will empty those caches. π§Ή
ngl, "I've loaded over 12000 objects and not encountered a memory problem." β that's a direct quote from the author, bestie. so like, you're probably fine. π
π disable_cache / enable_cache π
Email::Address->disable_cache if memory_low();
if you'd rather not cache address parses at all, you can disable (and re-enable) the Email::Address cache with these methods. the cache is enabled by default. π
π¦ Instance Methods π¦
πΈ phrase πΈ
my $phrase = $address->phrase;
$address->phrase( "Me oh my" );
accessor and mutator for the phrase portion of an address. π
β¨ address β¨
my $addr = $address->address;
$addr->address( "me@PROTECTED.com" );
accessor and mutator for the address portion of an address. π
π comment π
my $comment = $address->comment;
$address->comment( "(Work address)" );
accessor and mutator for the comment portion of an address. π«Ά
π original π
my $orig = $address->original;
accessor for the original address found when parsing, or passed to new. π
β host β
my $host = $address->host;
accessor for the host portion of an address's address. π
π₯ user π₯
my $user = $address->user;
accessor for the user portion of an address's address. π§
π format π
my $printable = $address->format;
returns a properly formatted RFC 2822 address representing the object. slay. β¨
π¦ name π¦
my $name = $address->name;
this method tries very hard to determine the name belonging to the address. first the phrase is checked. if that doesn't work out the comment is looked into. if that still doesn't work out, the user portion of the address is returned. π
this method does not try to massage any name it identifies and instead leaves that up to someone else. who is it to decide if someone wants their name capitalized, or if they're Irish? π
π Overloaded Operators π
π stringify π
print "I have your email address, $address.";
objects stringify to format by default. it's possible that you don't like that idea. okay, then, you can change it by modifying $Email:Address::STRINGIFY. please consider modifying this package variable using local. you might step on someone else's toes if you don't. π
{
local $Email::Address::STRINGIFY = 'host';
print "I have your address, $address.";
# geeknest.com
}
print "I have your address, $address.";
# "Casey West" <casey@geeknest.com>
modifying this package variable is now deprecated. subclassing is now the recommended approach. periodt. π
π Did I Mention Fast? π
on his 1.8GHz Apple MacBook, rjbs gets these results:
$ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 5
Rate Mail::Address Email::Address
Mail::Address 2.59/s -- -44%
Email::Address 4.59/s 77% --
$ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 25
Rate Mail::Address Email::Address
Mail::Address 2.58/s -- -67%
Email::Address 7.84/s 204% --
$ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 50
Rate Mail::Address Email::Address
Mail::Address 2.57/s -- -70%
Email::Address 8.53/s 232% --
...unfortunately, a known bug causes a loss of speed the string to parse has certain known characteristics, and disabling cache will also degrade performance. π
π PERL VERSION π
this library should run on perls released even a long time ago. it should work on any version of perl released in the last five years. π«Ά
although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. the version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl. that's just how it is, bestie. π
π ACKNOWLEDGEMENTS π
thanks to Kevin Riggle and Tatsuhiko Miyagawa for tests for annoying phrase-quoting bugs! you're the real MVPs. π«Ά
π AUTHORS π
- Casey West
- Ricardo SIGNES
<cpan@semiotic.systems>
π CONTRIBUTORS π
- Alex Vandiver
<alex@chmrr.net> - David Golden
<dagolden@cpan.org> - David Steinbrunner
<dsteinbrunner@pobox.com> - Glenn Fowler
<cebjyre@cpan.org> - Jim Brandt
<jbrandt@bestpractical.com> - Kevin Falcone
<kevin@jibsheet.com> - Pali
<pali@cpan.org> - Ricardo Signes
<rjbs@semiotic.systems> - Ruslan Zakirov
<ruz@bestpractical.com> - sunnavy
<sunnavy@bestpractical.com> - William Yardley
<pep@veggiechinese.net>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2004 by Casey West. π
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay. β¨
okay that's the whole page bestie! this was perl v5.42.0 documentation from 2023-01-10, and honestly? it ate. π β¨ no cap, fr fr. now go parse some emails and remember: update your modules, don't nest comments too deep, and stay ASCII only. mwah! ππ¦πΈππ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Email-Address 1.913-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms