使用搜索模式对文本文件进行Perl隔离,并自动通过电子邮件发送附件

时间:2015-03-07 08:27:25

标签: perl email

我有一个文本文件。我使用一个小的搜索模式算法隔离了这个,我得到了预期的输出

chomp(@ARGV);

if (@ARGV != 2)
 {

    print "Please Pass two parameters\n";
    print "Usage: \n\t $0 <Filename> <pattern>\n"; 
    exit;
}

$File_name     = $ARGV[0];
$res_File_name = $File_name . "\.result\.txt";
$Pattern       = $ARGV[1]; chomp($Pattern);

open(FD,"$File_name")      or die("File $File_name could not be opened\n");
open(WFD,">$res_File_name") or die("File $res_File_name could not be opened\n");

while(<FD>) 
{

    print WFD $_ if(/$Pattern/);
}

close(FD);
close(WFD);

但是当搜索完成后,我只提取文本文件中的那些模式并将其放在我的本地驱动器中。相反,我需要将该文本文件附加到电子邮件中,并需要将其发送。为此,我使用Office 365,它显示电子邮件已成功发送,但我没有收到任何附件的电子邮件

使用MIME :: Lite;

use Net::SMTP;
 use Mail::Sendmail1;
  use Net::SMTP::TLS; 
 #use strict;
if (@ARGV != 2)
 {

    print "Please Pass two parameters\n";
    print "Usage: \n\t $0 <Filename> <pattern>\n"; 
    exit;
}


$File_name     = $ARGV[0];
$res_File_name = $File_name . "\.result\.txt";
$Pattern       = $ARGV[1]; chomp($Pattern);

open(FD,"$File_name")      or die("File $File_name could not be opened\n");
open(WFD,">$res_File_name") or die("File $res_File_name could not be opened\n");

while(<FD>) 
{
 print WFD $_ if(/$Pattern/);






}
$to = 'xx@xx.com';
#$cc = 'xx@xx.com';
$from = 'xx@xx.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
open (txt.result.txt, '<', $res_File_name) or die "Failed to open $log_file: $!";
 @log_contents = <txt.result.txt>;
close txt.result.txt;
push @body, @log_contents;
 #open MAIL, '|C:\Perl\site\lib\Mail\Sendmail1 -t' or die "Failed to send mail: $!";
$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 #Cc       => $cc,
                 Subject  => $subject,
                 Type     => 'multipart/mixed'
                 );
                 #print MAIL "To: ${to}\n";
#print MAIL "From: ${from}\n";
#print MAIL "Subject: ${subject}\n\n";

# email body
#print MAIL @body;



# Add your text message.
$msg->attach(Type         => 'result.txt',
             Data         => $message
            ); 
            #$msg->send;
            #attach=( "Agent.txt.result.txt" )
    close(FD);
               close(WFD);      

    #close MAIL;
print "Email sent successfully.\n";     

exit;


#print "Email Sent Successfully\n";





    Help me on how i can achieve this. 

1 个答案:

答案 0 :(得分:0)

我认为你有一个拼写错误:use Mail::Sendmail1应该是Mail::Sendmail(没有“1”)。但使用MIME::Lite不需要Mail::Sendmail

我建议您使用其他模块发送邮件,例如Email::Simple