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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值