#!/usr/bin/perl -w
my
%TypeFor
=
qw(
txt text/plain
sh text/x-sh
csh text/x-csh
pm text/x-perl
pl text/x-perl
jpg image/jpeg
jpeg image/jpeg
gif image/gif
gif image/gif
tif image/tiff
tiff image/tiff
xbm image/xbm
)
;
@ARGV
or
die
<<EOF;
Usage:
mimesend [-t to] [-s subj] (-f file [-m type])+
-n Don't actually send it; just print it to stdout.
-t to The "to" address.
-s subj The subject of the message.
-f file Path to attached file.
-m type MIME type of most recent attached file.
EOF
sub
type_for {
my
$path
=
shift
;
my
(
$ext
) = (
$path
=~ /\.([a-z0-9]+)\Z/i);
(
$ext
and
$TypeFor
{
lc
(
$ext
)}) or
(-T
$path
?
'text/plain'
:
'application/octet-stream'
);
}
my
$nosend
;
my
@files
;
ARG:
while
(
@ARGV
) {
$_
=
shift
@ARGV
;
/^-n/ and
do
{
$nosend
= 1;
next
ARG;
};
/^-t(.*)/ and
do
{
$to
= $1 ||
shift
@ARGV
;
next
ARG;
};
/^-s(.*)/ and
do
{
$subj
= $1 ||
shift
@ARGV
;
next
ARG;
};
/^-f(.*)/ and
do
{
push
@files
, [$1||
shift
@ARGV
];
next
ARG;
};
/^-m(.*)/ and
do
{
$files
[-1][1] = $1 ||
shift
@ARGV
;
next
ARG;
};
die
"$0: bad usage: <$_>.\n"
;
}
$to
or
die
"$0: missing [-t to]\n"
;
$subj
or
die
"$0: missing [-s subject]\n"
;
my
$top
;
if
(!
@files
) {
die
"$0: no files specified!\n"
;
}
elsif
(
@files
== 1) {
my
(
$path
,
$type
) = @{
$files
[0]};
$top
= build MIME::Entity
Type
=> (
$type
|| type_for(
$path
)),
Path
=>
$path
,
Encoding
=>
'-SUGGEST'
;
$top
->head->add(
'To'
,
$to
);
$top
->head->add(
'Subject'
,
$subj
);
}
else
{
$top
= build MIME::Entity
Type
=>
"multipart/mixed"
;
$top
->head->add(
'To'
,
$to
);
$top
->head->add(
'Subject'
,
$subj
);
foreach
(
@files
) {
my
(
$path
,
$type
) = @{
$_
};
$top
->attach(
Type
=> (
$type
|| type_for(
$path
)),
Path
=>
$path
,
Encoding
=>
'-SUGGEST'
);
}
}
if
(!
$nosend
) {
open
SENDMAIL,
"|/usr/lib/sendmail -t -oi -oem"
or
die
"$0: open sendmail: $!\n"
;
$top
->
print
(\
*SENDMAIL
);
close
SENDMAIL;
die
"sendmail failed"
if
($? >> 255);
}
else
{
$top
->
print
(\
*STDOUT
);
}
1;