perl 发送邮件

用到的库:Net::SMTP;这个可以在cpan上找得到。

另外在windows下安装perl的库可以使用ppm方法。

在command line里面输入ppm会出现Perl Package Manager。

找到你所需要安装的库选择“File”->"Run task"就可以了。

 

看个发送邮件的简单例子:

sub SendEmail($$){
   my $emailfrom="";
   my ($to,$day) = @_;
   my $mailhost='**.**';#邮件服务器的地址
   my $subject = "主题";
   my $text = "发送的内容“;
   my $smtp = Net::SMTP->new($mailhost,Timeout=>120,Debug=>0);
   $smtp->mail($emailfrom);
   $smtp->to($to);#收件人
   $smtp->data();
   $smtp->datasend("To:$to/n");
   #$smtp->datasend("From: $mailfrom/n");
   $smtp->datasend("Subject: $subject/n");
   #$smtp->datasend("/n");
   # Send the message
   $smtp->datasend("$text/n/n");
   #Send the termination string
   $smtp->dataend();
   $smtp->quit;
}

 

调用该方法的时候可能会出现 can not call the methond mail() at line **

出现这个的原因有可能是mail server 无法ping 通。

 

另附perl追加文件的方法:

 

open File,'+>>',$filename;#以追加的方式打开文件
print File "$content","/n";#往文件中写内容
 close File;

 

perl解析xml文件:

用到的库:use XML::Simple;

 

看个例子:
   # read XML file
   print "begin to parse xml configure files..../n";
   my $xml = new XML::Simple;
   my $data;
   if (-e "config.xml")
   {
    $data = $xml->XMLin("config.xml");
    }else{
     die "could not find config.xml file! please validate it exists/n";
    }
   $username = $data->{usrname};
   $password = $data->{pwd};

使用XMLin()读取xml文件然后直接访问每一个element即可。

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...