package Email::Handle; use warnings; use strict; use base qw/Class::Accessor::Fast/; use overload '""' => \&as_string, fallback => 1; use Carp; use UNIVERSAL::require; our $VERSION = '0.01'; __PACKAGE__->mk_accessors(qw/user host/); sub new { my $class = shift; my $self = bless {}, $class; $self->_init(@_); return $self; } sub _init { my $self = shift; if (@_ > 0 and not @_ % 2) { my %args = @_; if ($args{user} && $args{host}) { $self->user($args{user}); $self->host($args{host}); } } else { $self->email(shift); } } sub email { my $self = shift; if (@_) { my ($user, $host) = $self->_parse_email(@_); if ($user && $host) { $self->user($user); $self->host($host); } } $self->user && $self->host ? join '@', $self->user, $self->host : ''; } sub _parse_email { my $self = shift; my $email = shift || ''; return split /@/, $email; } sub is_valid { my $self = shift; my %args = @_; my $validator = 'Email::Valid'; $validator = join '::', $validator, 'Loose' if delete $args{loose} or delete $args{-loose}; $validator->require; die $@ if $@; $validator->address(-address => $self->email, %args); } sub obfuscate { my $self = shift; require HTML::Email::Obfuscate; HTML::Email::Obfuscate->new(@_)->escape_html($self->email); } sub anonymize { # FIXME: method name my $self = shift; my $ph = shift; defined $ph or $ph = '...'; sprintf "%s@%s%s", $self->user, substr($self->host, 0, 1), $ph; } our $MIME_CLASS = 'MIME::Lite'; sub mime { my $self = shift; $MIME_CLASS->require; die $@ if $@; $MIME_CLASS->new(To => $self->email, @_); } sub send { shift->mime(@_)->send; } sub as_string { shift->email; } 1; __END__ =head1 NAME Email::Handle - A Objective Email Handler =head1 SYNOPSIS use Email::Handle; my $email = Email::Handle->new('root@example.com'); print $email->is_valid ? 'yes' : 'no'; print $email->obfuscate; print $email->anonymize; print $email; $email->send(From => 'foo@example.com'); This module is also convenient for using on the DB application with L