in reply to Re: Parse text file data to send a mail.
in thread Parse text file data to send a mail.

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.
  • Comment on Re^2: Parse text file data to send a mail.

Replies are listed 'Best First'.
Re^3: Parse text file data to send a mail.
by hippo (Archbishop) on Feb 04, 2019 at 11:09 UTC
    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.

Re^3: Parse text file data to send a mail.
by poj (Abbot) on Feb 04, 2019 at 11:21 UTC

    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