Doodad of the Day

gnat on 2002-05-13T22:40:27

This uses MIME::Lite to send the named files as attachments. Customize by adding your email address and (optionally) specifying an SMTP server to use (running sendmail locally is the default). Run with no arguments (or read the code) to see the possible arguments.

#!/usr/bin/perl -w

use MIME::Lite;
use Getopt::Std;

$FROM = 'me@example.com'; ### CHANGE THIS DEFAULT
# MIME::Lite->send('smtp', "localhost:1025", Timeout=>60);
### Uncomment that line and adjust to send via specified SMTP server

getopts('t:s:', \%o);

$o{t} ||= $FROM;
$o{s} ||= 'Your binary file, sir';

unless (@ARGV) {
    die "usage:\n\t$0 [-s subject] [-t to] file ...\n";
}

$msg = new MIME::Lite(
    From => $FROM,
    To   => $o{t},
    Subject => $o{s},
    Data => "Hi",
    Type => "multipart/mixed",
);
while (@ARGV) {
  $msg->attach('Type' => 'application/octet-stream',
               'Encoding' => 'base64',
               'Path' => shift @ARGV);
}

$msg->send();
--Nat


Did just the same thing today

Thomas on 2002-05-13T23:52:08

This is kind of funny.. I did something where I had to attach a few files to a mail and MIME::Lite was perfect for that task. But nice program indeed. Only thing I'm wondering wheter will work is tilde expansion?

Re:Did just the same thing today

Matts on 2002-05-14T09:32:58

tilde expansion should happen in the shell before perl sees your command line arguments.

Re:Did just the same thing today

gnat on 2002-05-14T15:21:55

Matt's right. If you're on Windows, add this after the call to getopts:
@ARGV = map {glob} @ARGV;
That expands wildcards (dunno what it does for ~, though!) on Windows.

--Nat
(I thank perlmonks every time I use that!)