MIME::Tools::overview - an overview of the MIME-tools classes and features
use MIME::Parser;
### Create parser, and set some parsing options: my $parser = new MIME::Parser; $parser->output_under("$ENV{HOME}/mimemail");
### Parse input: $entity = $parser->parse(\*STDIN) or die "parse failed\n";
### Take a look at the top-level entity (and any parts it has): $entity->dump_skeleton;
Here's some code which composes and sends a MIME message containing three parts: a text file, an attached GIF, and some more text:
use MIME::Entity;
### Create the top-level, and set up the mail headers: $top = MIME::Entity->build(Type =>"multipart/mixed", From => "me\@myhost.com", To => "you\@yourhost.com", Subject => "Hello, nurse!");
### Part #1: a simple text document: $top->attach(Path=>"./testin/short.txt");
### Part #2: a GIF file: $top->attach(Path => "./docs/mime-sm.gif", Type => "image/gif", Encoding => "base64");
### Part #3: some literal text: $top->attach(Data=>$message);
### Send it: open MAIL, "| /usr/lib/sendmail -t -oi -oem" or die "open: $!"; $top->print(\*MAIL); close MAIL;
For more examples, look at the scripts in the examples directory of the MIME-tools distribution.
(START HERE) results() .-----------------. \ .-------->| MIME:: | .-----------. / | Parser::Results | | MIME:: |--' `-----------------' | Parser |--. .-----------------. `-----------' \ filer() | MIME:: | | parse() `-------->| Parser::Filer | | gives you `-----------------' | a... | output_path() | | determines | | path() of... | head() .--------. | | returns... | MIME:: | get() | V .-------->| Head | etc... | .--------./ `--------' | .---> | MIME:: | | `-----| Entity | .--------. | parts() `--------'\ | MIME:: | / returns `-------->| Body |<---------' sub-entities bodyhandle() `--------' (if any) returns... | open() | returns... | V .--------. read() | IO:: | getline() | Handle | print() `--------' etc...
To illustrate, parsing works this way:
A typical multipart message containing two parts --- a textual greeting and an ``attached'' GIF file --- would be a tree of MIME::Entity objects, each of which would have its own MIME::Head. Like this:
.--------. | MIME:: | Content-type: multipart/mixed | Entity | Subject: Happy Samhaine! `--------' | `----. parts | | .--------. |---| MIME:: | Content-type: text/plain; charset=us-ascii | | Entity | Content-transfer-encoding: 7bit | `--------' | .--------. |---| MIME:: | Content-type: image/gif | Entity | Content-transfer-encoding: base64 `--------' Content-disposition: inline; filename="hs.gif"
MIME::Tools->debugging($bool); ### turn all debugging on? MIME::Tools->quiet($bool); ### suppress warnings?
You then give that instance a readable filehandle on which waits a MIME message. If all goes well, you will get back a MIME::Entity object (a subclass of Mail::Internet), which consists of...
If the original message was a multipart document, the MIME::Entity object will have a non-empty list of ``parts'', each of which is in turn a MIME::Entity (which might also be a multipart entity, etc, etc...).
Internally, the parser (in MIME::Parser) asks for instances of MIME::Decoder whenever it needs to decode an encoded file. MIME::Decoder has a mapping from supported encodings (e.g., 'base64') to classes whose instances can decode them. You can add to this mapping to try out new/experiment encodings. You can also use MIME::Decoder by itself.
For multipart messages, you can start by creating a top-level "multipart" entity with MIME::Entity::build(), and then use the similar MIME::Entity::attach() method to attach parts to that message. Please note: what most people think of as ``a text message with an attached GIF file'' is really a multipart message with 2 parts: the first being the text message, and the second being the GIF file.
When building MIME a entity, you'll have to provide two very important pieces of information: the content type and the content transfer encoding. The type is usually easy, as it is directly determined by the file format; e.g., an HTML file is "text/html". The encoding, however, is trickier... for example, some HTML files are "7bit"-compliant, but others might have very long lines and would need to be sent "quoted-printable" for reliability.
See the section on encoding/decoding for more details, as well as ``A MIME PRIMER''.
$entity->smtpsend;
Encoding: | Normally used when message contents are: ------------------------------------------------------------------- 7bit | 7-bit data with under 1000 chars/line, or multipart. 8bit | 8-bit data with under 1000 chars/line. binary | 8-bit data with some long lines (or no line breaks). quoted-printable | Text files with some 8-bit chars (e.g., Latin-1 text). base64 | Binary files.
Which encoding you choose for a given document depends largely on (1) what you know about the document's contents (text vs binary), and (2) whether you need the resulting message to have a reliable encoding for 7-bit Internet email transport.
In general, only "quoted-printable" and "base64" guarantee reliable transport of all data; the other three ``no-encoding'' encodings simply pass the data through, and are only reliable if that data is 7bit ASCII with under 1000 characters per line, and has no conflicts with the multipart boundaries.
I've considered making it so that the content-type and encoding can be automatically inferred from the file's path, but that seems to be asking for trouble... or at least, for Mail::Cap...
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |