📄 sendmail.pm
字号:
## This is the name of the current module.#package SendMail;#===============================================================================## Constructor: # $obj = new SendMail;# $obj = new SendMail($smtpserver);# $obj = new SendMail($smtpserver, $smtpport);## Methods:# $obj->Attach($filename, [\$data]);# $obj->Bcc($bccemailadd1, [$bccemailadd2, ...]);# $obj->Cc($ccemailadd1, [$ccemailadd2, ...]);# $obj->ErrorsTo($errorstoadd1, [$errorstoadd2, ...]);# $obj->From($sender);# $obj->Inline($filename, [\$data]);# $obj->OFF;# $obj->ON;# $obj->ReplyTo($replytoadd1, [$replytoadd2, ...]);# $obj->Subject($subject);# $obj->To($recipient1, [$recipient2, ...]);# $obj->attach(\%hash);# $obj->createMailData();# $obj->getEmailAddress($emailaddstr);# $obj->getRcptLists();# $obj->isMailReady();# $obj->receiveFromServer(\*SOCKET);# $obj->reset();# $obj->sendMail();# $obj->sendToServer(\*SOCKET, $message);# $obj->setDebug($obj->ON);# $obj->setError($errormessage);# $obj->setMailBody($mailbody);# $obj->setMailHeader($mailheader, $mailheadervalue);# $obj->setSMTPPort($smtpport);# $obj->setSMTPServer($smtpserver);# $obj->version;## *p/s: For more details, please refer to the description below.##===============================================================================## We are using Socket.pm to connect to the SMTP port.#use Socket;## We are using MIME::Base64 and MIME::QuotedPrint to encode MIME data.#use MIME::Base64;use MIME::QuotedPrint;use Exporter;use strict;use vars qw($_LOCALHOST $VERSION $_MAILER @ISA @EXPORT @EXPORT_OK $_ERR);use vars qw($_DEFAULT_SMTP_PORT);@EXPORT = qw();@EXPORT_OK = qw();$VERSION = "2.03";$_MAILER = "Perl SendMail Module $VERSION";$_DEFAULT_SMTP_PORT = 25;## Some of the SMTP server needs to say "HELO domain.address".#eval { require Sys::Hostname; Sys::Hostname::import('hostname'); $_LOCALHOST = hostname();};$_LOCALHOST = $_MAILER if $@;#===============================================================================## CONSTRUCTOR: $obj = new SendMail;# $obj = new SendMail($smtpserver);# $obj = new SendMail($smtpserver, $smtpport);## DESCRIPTION: This is the constructor of the SendMail object.##===============================================================================sub new { my($pkg) = shift; my($smtpserver) = shift; my($smtpport) = shift; my($self) = {}; bless $self, $pkg; # # The mail server. # $self->{'smtpserver'} = ($smtpserver && $smtpserver =~ /^\s*$/) ? "localhost" : $smtpserver; # # The port number for smtp. # $self->{'smtpport'} = ($smtpport && $smtpport =~ /^\d+$/) ? $smtpport : $_DEFAULT_SMTP_PORT; # # The default debug mode is "OFF". # $self->{'debugmode'} = $self->OFF; # # Set the default mailer. # $self->setMailHeader("X-MAILER", $_MAILER); # # Create empty attachment array. # $self->{'attachmentArr'} = []; return $self;}#===============================================================================## METHOD: $obj->Attach($filename, [\$data]);## DESCRIPTION: This method will attach file to the mail. If the data has been# specified, will use the filename and the data, instead of # reading from the file.##===============================================================================sub Attach ($;$) { my($self) = shift; my($filename) = shift; my($dataRef) = shift; my(%hash, $dump); return $self->setError("No attachment has been specified.") if $filename =~ /^\s*$/; if ($filename =~ /(\\|\/)/) { ($hash{'filename'}) = $filename =~ /^.*[\\\/]([^\\\/]+)$/; } else { $hash{'filename'} = $filename; } $hash{'filepath'} = $filename; $hash{'dataref'} = $dataRef if (ref($dataRef) eq "SCALAR" || ref($dataRef) eq "GLOB"); $hash{'attachtype'} = "attachment"; return $self->attach(\%hash); return 0;}#===============================================================================## METHOD: $obj->Bcc($bccemailadd1, [$bccemailadd2, ...]);## DESCRIPTION: Add a list of the name/email address to the blind carbon copy # list.##===============================================================================sub Bcc ($) { my($self) = shift; my(@bcc) = @_; my($currEmail) = undef; for $currEmail (@bcc) { push(@{$self->{'mailheaders'}->{'BCC'}}, $currEmail) if ($self->getEmailAddress($currEmail) !~ /^\s*$/); } return 0;}#===============================================================================## METHOD: $obj->Cc($ccemailadd1, [$ccemailadd2, ...]);## DESCRIPTION: Add a list of the name/email address to the carbon copy list.##===============================================================================sub Cc ($) { my($self) = shift; my(@cc) = @_; my($currEmail) = undef; for $currEmail (@cc) { push(@{$self->{'mailheaders'}->{'CC'}}, $currEmail) if ($self->getEmailAddress($currEmail) !~ /^\s*$/); } return 0;}#===============================================================================## METHOD: $obj->ErrorsTo($errorstoadd1, [$errorstoadd2, ...]);## DESCRIPTION: Add a list of the name/email address into the "Errors-To" list.##===============================================================================sub ErrorsTo ($) { my($self) = shift; my(@errorsto) = @_; my($currEmail) = undef; for $currEmail (@errorsto) { push(@{$self->{'mailheaders'}->{'ERRORS-TO'}}, $currEmail) if ($self->getEmailAddress($currEmail) !~ /^\s*$/); } return 0;}#===============================================================================## METHOD: $obj->From($sender);## DESCRIPTION: Set the sender of the email.##===============================================================================sub From ($) { my($self) = shift; my($from) = shift; $self->{'mailheaders'}->{'FROM'} = $from; return 0;}#===============================================================================## METHOD: $obj->Inline($filename, [\$data]);## DESCRIPTION: This method will attach file to the mail. If the data has been# specified, will use the filename and the data, instead of # reading from the file.##===============================================================================sub Inline ($;$) { my($self) = shift; my($filename) = shift; my($dataRef) = shift; my(%hash, $dump); return $self->setError("No attachment has been specified.") if $filename =~ /^\s*$/; if ($filename =~ /(\\|\/)/) { ($hash{'filename'}) = $filename =~ /^.*[\\\/]([^\\\/]+)$/; } else { $hash{'filename'} = $filename; } $hash{'filepath'} = $filename; $hash{'dataref'} = $dataRef if (ref($dataRef) eq "SCALAR" || ref($dataRef) eq "GLOB"); $hash{'attachtype'} = "inline"; return $self->attach(\%hash); return 0;}#===============================================================================## METHOD: $obj->OFF;## DESCRIPTION: Will return 0. Basically, it is used to set the debug mode OFF.# Eg. $obj->setDebug($obj->OFF);##===============================================================================sub OFF () { return 0;}#===============================================================================## METHOD: $obj->ON;## DESCRIPTION: Will return 1. Basically, it is used to set the debug mode ON.# Eg. $obj->setDebug($obj->ON);##===============================================================================sub ON () { return 1;}#===============================================================================## METHOD: $obj->ReplyTo($replytoadd1, [$replytoadd2, ...]);## DESCRIPTION: Add a list of the name/email address into the "Reply-To" list.##===============================================================================sub ReplyTo ($;@) { my($self) = shift; my(@replyto) = @_; push(@{$self->{'mailheaders'}->{'REPLY-TO'}}, @replyto); return 0;}#===============================================================================## METHOD: $obj->Subject($subject);## DESCRIPTION: Set the subject of the email.##===============================================================================sub Subject ($) { $_[0]->{'mailheaders'}->{'SUBJECT'} = $_[1]; return 0;}#===============================================================================## METHOD: $obj->To($recipient1, [$recipient2, ...]);## DESCRIPTION: Add a list of the name/email address to the recipient list.##===============================================================================sub To ($;@) { my($self) = shift; my(@to) = @_; for (@to) { my($currEmail) = $_; push(@{$self->{'mailheaders'}->{'TO'}}, $currEmail) if ($self->getEmailAddress($currEmail) !~ /^\s*$/); } return 0;}#===============================================================================## METHOD: $obj->attach(\%hash);## DESCRIPTION: This method will attach file to the mail. If the data has been# specified, will use the filename and the data, instead of # reading from the file.##===============================================================================sub attach ($) { my($self) = shift; my($dataRef) = shift; return $self->setError("No attachment has been specified.") if $dataRef->{'filename'} =~ /^\s*$/; push(@{$self->{'attachmentArr'}}, $dataRef); return 0;}#===============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -