in reply to Parse text file data to send a mail.

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.

Replies are listed 'Best First'.
Re^2: Parse text file data to send a mail.
by rahu_6697 (Novice) on Feb 04, 2019 at 10:50 UTC
    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