解析文本文件数据以在Perl中发送邮件

时间:2019-02-04 11:36:13

标签: perl

我是PERL的新手。我想创建一个PERL脚本,该脚本可以通过从桌面上存在的输入文件中获取所有必填字段来发送邮件,并可以相应地将邮件发送给邮件列表中的每个人(用逗号分隔)。我的输入数据文件的内容如下:

  • 收件人位于标签TO和ENDTO之间。
  • 邮件主题放在SUBJECT和ENDSUBJECT之间。
  • 邮件正文包含在BODY和ENDBODY之间。

样本数据文件:


rahul @ example.com,you @ everyone.com
ENDTO

主题
WW-5的每周状态快照
ENDSUBJECT

身体
大家好, 本周的每周进度快照将在1月30日星期四结束时进行。 (请在快照前更新您的状态)
使用工作周编号作为5进行本周的更新。
注意:如果您希望从工作表中提取(或删除)任何其他数据,请与我一起工作,以便可以更新状态整理脚本来执行此操作。
谢谢,
-Ram
ENDBODY

  • 到目前为止,我使用的代码从两个不同的文件(“ message.txt”和“ recipients.txt”)中获取输入,并相应地发送邮件。但是现在我想解析单个文件中的数据,这些文件由收件人,正文和主题的标签字段分隔。
    $mailprog = "/usr/sbin/sendmail";

    my $file1 = 'message';
    open my $ifh1, '<', $file1;
    local $/ = undef;
    my $contents = $ifh1;

    my $file2 = 'recipients';
    open my $ifh2, '<', $file2;
    local $/ = undef;
    my $recip = $ifh2; 

    close( $ifh2 );
    close( $ifh1 );

    $subject= "Weekly status snapshot for WW-3";
    open(MAIL, "|$mailprog -t");
    print MAIL "To: $recip \n";
    print MAIL "bcc: rahul.agarwal\@everyone.com\n";
    print MAIL "From: rahul.agarwal\@everyone.com\n";
    print MAIL "Subject: $subject\n\n";
    print MAIL "$contents";
    close(MAIL); 



1 个答案:

答案 0 :(得分:1)

怎么样?

实现方法的要点:将..双稳态运算符与定义开始和结束条件的正则表达式一起使用。

#!/usr/bin/perl
use strict;
use warnings;

# Generate regexes for section keywords
my @sections = qw(TO SUBJECT BODY);
my %keywords = map {
    (
     "$_"    => qr/^$_$/,
     "END$_" => qr/^END$_$/
    )} @sections;

my($to, $subject, @body);
while (<STDIN>) {
    chomp;
    if      (/$keywords{TO}/../$keywords{ENDTO}/) {
        # skip keyword lines
        next if /$keywords{TO}|$keywords{ENDTO}/;
        $to = $_;
    } elsif  (/$keywords{SUBJECT}/../$keywords{ENDSUBJECT}/) {
        # skip keyword lines
        next if /$keywords{SUBJECT}|$keywords{ENDSUBJECT}/;
        $subject = $_;
    } elsif  (/$keywords{BODY}/../$keywords{ENDBODY}/) {
        if      (/$keywords{BODY}/) {
            # skip keyword line
        } elsif (/$keywords{ENDBODY}/) {
            # mail is done... send it
            print "To: ${to}\n";
            print "Bcc: rahul.agarwal\@everyone.com\n";
            print "From: rahul.agarwal\@everyone.com\n";
            print "Subject: ${subject}\n";
            print "\n";
            print "$_\n" foreach (@body);
        } else {
            # body text line
            push(@body, $_);
        }
    }
}

exit 0;

输出您的测试示例:

# add "| /usr/sbin/sendmail -t" after command to actually send it...
$ perl dummy.pl <dummy.txt
To: rahul@example.com,you@everyone.com
Bcc: rahul.agarwal@everyone.com
From: rahul.agarwal@everyone.com
Subject: Weekly status snapshot for WW-5

Hi All, Weekly progress snapshot for this week will be taken on Thursday, 30 Jan at the end of the day. ( Please update your status before the snapshot)
Use work week number as 5 for this week's updates .
Note : If you want any additional data to be picked up ( or dropped ) from your sheets, do work with me so that status collation script can be updated to do this .
Thanks ,
-Ram