rahu_6697 has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to PERL. I want to create a PERL Script that could send mail by taking all required fields from an input file present at desktop and can send a mail to everyone in mailing list (separated by comma) accordingly. Contents of my input data file is as follows:

Recipients are enclosed between tags TO & ENDTO.

Subject of mail is enclosed between SUBJECT & ENDSUBJECT.

Body of mail is enclosed between BODY & ENDBODY.

Sample data file :

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

SUBJECT
Weekly status snapshot for WW-5
ENDSUBJECT

BODY
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
ENDBODY


Till now I'm using a code that takes inputs from two different files ("message.txt" & "recipients.txt") and sends mail accordingly. But now I want to parse data from a single file separated by tag fields for recipients, body and subject.
$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);

Replies are listed 'Best First'.
Re: Parse text file data to send a mail.
by hippo (Archbishop) on Feb 04, 2019 at 10:08 UTC

    Since you already have one method of sending the mail, that's not pertinent to changing the input so I'll omit it here. This is one example of how to parse the sort of input you have. It's a bit verbose and is far from the only way to do it.

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my %parts; my $key; while (my $line = <DATA>) { if ($line =~ /^([A-Z]+)$/) { $key = $1; if ($key =~ /^END/) { undef $key; } next; } next unless defined $key; $parts{$key} //= ''; $parts{$key} .= $line; } print Dumper (\%parts); __DATA__ TO rahul@example.com,you@everyone.com ENDTO SUBJECT Weekly status snapshot for WW-5 ENDSUBJECT BODY Hi All, Weekly progress snapshot for this week will be taken on Thursday, 30 J +an at the end of the day. ( Please update your status before the snap +shot) 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 ENDBODY

    Data::Dumper is only used here as an easy way to display the hash once constructed. You won't need that in your final program.

      This code is not working fine can you please check it at your end. Send a working code with actual variable names as I got confused what to change in your code. Finally I need all data fields (subject,body etc.) into separate variables to pass it to send a mail.
        Finally I need all data fields (subject,body etc.) into separate variables to pass it to send a mail.

        That isn't necessary. You can use the values directly from the hash. eg:

        print MAIL "Subject: $parts{SUBJECT}\n\n";

        See Not Exactly a Hash Tutorial for more on how to use hashes.

        As hippo said use the values in hash directly.

        #!/usr/bin/perl use strict; use warnings; my $mailprog = "/usr/sbin/sendmail"; my $file = 'mailinglist.txt'; open my $fh, '<', $file or die "Could not open $file : $!"; my %mail = ( 'FROM' => 'rahul.agarwal@everyone.com', ); my $key; while (my $line = <$fh>) { if ($line =~ /^([A-Z]+)$/) { $key = $1; if ($key =~ /^END/) { undef $key; } next; } next unless defined $key; $mail{$key} //= ''; $mail{$key} .= $line; } close $fh; $mail{$_} =~ s/^\s+|\s+$//g for keys %mail; # trim #open (MAIL, "|$mailprog -t"); #print MAIL << "EOM"; print << "EOM"; To: $mail{'TO'} From: $mail{'FROM'} Subject: $mail{'SUBJECT'} $mail{'BODY'} EOM #close(MAIL);
        poj
Re: Parse text file data to send a mail.
by choroba (Cardinal) on Feb 04, 2019 at 11:57 UTC
    Crossposted to StackOverflow. It's considered polite to inform about crossposting to avoid duplicate work of people not attending all the involved sites.
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Parse text file data to send a mail.
by Veltro (Hermit) on Feb 04, 2019 at 11:24 UTC
Re: Parse text file data to send a mail.
by karlgoethebier (Abbot) on Feb 04, 2019 at 21:40 UTC

    Be fearless. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help