工作上需要给客户发日志,老手动很不爽,写了这个脚本,又因为机器上sendmail进程被其他perl脚本占用,无奈下只能直接走perl的脚本发送邮件了。特此记录如有人需要借鉴,请自便。
主程序shell
#!/bin/bash
mail="xxx@xxxxx.com"
mailhead=`echo "$mail" |awk -F"@" '{print $1}'`
mailtail=`echo "$mail" |awk -F"@" '{print $2}'`
mindate="20130218"
maxdate="20130418"
#cd /mnt/log/ent_new
#cd /mnt/log/ent_old
#cd /mnt/log/global
#cd /mnt/log/oversea
cd /mnt/log/zmail
ls www_authlog.201*|awk -v a=$mindate -v b=$maxdate -F"." '$2>a&&$2<b'|xargs grep "$mailhead $mailtail"|awk -F":" '{print $2":"$3}' >>/getlog/log1.tmp
ls pop_authlog.201*|awk -v a=$mindate -v b=$maxdate -F"." '$2>a&&$2<b'|xargs grep "$mailhead $mailtail"|awk -F":" '{print $2":"$3":"$4}' >>/getlog/log1.tmp
/var/eyou/sbin/ev /getlog/log1.tmp >/getlog/log.txt
/usr/bin/unix2dos -o /getlog/log.txt
/usr/bin/zip /getlog/log.zip /getlog/log.txt
echo "$mail"|perl /getlog/sendmail.pl
/bin/rm -rf /getlog/log1.tmp
/bin/rm -rf /getlog/log.txt
/bin/rm -rf /getlog/log.zip
副程序pl
#!/usr/bin/perl
#use strict;
use POSIX;
use Encode;
use Mail::Sender;
open my $DEBUG,">> /getlog/mail.log" or die "Can't open the debug file: $!\n";
my $mail = <>;
my $sender = new Mail::Sender
{smtp => 'mail.xxxx.cn',
from => 'xxxx@xxxx.cn',
auth => 'LOGIN',
authid => 'xxxx@xxx.cn',
authpwd => 'password',
on_errors => undef
} or die "Can't create the Mail::Sender object: $Mail::Sender::Error\n";
$sender->MailFile(
{
to => "$mail",
cc => 'xxxx@xxx.cn',
subject =>"$mail mail log",
charset =>"gb2312",
msg => "$mail mail log",
file => "/getlog/log.zip",
debug => $DEBUG
}) or print $Mail::Sender::Error;
$sender ->Close()