用Perl发送邮件

#!/usr/bin/perl -w

package Mailiter;
use vars qw(@ISA @EXPORT @EXPORT_OK);
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
	smtpserver from to cc subject mailbody 
	attach unattach unattachall 
	send
);

use MIME::Lite;
use Common;

sub new
{
	my $self = shift;
    my ($subject, $tolist, @messages) = @_;
    $self->{'subject'} = $subject;
    $self->to($tolist);
	$self->{'mailbody'} = {'Type' => 'text/plain', 'Data' => qq(join('\n',@messages))};
    bless $self;
}

sub smtpserver
{
	my $self = shift;
	my ($smtpserver) = @_;
	$self->{'mailserver'} = $smtpserver;
}

sub from
{
	my ($self, $mailfrom) = @_;
	$self->{'from'} = $mailfrom;
}

sub to
{
	my ($self, $tolist) = @_;
	$self->{'tolist'} = join(',', split(/[:,;|\s]+/, $tolist));
}

sub cc
{
	my ($self, $cclist) = @_;
	$self->{'cclist'} = join(',', split(/[:,;|\s]+/, $cclist));
}

sub subject
{
	my ($self, $subject) = @_;
	$self->{'subject'} = $subject;
}
sub mailbody
{
	my ($self, $type, $mailbody) = @_;
	$self->{'mailbody'} = {'Type' => $type, 'Data' => $mailbody};
}

sub attach
{
	my ($self, $type, $path, $name) = @_;
	$self->{'attachcs'} = () if not $self->{'attachcs'};
	push(@{$self->{'attachcs'}}, 
		{ 'Type' => $type, 'Path' => $path, 'Filename' => $name, 'Disposition' => 'attachment'}
		);
}

sub unattach
{
	my ($self, $type, $path, $name) = @_;
	$self->{'attachcs'} = () if not $self->{'attachcs'};
	my @attached = ();
	my @unattached = ();
	foreach (@{$self->{'attachcs'}}){
		if(($_->{'Type'} eq $type) and ($_->{'Path'} eq $path) and ($_->{'Filename'} eq $name)){
			push(@unattached, $_);
		}
		else{
			push(@attached, $_);
		}
	}
	$self->{'attachcs'} = @attached;
	return @unattached;
}

sub unattachall
{
	my ($self, $type, $path, $name) = @_;
	$self->{'attachcs'} = ();
}

sub send
{
	my $self = shift;
	if(&isScmDebug()){
		print("mail server: ".$self->{'mailserver'}."\n");
		print("mail from: ".$self->{'from'}."\n");
		print("to list: ".$self->{'tolist'}."\n");
		print("cc list: ".$self->{'cclist'}."\n");
		print("subject: ".$self->{'subject'}."\n");
	}
	
	if(&isNotBlank($self->{'mailserver'})){
		### Add parts (each "attach" has same arguments as "new"):
		my $msg = MIME::Lite->new(
			From    => $self->{'from'},
			To      => $self->{'tolist'},
			Cc      => $self->{'cclist'},
			Subject => $self->{'subject'},
			Type    =>'multipart/mixed'
		);

		### Add parts (each "attach" has same arguments as "new"):
		$msg->attach(
			Type => $self->{'mailbody'}->{'Type'},
			Data => $self->{'mailbody'}->{'Data'});
		foreach (@{$self->{'attachcs'}}){
			$msg->attach(
				Type => $_->{'Type'},
				Path => $_->{'Path'},
				Filename => $_->{'Filename'},
				Disposition => $_->{'Disposition'}
			);
		}
		### use Net:SMTP to do the sending
		$msg->send('smtp', $self->{'mailserver'}, Debug=>0);
	}
	else{ #send email with an MTA like sendmail, cannot send attach with this option
		open(MAIL,'|$SENDMAIL -t') || die "Can't start sendmail.: $!";
		print MAIL<<"END_OF_HEADER";
To: @{$self->{'tolist'}}
Cc: @{$self->{'cclist'}}
From: $self->{'from'}
Subject: $subject
END_OF_HEADER
		print MAIL "Content-Type: $self->{'mailbody'}->{'Type'}\n";
		print MAIL "\n";
		print MAIL "$self->{'mailbody'}->{'Data'}\n";
		print MAIL "\n";
		close(MAIL);
	}
}

1;
 

 

你可能感兴趣的:(perl)