一个用perl写的发邮件的脚本

#!/usr/bin/perl
=pod
#send email common script
#Angelo - 2008/10/06
ttachment]
#EmailServer - emailserver.
#port - listening port, default is 25
uard.com.set "" if no to-sendee
uard.com.set "" if no cc-sendee
#Subject   - subject of your email,string.set "" if no subject.
r.
#Attachment- full path of attachement,seperated by ",".

# Revision History
# Will Yi - 2011/12/30 - Added a command line argument to specify the debug level for send()
=cut
use MIME::Lite;
use strict;
#my $reportserver = 'ex01bd.bdc.wgti.net';
#my $port = '25';
#my $fromaddress  = 'local.test';
#my $reporttoaddress = '[email protected]';

if ((scalar @ARGV) < 5 || (scalar @ARGV) > 9) {
    &usage;
    exit -1;
}
my $reportserver = $ARGV[0];
my $port = $ARGV[1];
my $fromaddress  = $ARGV[2];
my $reporttoaddress = $ARGV[3];
my $encode_type = $ARGV[4];
if ((scalar @ARGV) < 5 || (scalar @ARGV) > 9) {
    &usage;
    exit -1;
if ((scalar @ARGV) < 5 || (scalar @ARGV) > 9) {
    &usage;
    exit -1;
my $subject = 'automation_test';
my $body = 'automation test';
my $attachment = '';
my @attachlist;
if ($ARGV[5]) {
    $subject = $ARGV[5];
if ($ARGV[5]) {
    $subject = $ARGV[5];
}
if ($ARGV[5]) {
    $subject = $ARGV[5];
}
    $body = $ARGV[6];
if ($ARGV[6]) {
    $body = $ARGV[6];
if ($ARGV[6]) {
    $body = $ARGV[6];
}
if ($ARGV[7]) {
    $attachment = $ARGV[7];
if ($ARGV[7]) {
    $attachment = $ARGV[7];
if ($ARGV[7]) {
    $attachment = $ARGV[7];
}
if ($ARGV[8]) {
    $debug_level = $ARGV[8]; # 0-disable; 1-verbose
if ($ARGV[8]) {
    $debug_level = $ARGV[8]; # 0-disable; 1-verbose
}
if ($ARGV[8]) {
    $debug_level = $ARGV[8]; # 0-disable; 1-verbose

if (!($attachment eq '')) {
    @attachlist = split(',',$attachment);
    foreach my $eachattach (@attachlist) {
        -e $eachattach or die "$eachattach doesn't exists.\n";
    }
}
}
my $msg = MIME::Lite->new(
    From     => $fromaddress,
    To       => $reporttoaddress,
    Subject  => $subject,
    Type     => 'multipart/mixed',
    Encoding => $encode_type,
);
);
$msg->attach(
    #Type      => 'AUTO',
    Data      => $body,
$msg->attach(
    #Type      => 'AUTO',
    Data      => $body,
);
$msg->attach(
    #Type      => 'AUTO',
    Data      => $body,
);
if (!($attachment eq '')) {
    foreach my $eachattach (@attachlist) {
        $msg->attach(
        Path        => $eachattach,
        Type            => 'AUTO',
        Disposition => 'attachment'
        );
    }
}
}
$msg->send('smtp',$reportserver, Debug=>$debug_level, Port => $port);

.\n";
    print "EmailServer\t- emailserver address.\n";
d.com,yyy\@watchguard.com.set \"\" if no to-sendee.\n";
    print "EncodeType\t- should be 7bit/8bit/binary\n";
st one char.\n";
t, default is 1\n";
    return 0;

 

使用方法如下:automation@net1PC1:~$ perl send_common_email.pl ?
Usage: send_common_email.pl <EmailServer> <port> <FromAddress> <ToAddress> <encode_type> [Subject] [Body] [Attachment] [Debug Level].
eg. send_common_email.pl ex01bd.bdc.wgti.net [email protected],[email protected],[email protected].
EmailServer     - emailserver address.
FromAddress     - "From" address
ToAddress       - "to" address,seperated by "," if you have muti-sendee,eg: [email protected],[email protected] "" if no to-sendee.
EncodeType      - should be 7bit/8bit/binary
Subject         - subject of your email,string.set "" if no subject.
Body            - context of your email,it can be a string or a exist-file.can not be "".at least one char.
Attachment      - full path of attachement,seperated by ",".
DebugLevel      - Set the debug level when sending mail. Value 1-verbose,0-disable. If not set, default is 1

本文出自 “但行好事莫问前程” 博客,转载请与作者联系!

你可能感兴趣的:(脚本,perl,smtp)