MIME:: |
MIME::Lite - low-calorie MIME generator
WARNING: This is Alpha code. I have not yet fully tested it, and I can't guarantee that the interface won't change in the next few releases in a non-backwards-compatible manner. It is being provided to the community for suggestions and in the hopes that it will be useful.
use MIME::Lite;
Create a single-part message:
# Create a new single-part message, to send a GIF file: $msg = new MIME::Lite -From =>'me@myhost.com', -To =>'you@yourhost.com', Cc =>'some@other.com, some@more.com', Subject =>'Helloooooo, nurse!', Type =>'image/gif', Path =>'hellonurse.gif';
Create a multipart message (i.e., one with attachments):
# Create a new multipart message: $msg = new MIME::Lite -From =>'me@myhost.com', -To =>'you@yourhost.com', -Cc =>'some@other.com, some@more.com', -Subject =>'A message with 2 parts...', Type =>'multipart/mixed';
# Add parts (each "attach" has same arguments as "new"): attach $msg Type =>'TEXT', Data =>"Here's the GIF file you wanted"; attach $msg Type =>'image/gif', Path =>'aaa000123.gif', Filename =>'logo.gif';
Output a message:
# As a string... $str = $msg->stringify;
# To a filehandle (say, a "sendmail" stream...) $msg->print(\*SENDMAIL);
In the never-ending quest for great taste with fewer calories, we proudly present: MIME::Lite.
MIME::Lite is intended as a simple, standalone module for generating (not parsing!) MIME messages... specifically, it allows you to output a simple, decent single- or multi-part message with text or binary attachments. It does not require that you have the Mail:: or MIME:: modules installed.
You can specify each message part as either the literal data itself (in a scalar or array), or as a string which can be given to open() to get a readable filehandle (e.g., "<filename" or "somecommand|").
If you need more sophisticated behavior, please get the MIME-tools package instead. I will be more likely to add stuff to that toolkit over this one.
Create a multipart message exactly as above, but using the "attach to singlepart" hack:
# Create a new multipart message: $msg = new MIME::Lite -From =>'me@myhost.com', -To =>'you@yourhost.com', Cc =>'some@other.com, some@more.com', Subject =>'A message with 2 parts...', Type =>'TEXT', Data =>"Here's the GIF file you wanted";
# Attach a part: attach $msg Type =>'image/gif', Path =>'aaa000123.gif', Filename =>'logo.gif';
Output a message to a filehandle:
# Write it to a filehandle: $msg->print(\*STDOUT);
# Write just the header: $msg->print_header(\*STDOUT);
# Write just the encoded body: $msg->print_body(\*STDOUT);
Get a message as a string:
# Get it as a string: $str = $msg->stringify;
# Get just the header: $str = $msg->stringify_header;
# Get just the encoded body: $str = $msg->stringify_body;
# Send a message (Unix systems only!):
# Send it! $msg->send;
If any arguments are given, they are passed into build(); otherwise, just the empty object is created.
You can attach a MIME::Lite OBJECT, or have it create one by specifying a PARAMHASH that will be automatically given to new().
One of the possibly-quite-useful hacks thrown into this is the "attach-to-singlepart" hack: if you attempt to attach a part (let's call it "part 1") to a message that isn't a multipart message (the "self" object in this case), the following happens:
One of the nice side-effects is that you can create a text message and then add zero or more attachments to it; it will be output much as a user agent like Netscape would output the message.
Content-type
unless you know what you're doing!
"inline"
or "attachment"
.
The default is "inline"
.
"binary"
.
"TEXT" means "text/plain" "BINARY" means "application/octet-stream"
The default is "TEXT"
.
A picture being worth 1000 words (which is of course 2000 bytes, so it's probably more of an "icon" than a "picture", but I digress...), here are some examples:
$msg = build MIME::Lite -From => 'yelling@inter.com', -To => 'stocking@fish.net', -Subject => "Hi there!", Type => 'TEXT', Encoding => '7bit', Data => "Just a quick note to say hi!";
$msg = build MIME::Lite -From => 'dorothy@emerald-city.oz', -To => 'gesundheit@edu.edu.edu', -Subject => "A gif for U" Type => 'image/gif', Path => "/home/httpd/logo.gif";
$msg = build MIME::Lite -From => 'laughing@all.of.us', -To => 'scarlett@fiddle.dee.de', -Subject => "A gzipp'ed tar file", Type => 'x-gzip', Path => "gzip < /usr/inc/somefile.tar |", ReadNow => 1, File => "somefile.tgz";
To show you what's really going on, that last example could also have been written:
$msg = new MIME::Lite;
$msg->build(Type => 'x-gzip', Path => "gzip < /usr/inc/somefile.tar |", ReadNow => 1, File => "somefile.tgz");
$msg->add(From => "laughing@all.of.us"); $msg->add(To => "scarlett@fiddle.dee.de"); $msg->add(Subject => "A gzipp'ed tar file");
Normally, you will use this to add non-MIME tags:
$msg->add("Subject" => "Hi there!");
Giving VALUE an arrayref will cause all those values to be added:
$msg->add("Received" => ["here", "there", "everywhere"]
Note: add() is probably going to be more efficient than replace(), so you're better off using it for most applications.
Note: the name comes from Mail::Header.
$msg->attr("content-type" => "text/html"); $msg->attr("content-type.charset" => "US-ASCII"); $msg->attr("content-type.name" => "homepage.html");
This would cause the final output to look something like this:
Content-type: text/html; charset=US-ASCII; name="homepage.html"
Note that the special empty sub-field tag indicates the anonymous first sub-field.
Giving VALUE as undefined will cause the contents of the named subfield to be deleted.
Supplying no VALUE argument just returns the attribute's value:
$type = $msg->attr("content-type"); # returns "text/html" $name = $msg->attr("content-type.name"); # returns "homepage.html"
$msg->delete("Subject");
Note: the name comes from Mail::Header.
[TAG, VALUE]
pairs.
Any fields that the user has explicitly set will override the corresponding MIME fields that we would generate. So: don't say:
$msg->set("Content-type" => "text/html; charset=US-ASCII");
unless you mean it!
Note: I called this "fields" because the header() method of Mail::Header returns something different, but similar enough to be confusing.
With no argument, returns the filename as dictated by the content-disposition.
$msg->get_length;
Returns the length, or undefined if not set.
Note: the content length can be difficult to compute, since it involves assembling the entire encoded body and taking the length of it (which, in the case of multipart messages, means freezing all the sub-parts, etc.).
This method only sets the content length to a defined value if the
message is a singlepart with "binary"
encoding, and the body is
available either in-core or as a simple file. Otherwise, the content
length is set to the undefined value.
Since content-length is not a standard MIME field anyway (that's right, kids: it's not in the MIME RFCs, it's an HTTP thing), this seems pretty fair.
$msg->replace("Subject" => "Hi there!");
Giving VALUE as undefined will simply cause the contents of the named field to be deleted. Giving VALUE as an arrayref will cause all the values in the array to be added.
Note: the name comes from Mail::Header.
The default behavior is that any content type other than
text/*
or message/*
is binmode'd; this should in general work fine.
With a defined argument, this method sets an explicit "override" value. An undefined argument unsets the override. The new current value is returned.
Warning: setting the data causes the "content-length" attribute to be recomputed (possibly to nothing).
Warning: setting the path recomputes any existing "content-length" field, and re-sets the "filename" (to the last element of the path if it looks like a simple path, and to nothing if not).
Note that the in-core data will always be used if available.
Be aware that everything is slurped into a giant scalar: you may not want to use this if sending tar files! The benefit of not reading in the data is that very large files can be handled by this module if left on disk until the message is output via print() or print_body().
If no arguments are given, the default is:
Path => "$ENV{HOME}/.signature"
The content-length is recomputed.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or any object that responds to a print() message.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or any object that responds to a print() message.
Fatal exception raised if unable to open any of the input files, or if a part contains no data, or if an unsupported encoding is encountered.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or any object that responds to a print() message.
Note: uses print_body() internally to print to a scalar.
Right now, this is done by piping it into the "sendmail" command as given by sendmail(). It probably will only work on Unix systems.
Returns false if sendmail seems to have failed, true otherwise. Fatal exception raised if the open fails.
sendmail MIME::Lite "/usr/lib/sendmail", "-t", "-oi", "-oem";
What you see above is the default.
This is "lite", after all...
I thought putting in a sendmail invocation wasn't too bad an idea, since a lot of Perlers are on UNIX systems. The default arguments to sendmail (which you can change) are:
-t Scan message for To:, Cc:, Bcc:, etc.
-oi Do NOT treat a single "." on a line as a message terminator. As in, "-oi vey, it truncated my message... why?!"
-oem On error, mail back the message (I assume to the appropriate address, given in the header). When mail returns, circle is complete. Jai guru deva -oem.
This class treats a MIME header in the most abstract sense, as being a collection of high-level attributes. The actual RFC-822-style header fields are not constructed until it's time to actually print the darn thing.
The "Type" parameter of build() is a content type.
This is the actual type of data you are sending.
Generally this is a string of the form "majortype/minortype"
.
Here are the major MIME types. A more-comprehensive listing may be found in RFC-2046.
application/octet-stream
, application/gzip
, application/postscript
...
audio/basic
...
image/gif
, image/jpeg
...
message/rfc822
...
multipart/mixed
, multipart/alternative
...
text/plain
, text/html
...
video/mpeg
...
The "Encoding" parameter of build(). This is how the message body is packaged up for safe transit.
Here are the 5 major MIME encodings. A more-comprehensive listing may be found in RFC-2045.
The most liberal, and the least likely to get through mail gateways. Use sparingly.
Copyright (c) 1997 by Eryq. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This software comes with NO WARRANTY of any kind. See the COPYING file in the distribution for details.
For some reason, the US FDA says that this is now required by law on any products that bear the name "Lite"...
Serving size: 1 module Servings per container: 1 Calories: 0 Fat: 0g Saturated Fat: 0g
Warning: for consumption by hardware only! May produce indigestion in humans if taken internally.
Eryq, (who really should be wrapping holiday presents instead). eryq@enteract.com / http://enteract.com/~eryq.
Created: 11 December 1996. Ho ho ho.