分类: PERL
2012-12-14 16:12:40
#!/bin/env perl use DBI; use POSIX; use Net::SMTP_auth; use MIME::Base64; use strict; my ($date,$wday,$Dtime,$Day,$dbh,$sth,$sth_E4,$smtp,%hash,@list)=(localtime)[3,6]; if($wday =~ /0/){ exit; }elsif($date =~ /1/){ $Dtime = (strftime ("%Y-%m",localtime(time-86400)))."-01"; $Day = strftime ("%Y-%m-%d",localtime); }else{ $Dtime = (strftime ("%Y-%m",localtime))."-01"; $Day = strftime ("%Y-%m-%d",localtime); } $dbh = DBI->connect("DBI:mysql:host=192.168.11.1","root","123456") || die "Could not connect to database: $DBI::errstr"; my @database=('Site','Site4.0'); foreach(@database){ $sth = $dbh->prepare(qq{select date_format(create_time,'%Y-%m-%d'),visit_count from `$_`.tbl_site_record_by_daily where create_time>=? and create_time order by create_time}); $sth->execute($Dtime,$Day); while(my ($add_data,$value) = $sth->fetchrow_array()){ my $result = sprintf("%.2f",$value/10000)."万"; if(exists $hash{$add_data}){ $hash{$add_data} .= "\t$result\n"; }else{ $hash{$add_data} .= "$result"; } } } $sth->finish(); $dbh->disconnect(); foreach(reverse sort keys %hash){ push @list,"$_tt$hash{$_}"; } $smtp = Net::SMTP_auth->new('mail.b.com', Timeout => 30, Hello => 'mail.b.com', Debug => 0); $smtp->auth('LOGIN','pv@b.com','123456'); $smtp->mail('pv@b.com '); $smtp->to('a@b.com','b@b.com','c@b.com'); $smtp->data(); $smtp->datasend("From: =?utf-8?B?".encode_base64("访问量统计",'')."?=\n"); $smtp->datasend("To: a@b.com\n"); $smtp->datasend("Cc: b@b.com;c@b.com\n"); $smtp->datasend("Content-Type:text/plain;ChartSet=GB2312;"); $smtp->datasend("Content-Transfer-Encoding:base64\n"); $smtp->datasend("Mime-Version: 1.0\n"); $smtp->datasend("Subject: $Day 访问量统计\n"); $smtp->datasend("\n"); $smtp->datasend("访问量统计($Dtime ~ $Day)\n"); $smtp->datasend("\t\t\tsite 3.0\tsite 4.0\n"); $smtp->datasend(@list); $smtp->datasend("\n"); $smtp->dataend(); $smtp->quit;