NAME
Mail::MboxParser::Mail - Provide mail-objects and methods upon
SYNOPSIS
See Mail::MboxParser for examples on usage.
DESCRIPTION
Mail::MboxParser::Mail objects are usually not created directly though, in theory, they could be. A description of the provided methods can be found in Mail::MboxParser.
However, go on reading if you want to use methods from MIME::Entity and learn about overloading.
METHODS
- new(header, body)
-
This is usually not called directly but instead by get_messages(). You could however create a mail-object manually providing the header and body both as one string.
- header
-
Returns the mail-header as a hash-ref with header-fields as keys. All keys are turned to lower-case, so $header{Subject} has to be written as $header{subject}.
- body
- body(n)
-
Returns a Mail::MboxParser::Mail::Body object. For methods upon that see further below. When called with the argument n, the n-th body of the message is retrieved. That is, the body of the n-th entity.
Sets $message->error if something went wrong.
- find_body
-
This will return an index number that represents what Mail::MboxParser considers to be the actual (main)-body of an email. This is useful if you don't know about the structure of a message but want to retrieve the message's signature for instance:
$signature = $msg->body($msg->find_body)->signature;
Changes are good that find_body does what it is supposed to do.
- get_field(headerfield)
-
Returns the specified raw field from the message header, that is: no transformation or decoding is done. Returns multiple lines as needed if the field is "Received" or another multi-line field. Not case sensitive. Sets $mail->error() if the field was not found in which case get_field() returns undef.
- from
-
Returns a hash-ref with the two fields 'name' and 'email'. Returns undef if empty. The name-field does not necessarily contain a value either. Example:
print $mail->from->{email};
- to
-
Returns an array of hash-references of all to-fields in the mail-header. Fields are the same as those of $mail->from. Example:
for my $recipient ($mail->to) { print $recipient->{name} || "<no name>", "\n"; print $recipient->{email}; }
- cc
-
Identical with to() but returning the hash-refed "Cc: "-line.
- id
-
Returns the message-id of a message cutting off the leading and trailing '<' and '>' respectively.
- num_entitities
-
Returns the number of MIME-entities. That is, the number of sub-entitities actually. If 0 is returned and you think this is wrong, check $mail->log.
- get_entities
- get_entities(n)
-
Either returns an array of all MIME::Entity objects or one particular if called with a number. If no entity whatsoever could be found, an empty list is returned.
$mail->log instantly called after get_entities will give you some information of what internally may have failed. If set, this will be an error raised by MIME::Entity but you don't need to worry about it at all. It's just for the record.
- get_entity_body(n)
-
Returns the body of the n-th MIME::Entity as a single string, undef otherwise in which case you could check $mail->error.
- store_entity_body(n, handle => FILEHANDLE)
-
Stores the stringified body of n-th entity to the specified filehandle. That's basically the same as:
my $body = $mail->get_entity_body(0); print FILEHANDLE $body;
and could be shortened to this:
$mail->store_entity_body(0, handle => \*FILEHANDLE);
It returns a true value on success and undef on failure. In this case, examine the value of $mail->error since the entity you specified with 'n' might not exist.
- store_attachement(n) [sic!]
- store_attachement(n, options) [sic!]
-
It is really just a call to store_entity_body but it will take care that the n-th entity really is a saveable attachement. That is, it wont save anything with a MIME-type of, say, text/html or so.
Unless further 'options' have been given, an attachement (if found) is stored into the current directory under the recommended filename given in the MIME-header. 'options' are specified in key/value pairs:
key: | value: | description: ==========|==============|=============================== path | relative or | directory to store attachement (".") | absolute | | path | ==========|==============|=============================== code | an anonym | first argument will be the | subroutine | $msg-object, second one the | | index-number of the current | | MIME-part | | should return a filename for | | the attachement ==========|==============|=============================== args | additional | this array-ref will be passed | arguments as | on to the 'code' subroutine | array-ref | as a dereferenced array
Example:
$msg->store_attachement(1, path => "/home/ethan/", code => sub { my ($msg, $n, @args) = @_; return $msg->id."+$n"; }, args => [ "Foo", "Bar" ]);
This will save the attachement found in the second entity under the name that consists of the message-ID and the appendix "+1" since the above code works on the second entity (that is, with index = 1). 'args' isn't used in this example but should demonstrate how to pass additional arguments. Inside the 'code' sub, @args equals ("Foo", "Bar").
If 'path' does not exist, it will try to create the directory for you.
Returns the filename under which the attachement has been saved. undef is returned in case the entity did not contain a saveable attachement, there was no such entity at all or there was something wrong with the 'path' you specified. Check $mail->error to find out which of these possibilities appliy.
- store_all_attachements [sic!]
- store_all_attachements(options) [sic!]
-
Walks through an entire mail and stores all apparent attachements. 'options' are exactly the same as in store_attachement() with the same behaviour if no options are given.
Returns a list of files that has been succesfully saved and an empty list if no attachement could be extracted.
$mail->error will tell you possible failures and a possible explanation for that.
- make_convertable
-
Returns a Mail::MboxParser::Mail::Convertable object. For details on what you can do with it, read Mail::MboxParser::Mail::Convertable.
EXTERNAL METHODS
Mail::MboxParser::Mail implements an autoloader that will do the appropriate type-casts for you if you invoke methods from external modules. This, however, currently only works with MIME::Entity. Support for other modules will follow. Example:
my $mb = Mail::MboxParser->new("/home/user/Mail/received");
for my $msg ($mb->get_messages) {
print $msg->effective_type, "\n";
}
effective_type() is not implemented by Mail::MboxParser::Mail and thus the corresponding method of MIME::Entity is automatically called.
To learn about what methods might be useful for you, you should read the "Access"-part of the section "PUBLIC INTERFACE" in the MIME::Entity manpage. It may become handy if you have mails with a lot of MIME-parts and you not just want to handle binary-attachements but any kind of MIME-data.
OVERLOADING
Mail::MboxParser::Mail overloads the " " operator. Overloading operators is a fancy feature of Perl and some other languages (C++ for instance) which will change the behaviour of an object when one of those overloaded operators is applied onto it. Here you get the stringified mail when you write "$mail" while otherwise you'd get the stringified reference: Mail::MboxParser::Mail=HASH(...).
AUTHOR AND COPYRIGHT
Tassilo von Parseval <Tassilo.Parseval@post.RWTH-Aachen.de>.
Copyright (c) 2001 Tassilo von Parseval. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.