1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
package Email::Abstract;
use Carp;
use Email::Simple;
use 5.006;
use strict;
use warnings;
our $VERSION = '2.01';
use Module::Pluggable search_path => [ __PACKAGE__ ], require => 1;
my @plugins = __PACKAGE__->plugins(); # Requires them.
for my $func (qw(get_header get_body
set_header set_body
as_string)) {
no strict 'refs';
*$func = sub {
my ($class, $thing, @args) = @_;
$thing = Email::Simple->new($thing) unless ref $thing;
my $target = ref $thing;
$target =~ s/:://g;
$class .= "::".$target;
if ($class->can($func)) {
$class->$func($thing, @args);
} else {
for my $class (@plugins) {
if ($class->can("target") and $thing->isa($class->target)) {
return $class->$func($thing, @args);
}
}
croak "Don't know how to handle ".ref($thing);
}
};
}
sub cast {
my ($class, $thing, $target) = @_;
$thing = $class->as_string($thing) if ref $thing;
$target =~ s/:://g;
$class .= "::".$target;
if ($class->can("construct")) {
$class->construct($thing);
} else {
for my $class (@plugins) {
if ($class->can("target") and $thing->isa($class->target)) {
return $class->construct($thing);
}
}
croak "Don't know how to handle $class";
}
}
# Preloaded methods go here.
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
Email::Abstract - Unified interface to mail representations
=head1 SYNOPSIS
my $message = Mail::Message->read($rfc822)
|| Email::Simple->new($rfc822)
|| Mail::Internet->new([split /\n/, $rfc822])
|| ...;
my $subject = Email::Abstract->get_header($message, "Subject");
Email::Abstract->set_header($message, "Subject", "My new subject");
my $body = Email::Abstract->get_body($message);
Email::Abstract->set_body($message, "Hello\nTest message\n");
$rfc822 = Email::Abstract->as_string($message);
my $mail_message = Email::Abstract->cast($message, "Mail::Message");
=head1 DESCRIPTION
C<Email::Abstract> provides module writers with the ability to write
representation-independent mail handling code. For instance, in the
cases of C<Mail::Thread> or C<Mail::ListDetector>, a key part of the
code involves reading the headers from a mail object. Where previously
one would either have to specify the mail class required, or to build a
new object from scratch, C<Email::Abstract> can be used to perform
certain simple operations on an object regardless of its underlying
representation.
C<Email::Abstract> currently supports C<Mail::Internet>,
C<MIME::Entity>, C<Mail::Message>, C<Email::Simple> and C<Email::MIME>.
Other representations are encouraged to create their own
C<Email::Abstract::*> class by copying C<Email::Abstract::EmailSimple>.
All modules installed under the C<Email::Abstract> hierarchy will be
automatically picked up and used.
=head1 METHODS
=head2 get_header($obj, $header)
This returns the value or list of values of the given header.
=head2 set_header($obj, $header, @lines)
This sets the C<$header> header to the given one or more values.
=head2 get_body($obj)
This returns the body as a string.
=head2 set_body($obj, $string)
This changes the body of the email to the given string.
=head2 as_string($obj)
This returns the whole email as a string.
=head1 AUTHOR
Casey West, <F<casey@geeknest.com>>
Simon Cozens, <F<simon@cpan.org>>
=head1 COPYRIGHT AND LICENSE
Copyright 2004 by Simon Cozens
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
http://pep.kwiki.org
=cut
|