|
Sending attatchments is very easy....if you want to modify your own Perl
function, here is
a sample for TXT attatchments. What I'm sharing is a Perl program called mmail(1). run it with -h to get the help stuff. See SendMail() function for an example of writing attatchments. davidu wrote: I would recommend that if you have mutt installed you just use mutt from the command line, it is one for the few MUA's that can work from the command line without user intervention.read the mutt manpage.-davidu -- ======================================================================= Medi Montaseri, [EMAIL PROTECTED], 408-450-7114 Prepass Inc, IT/Operations, Software Eng. ======================================================================= |
#!/usr/bin/perl
# =========================================================
# Desc : This program mails a message to a group of recipients.
# A generic subject is provided if subject is not provided.
# If -f From is not specified, real-uid@host is assumed.
# If -m message is not sepcified, stdin is assume.
# Usage : $0 [-s "My Subject"] [-m MesgFile] [-f From]
# -r "List1 List2 ... Listn" | -e email1 email2 ... emailn"
#
# where MesgFile is an absolute path ot a file containing the message
# List-i is an abosolute path a to file containing email addresses
# and email-i is an email address, either -r or -e must be present
# Input : MsgFile1 MsgFile2...MsgFilen List1 List2 ... Listn
# Output: pipe to sendmail
# Version: 1.1
# Author: Medi, Montaseri, 00/12/14
# History: 01/01/19, added multiple message file feature
# =========================================================
# ------------------------ constructor ----------------------
BEGIN
{
use strict;
use vars qw ($opt_h $opt_s $opt_m $opt_f $opt_r $opt_e);
use Getopt::Std;
use Sys::Hostname;
}
# ------------------------ prototypes -------------------
sub PrintHelp();
sub GetSubject($);
sub GetFrom($);
sub GetMessages($$);
sub GetRecipients($$);
sub SendMail($$$$);
# ------------------------ Global Vars ------------------
my $Usage = "$0 [-h] [-s 'My Subject'] [-m 'MesgFile1 MsgFile2 ...MsgFilen']
[-f From] -r 'List1...Listn' | -e 'email1 email2 ... emailn'";
$|++;
# ======================= main() ======================
# Validate usage
die "$Usage \n" unless ( $#ARGV >= 0 );
die "$Usage" unless ( getopts('hs:m:f:r:e:') );
PrintHelp() if ( $opt_h );
die "$Usage" unless ( $opt_r || $opt_e );
my $Subject = GetSubject($opt_s);
my $From = GetFrom($opt_f) ;
my %Messages = ();
GetMessages($opt_m || '-' , \%Messages) ||
die "$0: Error: failed getting message [$!]";
my $Recipients = GetRecipients($opt_r, $opt_e) ||
die "$0: Error: failed getting recipients [$!]";
SendMail($From, $Subject, \%Messages, $Recipients);
# ----------------------- PrintHelp() ------------
sub PrintHelp()
{
my $Hostname = hostname() ;
my $Name = getpwuid( $< );
my $From = $Name . '@' . $Hostname;
print <<"EOH";
Usage:
$Usage
Where
-s 'My Subject' is an optional string, quoted if multi-token
-f from is the optional from line, else set to $From
-m msgfile is the optional mesg file or stdin if not provided
list-i is a file containing list of recipients
EOH
exit(1);
}
# ---------------------- GetSubject() ------------------
sub GetSubject($)
{
my $subject = shift;
return($subject) if ( $subject );
$subject = "Message-Of " . localtime();
return($subject);
}
# ---------------------- GetFrom() ------------------
sub GetFrom($)
{
my $from = shift;
return($from) if ( $from );
$from = getpwuid($<) . '@' . hostname();
return($from);
}
# ---------------------- GetMessages() --------------------
# Usage: GetMessages($MsgFile)
# Desc : This function takes a MesgFile and returns the content in a scalar
sub GetMessages($$)
{
my ($msgfile, $MsgsRef) = @_;
return(undef) unless ( $msgfile );
$/ = undef;
my ($OpenedMsg);
for my $m ( split(' ', $msgfile ) )
{
open(MSG, "$m") || do { warn("can not open $m [$!]"); next};
$OpenedMsg++;
$m =~ s/^-$/standard_input/;
$MsgsRef->{"$m"} = <MSG>; # slurp in the entire message
close(MSG);
}
$/ = "\n";
($OpenedMsg)? return(1): return(undef);
}
# ---------------------- GetRecipients() --------------------
sub GetRecipients($$)
{
return(undef) unless ( @_ ); # I better have some payload
my ($r, $e) = @_;
my (%Recipients);
if ( $r )
{
my $f;
my @files = split(/\s+/, $r);
foreach $f ( @files )
{
open(IN, "$f") || do
{
warn("$0: Error: opening [$f], proceeding $!");
next;
};
while(<IN>)
{
chop();
$Recipients{$_}++ ;
}
close(IN);
}
}
if ( $e )
{
my @emails = split(/\s+/, $e);
my $email;
foreach $email ( @emails )
{
$Recipients{"$email"}++;
}
}
return( join(',', sort (keys(%Recipients))) );
}
# ---------------------- SendMail() ----------------------
sub SendMail($$$$)
{
my ($From, $Subject, $MsgsRef, $Recipients) = @_;
my $Date = localtime();
open(OUT, "| /usr/sbin/sendmail -t -f $From" ) ||
die "failed instantiating sendmail $!";
print OUT <<EOT;
From: $From
To: $Recipients
Date: $Date
Subject: $Subject
Reply-To: $From
MIME-Version: 1.0
X-Mailer: mmail version 1.0
X-Accept-Language: en
Content-Type: multipart/mixed;
boundary="============$$"
This is a multi-part message in MIME format.
--============$$
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
========== This is a machine generated message. See Attatchment(s) =========
EOT
my ($filename, $filecontent);
my %tmp = %$MsgsRef;
while ( ($filename, $filecontent) = each ( %tmp ) )
{
print OUT <<EOT;
--============$$
Content-Type: text/plain; charset=us-ascii;
name="$filename"
Content-Transfer-Encoding: 7bit
filename="$filename"
$filecontent
EOT
}
print OUT <<EOT;
--============$$--
EOT
close(OUT);
if ( $@ )
{
warn("sendmail returned $@ $!");
}
}
# ----------------------------------------------------------
__END__
