#!/usr/bin/perl -w

# Simple script to send the contents of stdin as an
# application/octet-stream MIME block. Parameters are "-s Subject",
# "-n Suggested Name For File" (defaults to "no-name"), and as many
# recipients as you like.
#
# Uses sendmail to do the actual send, so you gotta have that
# configured.
use strict;
use MIME::Entity;
use Getopt::Long;
use File::Temp;

my $subject = "";
my $name = "no-name";
my @recipients;
my $dump = 0;

if ( !GetOptions( "s=s" => \$subject, "n=s" => \$name, "dump!" => \$dump )) {
  die "Bad Args";
}

die "no recipients specified" unless @ARGV;

while( @ARGV ) {
  push @recipients, shift @ARGV;
}

my $msg = MIME::Entity->build( To => $recipients[0],
                               Subject => $subject,
                               Data => [ "This is your message.\n" ] );

# Get the data, which we will for now presume to be a binary block
my $data = "";
{
  local $/ = undef;
  $data = <>;
}

$msg->attach( Data => $data,
              Type => "application/octet-stream",
              Filename => $name );

if ( !$dump ) {
    $msg->send( 'sendmail' ) || die "Mail send failed ($!)";
} else {
    $msg->print( \*STDOUT );
}

