perl mail

#!/opt/perl/5.8.0/bin/perl
#ident "%W%"


# Usage:   mail_files -t mailid [ -c mailid ] [ -s subject ] [ -f mailid ] [ -x content_type ]
#          [-n file_list] [-u file_list] [-b file_list] file_list
#
#    -f      : The mailid of the sender ( defaults to your userid )
#              Only userids that have been defined as "trusted" in the sendmail
#              config file can make use of the -f option. For non-trusted users
#              any value specified by this parameter will be ignored by
#              sendmail.
#    -t      : The mailid of the recipient. Mandatory, no default
#              multiple mailids can be specified, separated by commas.
#    -c      : The mailid of any carbon-copy recipients. Optional.
#              multiple mailids can be specified, separated by commas.
#    -s      : The subject string. Optional, default = "Not specified".
#              Enclose in quotes.
#    -n      : no-encode: indicates a list of files which are NOT to be base64
#              or uuencode encoded. Multiple files may be enclosed in double
#              quotes. Usual wildcard notation can be used. This option is
#              for completeness and can be omitted because the default action
#              is not to encode the file-list.
#    -b      : base64 encoding: indicates a list of files which are to be
#              base64 encoded. Multiple files may be enclosed in double quotes.
#              Usual wildcard notation can be used.
#    -u      : uuencode encoding: indicates a list of files which are to be
#              uuencode encoded. Multiple files may be enclosed in double
#              quotes. Usual wildcard notation can be used.
#    -x      : the main body content-type: "plain" (default) or "html"
#  file_list : The list of files to send as attachments with no-encoding
#              (same as -n option, but the file list does not need to be
#              enclosed in quotes if more than one file specified).
#              Usual wildcard notation can be used.

# The program will also prompt for text to be supplied on standard input
# as the main text of the message.

# The script makes use of perl's MIME package to perform the base-64
# encoding/decoding.

# Note that files destined for Windows environments should have a name of
# the form aaaa.bbb where aaaa is up to 8 characters long, and bbb is a
# 3 character sufix. The suffix determines which program is used to
# display/process the data at the remote end.

# Simple text files can be emailed unencoded. Binary files, or text files
# with long lines ( ie > 1000 chars ) should use the  base64 or uuencode
# encoding procedures. Base64 is preferred because it is more universally
# supported. In particular, most PC mail-clients can automatically decode
# base64 encoded attachments. Note that simple text files with short lines
# which are destined for PC environments should not be base64 encoded.
# This is because PCs use a different line-break character to Unix.
# If the text is base64 encoded, the line-breaks are not converted
# automatically and so the data arrives at the remote end without
# line-breaks.

# set up a 'usage' routine
# ------------------------

sub usage()
{
  print @_;
  print <<EOF;
  Usage:   mail_files -t mailid [ -c mailid ] [ -s subject ] [ -f mailid ] [ -x content_type ]
           [-n file_list] [-u file_list] [-b file_list] file_list
EOF
  exit 4;
}

# Initialise main variables ...
# -------------------------

use MIME::Base64 qw(encode_base64);
use Getopt::Std;
getopt("ftcsnbux");

$content_type = "plain";

if ($opt_x ne "") {
    $content_type = $opt_x;
}

if ($opt_t eq "") {
    &usage("An addressee must be specified");
}

# All remaining parameters are files not requiring encoding ...
# ---------------------------------------------------------

# Build up $FILES as the list of non-encoded files. Use sed to remove
# any leading space from the variable.


#if ($opt_b eq "" && $opt_u eq "" && $opt_n eq "") {
#    &usage("At least one file must be specified");
#}

# Remove leading commas from TO, CC  ...
# ---------------------------------

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

# Validate that the files exist ...
# -----------------------------

@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 $f does not exist / is not readable.\n";
      print "Exiting. ( Mail not sent ).\n";
      exit;
    }
}

# Get environment script is running from
$RTENV=$ENV{"WORLD"};
if ( "$RTENV" ne "" ) {
    $RTENV=~tr/[a-z]/[A-Z]/;
    $RTENV=" ($RTENV):";
}

print STDERR "Enter text of main message ( finish with CTRL-D ) ...";

# Now do the work ...
# ---------------

# The generated mail message is output onto standard out, which is then
# piped in to sendmail.


open(STDOUT, "| /usr/lib/sendmail -t") or open(STDOUT, "| /usr/sbin/sendmail -t") or die "Cannot find sendmail";

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"

This is a Mime message, which your mail program may not understand. Parts
of the message will appear as text. If the remainder appears as random
characters in the message body, instead of as attachments, then you'll
have to extract these parts and decode them manually.

--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

# Read the standard input as the main text of the message ...
# -------------------------------------------------------

while (<ARGV>) {
print;
}

print "\n";

# Now process the non-encrypted attachments ...
# -----------------------------------------

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);
}

# Now process the base64 encrypted attachments ...
# --------------------------------------------

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");
       binmode BFILE;
       local($/) = undef;
       print encode_base64(<BFILE>);
#       while (<BFILE>) {
#        print encode_base64($_);
#       }
       close(BFILE);
}

# Now process the uuencode encrypted attachments ...
# ----------------------------------------------

# Sorry, this bit is untested - I haven't got a mail-client which can
# handle uuencoded MIME messages automatically, so can't test if the
# 'Content-Transfer-Encoding: uuencode' line is correct and whether I
# need the uuencode "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 < $f xxx`
}

# append the final boundary line ...

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

#) | /usr/lib/sendmail -t



猜你喜欢

转载自zgcsy1986.iteye.com/blog/1012974