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.comn"; }
close $fh; 

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

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

Your balance is:12345

至,jack@gmail.com

Your balance is:67890

至,john@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;
}

期待帮助。提前谢谢。

尚未进行测试,但这应该可以解决您的问题

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;

最新更新