perl mail

#!/opt/perl/5.8.0/bin/perl

#ident"%W%"

#Usage:mail_files-tmailid[-cmailid][-ssubject][-fmailid][-xcontent_type]

#[-nfile_list][-ufile_list][-bfile_list]file_list

#

#-f:Themailidofthesender(defaultstoyouruserid)

#Onlyuseridsthathavebeendefinedas"trusted"inthesendmail

#configfilecanmakeuseofthe-foption.Fornon-trustedusers

#anyvaluespecifiedbythisparameterwillbeignoredby

#sendmail.

#-t:Themailidoftherecipient.Mandatory,nodefault

#multiplemailidscanbespecified,separatedbycommas.

#-c:Themailidofanycarbon-copyrecipients.Optional.

#multiplemailidscanbespecified,separatedbycommas.

#-s:Thesubjectstring.Optional,default="Notspecified".

#Encloseinquotes.

#-n:no-encode:indicatesalistoffileswhichareNOTtobebase64

#oruuencodeencoded.Multiplefilesmaybeenclosedindouble

#quotes.Usualwildcardnotationcanbeused.Thisoptionis

#forcompletenessandcanbeomittedbecausethedefaultaction

#isnottoencodethefile-list.

#-b:base64encoding:indicatesalistoffileswhicharetobe

#base64encoded.Multiplefilesmaybeenclosedindoublequotes.

#Usualwildcardnotationcanbeused.

#-u:uuencodeencoding:indicatesalistoffileswhicharetobe

#uuencodeencoded.Multiplefilesmaybeenclosedindouble

#quotes.Usualwildcardnotationcanbeused.

#-x:themainbodycontent-type:"plain"(default)or"html"

#file_list:Thelistoffilestosendasattachmentswithno-encoding

#(sameas-noption,butthefilelistdoesnotneedtobe

#enclosedinquotesifmorethanonefilespecified).

#Usualwildcardnotationcanbeused.

#Theprogramwillalsopromptfortexttobesuppliedonstandardinput

#asthemaintextofthemessage.

#Thescriptmakesuseofperl'sMIMEpackagetoperformthebase-64

#encoding/decoding.

#NotethatfilesdestinedforWindowsenvironmentsshouldhaveanameof

#theformaaaa.bbbwhereaaaaisupto8characterslong,andbbbisa

#3charactersufix.Thesuffixdetermineswhichprogramisusedto

#display/processthedataattheremoteend.

#Simpletextfilescanbeemailedunencoded.Binaryfiles,ortextfiles

#withlonglines(ie>1000chars)shouldusethebase64oruuencode

#encodingprocedures.Base64ispreferredbecauseitismoreuniversally

#supported.Inparticular,mostPCmail-clientscanautomaticallydecode

#base64encodedattachments.Notethatsimpletextfileswithshortlines

#whicharedestinedforPCenvironmentsshouldnotbebase64encoded.

#ThisisbecausePCsuseadifferentline-breakcharactertoUnix.

#Ifthetextisbase64encoded,theline-breaksarenotconverted

#automaticallyandsothedataarrivesattheremoteendwithout

#line-breaks.

#setupa'usage'routine

#------------------------

subusage()

{

print@_;

print<<EOF;

Usage:mail_files-tmailid[-cmailid][-ssubject][-fmailid][-xcontent_type]

[-nfile_list][-ufile_list][-bfile_list]file_list

EOF

exit4;

}

#Initialisemainvariables...

#-------------------------

useMIME::Base64qw(encode_base64);

useGetopt::Std;

getopt("ftcsnbux");

$content_type="plain";

if($opt_xne""){

$content_type=$opt_x;

}

if($opt_teq""){

&usage("Anaddresseemustbespecified");

}

#Allremainingparametersarefilesnotrequiringencoding...

#---------------------------------------------------------

#Buildup$FILESasthelistofnon-encodedfiles.Usesedtoremove

#anyleadingspacefromthevariable.

#if($opt_beq""&&$opt_ueq""&&$opt_neq""){

#&usage("Atleastonefilemustbespecified");

#}

#RemoveleadingcommasfromTO,CC...

#---------------------------------

$opt_t=~s/^\.//;

$opt_c=~s/^\.//;

#Validatethatthefilesexist...

#-----------------------------

@txt_list=split/,/,$opt_n;

@uue_list=split/,/,$opt_u;

@b64_list=split/,/,$opt_b;

foreach$f(@txt_list,@uue_list,@b64_list){

if(!-r$f){

print"Error:File$fdoesnotexist/isnotreadable.\n";

print"Exiting.(Mailnotsent).\n";

exit;

}

}

#Getenvironmentscriptisrunningfrom

$RTENV=$ENV{"WORLD"};

if("$RTENV"ne""){

$RTENV=~tr/[a-z]/[A-Z]/;

$RTENV="($RTENV):";

}

printSTDERR"Entertextofmainmessage(finishwithCTRL-D)...";

#Nowdothework...

#---------------

#Thegeneratedmailmessageisoutputontostandardout,whichisthen

#pipedintosendmail.

open(STDOUT,"|/usr/lib/sendmail-t")oropen(STDOUT,"|/usr/sbin/sendmail-t")ordie"Cannotfindsendmail";

print<<EOF;

From:$opt_f

Subject:$RTENV$opt_s

To:$opt_t

EOF

if(defined($opt_c)){

print"Cc:$opt_c\n";

}

print<<EOF;

Mime-Version:1.0

Content-Type:multipart/mixed;boundary="DMW.Boundary.605592468"

ThisisaMimemessage,whichyourmailprogrammaynotunderstand.Parts

ofthemessagewillappearastext.Iftheremainderappearsasrandom

charactersinthemessagebody,insteadofasattachments,thenyou'll

havetoextractthesepartsanddecodethemmanually.

--DMW.Boundary.605592468

Content-Type:text/$content_type;charset=US-ASCII

EOF

#Content-Type:text/plain;name="message.txt";charset=US-ASCII

#Content-Disposition:inline;filename="message.txt"

#Content-Transfer-Encoding:7bit

#Readthestandardinputasthemaintextofthemessage...

#-------------------------------------------------------

while(<ARGV>){

print;

}

print"\n";

#Nowprocessthenon-encryptedattachments...

#-----------------------------------------

foreach$f(@txt_list){

$BASE=$f;

$BASE=~s/^.*\///;

print<<EOF;

--DMW.Boundary.605592468

Content-Type:application/octet-stream;name="$BASE"

Content-Disposition:attachment;filename="$BASE"

Content-Transfer-Encoding:7bit

EOF

open(BFILE,"<$f");

print<BFILE>;

close(BFILE);

}

#Nowprocessthebase64encryptedattachments...

#--------------------------------------------

foreach$f(@b64_list){

$BASE=$f;

$BASE=~s/.*\///;

print<<EOF;

--DMW.Boundary.605592468

Content-Type:application/octet-stream;name="$BASE"

Content-Disposition:attachment;filename="$BASE"

Content-Transfer-Encoding:base64

EOF

open(BFILE,"<$f");

binmodeBFILE;

local($/)=undef;

printencode_base64(<BFILE>);

#while(<BFILE>){

#printencode_base64($_);

#}

close(BFILE);

}

#Nowprocesstheuuencodeencryptedattachments...

#----------------------------------------------

#Sorry,thisbitisuntested-Ihaven'tgotamail-clientwhichcan

#handleuuencodedMIMEmessagesautomatically,socan'ttestifthe

#'Content-Transfer-Encoding:uuencode'lineiscorrectandwhetherI

#needtheuuencode"begin"and"end"lines.

foreach$f(@uue_list){

$BASE=$f;

$BASE=~s/.*\///;

print<<EOF;

--DMW.Boundary.605592468

Content-Type:application/octet-stream;name="$BASE"

Content-Disposition:attachment;filename="$BASE"

Content-Transfer-Encoding:uuencode

EOF

`uuencode<$fxxx`

}

#appendthefinalboundaryline...

print"--DMW.Boundary.605592468--\n"

#)|/usr/lib/sendmail-t

相关推荐