MIME::Tools is module for parsing MIME entities.
For this tutorial, you will need to have the MIME::Tools module installed on your computer.
(MIME is short for Multipurpose Internet Mail Extensions. Read more about MIME at http://en.wikipedia.org/wiki/MIME.)
MIME::Tools depends on modules not included in all Perl distribution:
IO::Stringy
MIME::Base64
Mail::Internet, Mail::Header, Mail::Field... from the MailTools distribution.
To check which modules are installed and which aren't, try perldoc perllocal.
Download any missing module from CPAN and install it.
For a complete specification about MIME::Tools, check out the online documentation included in the package:
I used version 5.411.
HTML documentation is included in this version, in the docs sub directory.
Also check out the Perl source code in the examples sub directory.
MIME::Tools is an extensive module distribution.
The following example is just one way to use it.
It sends a mail, similar as the example in the Mail::Send tutorial, but uses attachments.
Create a Perl script called mime-send.pl containing the following code:
#!/usr/bin/perl
usestrict;
uselibqw(/home/redanti/perl);
useMIME::Parser;
useMail::Send;
# Create MIME entity
####################my$num_attachments = 5;
my$entity = MIME::Entity->build(
From => '[email protected]',
To => '[email protected]',
Cc => '[email protected]',
Bcc => '[email protected]',
Subject => 'A MIME message',Data => "This message contains $num_attachments attachments.",
'X-Mailer' => 'My X-Mailer',
);
# Build attachment list
for (my$i = 1; $i <= $num_attachments; $i++) {
$entity->attach(Type => 'text/plain',
Data => "Attachment number $i");
}
# Send it:
my$sender = new Mail::Send;
foreach ($entity->head->tags) { # give the sender our headers
$sender->set($_, map {chomp $_; $_} $entity->head->get($_));
}
my$fh = $sender->open('sendmail');
$entity->print_body($fh);
$fh->close;
and send your mail:
# perl -w mime-send.pl
Check your mail to verify that the mail was sent correctly.
If you want to use Perl to check your mail, read the Mail::POP3Client tutorial.
Last modified: Thu Aug 23 13:07:54 Romance Daylight Time 2007