perl 发送邮件 可以发送附件

#cat send_mail.pl

#!/usr/bin/perl -w
#Author:duhui
#Date :2010-11-29

#perl -MCPAN -e shell
#cpan>install Net::SMTP_auth
use Net::SMTP;
use Net::SMTP_auth;
use MIME::Base64;
$mail_server = 'smtp.163.com';
$mail_from = 'dh03@163.com';
$mail_to = 'duhui@goso.cn';
$uname='XXX';
$passwd='XXX';
#开启Debug模式
$smtp = Net::SMTP->new("$mail_server" , Debug => 1);
#普通发送模式
#$smtp = Net::SMTP->new("$mail_server" );
$smtp->auth("$uname","$passwd");
$smtp->mail("$mail_from");
$smtp->to("$mail_to");
$smtp->data();
$smtp->datasend("from:$mail_from");
$smtp->datasend("To:$mail_to");
$smtp->datasend("Reply-To:$mail_from");
$smtp->datasend("Return-Path:$mail_from");
$smtp->datasend('Subject:Your Subject');
$smtp->datasend('Content-Type:text/plain; ChartSet=gb2312');
$smtp->datasend('Your mail content!');
$smtp->datasend('/n');
$smtp->quit;




另外一种实现方式 可以发送附件的

#*****************************************************************************#
#  Function Name:
#       SendMail($$/@$$/@)
#  Description:
#       Use Net::SMTP and MIME::Lite to send Email with attachment.
#  Argument:
#       1: Sending Email address
#       2: Password of sending Email.
#       3: The recipient Email array
#       4: The subject of the Email
#       5: The content of the Email
#       6: The attachments array of the Email
#  Return:
#       None
#*****************************************************************************#
sub SendMail($$/@$$/@)
{
use Net::SMTP;
use MIME::Lite;

my ($mailFrom, $password, $mailToRef, $subject, $content, $attachmentRef)=@_;

my ($userName,$mailHost) = split(//@/, $mailFrom);
my $helloPara = $mailHost;
# print "The Email user name is: $userName/n";
# print "The mailHost is: $mailHost/n";
print $mailHost;
if ('163.com.cn' ne $mailHost)
{
     $mailHost = "smtp.".$mailHost;
#     print "The mailHost is: $mailHost/n";
}            #The smtp host

my @mailTo = @$mailToRef;   #The recipient list
for(my $i=0; $i<=$#mailTo; $i++) {
     print "$#mailTo/n";
     print "$mailTo[$i]/n";   }

my @attachment = @$attachmentRef;    #The attachments

my $smtp = Net::SMTP->new($mailHost, Hello => $helloPara, Timeout => 120, Debug => 1)
        ||die 'Cannot connect to server /'$mailHost/'';

# anth login, type your user name and password here
  $smtp->auth($userName,$password)||print "Auth Error!/n";

  foreach my $mailTo (@mailTo)
  {
    # Create a new multipart message:
    my $msg = MIME::Lite->new(
        From    => $mailFrom,
        To      => $mailTo,
        Subject => $subject,
        Type    =>'multipart/mixed',
        )or print "Error creating MIME body: $!/n";

    # Add parts:
    $msg->attach(Type     =>'TEXT',
                 Data    => $content,
                );
    foreach my $attachment (@attachment)
    {
        $msg->attach(
          Type     => 'AUTO',      # the attachment mime type
          Path     => $attachment, # local address of the attachment
          )or print "Error attaching test file: $!/n";
    }

    my $str = $msg->as_string() or print "Convert the message as a string: $!/n";

        # Send the From and Recipient for the mail servers that require it
        $smtp->mail($mailFrom);
        $smtp->to($mailTo);

        # Start the mail
        $smtp->data();

        # Send the message
        $smtp->datasend("$str");

        # Send the termination string
        $smtp->dataend();
}
$smtp->quit;
return;
}
my $drvTestReport="/data/test";
our $mailFrom = 'dh03@163.com';       #Send email address
our $password = 'XXX';
my @mailTo = ('duhui@goso.cn');  #The recipient list
my $mailSubject = "Driver Test Result";
my $mailContent = "The attachment is the $mailSubject!/n
Please check it./nThanks!/n/n      Chenny.chen/n";
my @mailAttachment = ($drvTestReport);    #The attachments for the test result
SendMail($mailFrom, $password, @mailTo, $mailSubject, $mailContent, @mailAttachment);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值