Perl脚本发送电子邮件阅读文件内容

时间:2016-07-18 15:18:10

标签: perl

写了一个Perl脚本从2个数组中提取内容并将其存储到以下格式的文件(out.log)

open my $fh, ">", "out.log" or die "Cannot open out.log: $!";
for my $i (0..$#arr_1){
    print $fh "$arr_1[$i]\t$arr_2[$i]\@gmail.com\n"; }
close $fh; 


12345 joe@gmail.com
67890 jack@gmail.com
45678 john@gmail.com

现在通过阅读out.log文件内容,我必须通过电子邮件正文内容发送电子邮件到joe@gmail.com

Your balance is:12345

to,jack @ gmail.com

Your balance is:67890

要,约翰@ gmail.com

Your balance is:45678

我可以阅读日志文件并形成如下所示的邮件正文内容,但不确定如何实现上述方案。

my $mail_body = "Your balance is:\n";
{
  local $/ = undef;
  open FILE, "file" or die "...: !$";
  $mail_body .= <FILE>;
  close FILE;
}

期待寻求帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

尚未测试,但这可以解决您的问题。

use strict;
use warnings;

use Mail::Sendmail;

open my $fh, '<', 'output.log' or die "could not open file output.log !$";

while (my $line = <$fh>) { # read the content of the file line by line
      # match and capture balance and the email address
      my ($balance, $to_email) = $line =~ /(\d+)\s+(\S+)/;
      %mail = ( To      => $to_email,
                From    => 'your_eamil@address.comacine',
                Messag => "your balance is: $balance"
              );
      sendmail(%mail);
}
close $fh;