#!/usr/bin/perl -w
use strict;
use Getopt::Std;
sub print_usage
{
print @_;
print STDERR "Usage $0
-r <recipient>[,<recipient>.... ]
-b <binary file to send>[,<binary file to send>....]
-f <text file to send>[,<text file to send>.....]
-s <subject>
-t <Message Text>
-i <Message Text as file>";
die "\n";
}
my %opts;
getopt 'rbfsti', \%opts;
defined $opts{'r'} or print_usage "No recipent(s) defined\n";
my $recips = join " ", (split /,/, $opts{'r'});
my $boundary = "Message-Boundary-$$";
my $uname = `whoami`;
chomp $uname;
my $fullname;
open IFH, "/etc/passwd" or print_usage "Can't open /etc/passwd\n";
foreach (<IFH>)
{
/^${uname}:[^:]+:[^:]+:[^:]+:([^:]+)/ and $fullname = $1, last;
}
close IFH;
defined $opts{'i'} and do
#{ -r $opts{'i'} or print_usage "Unable to open $opts{'i'}\n"; };
{
foreach ( split /,/, $opts{'i'} )
{ -r $_ or print_usage "Unable to read $_\n"; }
};
defined $opts{'f'} and do
{
foreach my $tfile ( split /,/, $opts{'f'} )
{ -r $tfile or print_usage "Unable to open $tfile\n"; }
};
defined $opts{'b'} and do
{
foreach my $tfile ( split /,/, $opts{'b'} )
{ -r $tfile or print_usage "Unable to open $tfile\n"; }
};
open OFH, "|/usr/sbin/sendmail $recips" or print_usage "Unable to open pipe to
sendmail\n";
print OFH "From: $fullname\n";
print OFH "To: $recips\n";
print OFH "Subject: ";
defined $opts{'s'} and print OFH "$opts{'s'}\n" or print OFH "\n";
print OFH "Content-Type:Multipart/Mixed; boundary=$boundary\n\n";
defined $opts{'t'}|| defined $opts{'i'} and do
{
print OFH "--$boundary\n";
print OFH "Content-type: text/plain; charset=US-ASCII\n";
print OFH "Content-transfer-encoding: 7BIT\n";
print OFH "Content-description: Read Me First\n\n\n";
defined $opts{'t'} and print OFH "$opts{'t'}\n";
defined $opts{'i'} and do
{
foreach my $ifile ( split /,/, $opts{'i'} )
{
open IFH, $ifile or print_usage "Unable to open ifile $ifile\n";
print OFH <IFH>;
close IFH;
}
};
};
defined $opts{'b'} and do
{
foreach my $file ( split /,/, $opts{'b'} )
{
my $shortname;
$file =~ /([^\/]+)$/ and $shortname = $1;
print OFH "--$boundary\n";
#print OFH "Content-type: Application/Octet-stream; name=$shortname;
print OFH "Content-type: binary; name=\"$shortname\"\n";
print OFH "Content-Disposition: attachment; filename=\"$shortname\"\n";
print OFH "Content-Transfer-Encoding: x-uuencode\n\n";
#print OFH "Content-Transfer-Encoding: 7bit\n\n";
print OFH `uuencode $file $file`;
}
};
defined $opts{'f'} and do
{
foreach my $file ( split /,/, $opts{'f'} )
{
my $shortname;
$file =~ /([^\/]+)$/ and $shortname = $1;
print OFH "--$boundary\n";
print OFH "Content-type: Application/Octet-stream; name=$shortname;
type=text\n";
#print OFH "Content-disposition: attachment; filename=$shortname\n\n";
open IFH, $file or print_usage "Unable to open $file\n";
foreach (<IFH>)
{
chomp;
print OFH "$_\r\n";
}
close IFH;
}
};
close OFH;