Re: newbie pipe stdin to sendmail
On 02/11/2016 19:35, Andy Bach wrote: On Wed, Nov 2, 2016 at 9:13 AM, Mark Coetser> wrote: $parser->ignore_errors(1); $parser->output_to_core(1); my $entity = $parser->parse(\*STDIN); my $error = ($@ || $parser->last_error); #get email headers ... my ($header, $body); { local $/ = ""; $header = ; undef $/; $body = ; } Seems like you've already consumed STDIN in the parser, so there's nothing left in your local block to copy. My guess is you want to print $entity to MAIL. Note, you should always use a conditional on open stmts, often open(my $mh, "|$sendmail $vac) or die "can't exec $sendmail: $!"; Using a lexical, instead of a NAME is modern perl, you still do print $mh "header: ... maybe (perldoc MIME::Parser): ### Congratulations: you now have a (possibly multipart) MIME entity! $entity->dump_skeleton; # for debugging Again thanks for the help, to simply things how can I read stdin into a variable that I could parse twice. I tried to simplfy things as follows but get the same result.. #!/usr/bin/perl my $vacation_forward = "vacation\@domain.com"; open(OUT, "|/usr/sbin/sendmail $vacation_forward") or die ("Can't sendmail - $!"); while (my $line = ) { $email_addr_to = $1 if $line =~ /^To: (.*)$/; print OUT; } close (OUT); if ($email_addr_to) { print "$email_addr_to\n"; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
newbie pipe stdin to sendmail
Hi OK, complete newbie to perl. I am busy playing around/learning and I wanted to create a simple perl script that I can pipe an email too and then grab the To header and output that and then pipe the unmodified email to sendmail with a different recipient... similar to cat /var/mail/mailfile | sendmail t...@test.com This is what I have so far, I can print the To address fine but for some reason I am just receiving a blank email as if STDIN isnt outputting anything? There are a couple of hashed out variations I have tried already #!/usr/bin/perl use strict; use warnings; use MIME::Parser; my $parser = new MIME::Parser; my $to; #contains the message to header my $sendmail = "/usr/sbin/sendmail"; $parser->ignore_errors(1); $parser->output_to_core(1); my $entity = $parser->parse(\*STDIN); my $error = ($@ || $parser->last_error); #get email headers my $header = $entity->head; $to = $header->get('To'); chomp($to); print "To $to\n"; my $vacation_forward = "myem...@mydomain.com"; print "vacation_forward $vacation_forward\n"; #while () { #chomp; #system( "$_ | $sendmail $vacation_forward" ); #} #my $msgbody = ; #`echo "$msgbody" | $sendmail $vacation_forward`; #open MAIL, "|/usr/sbin/sendmail $sendmail $vacation_forward"; #print MAIL "$msgbody"; #close(MAIL); #open OUT, "|$sendmail $sendmail $vacation_forward"; #open(OUT, "|/usr/sbin/sendmail $vacation_forward") or die ("Can't sendmail - $!"); #open OUT, "|-", $sendmail, $vacation_forward; #print OUT while ; #close OUT; #open(OUT, "|/usr/sbin/sendmail $vacation_forward") or die ("Can't sendmail - $!"); #while(\*STDIN) { #print OUT; #} #close (OUT); #while () { print "taco\n";}; #while (\*STDIN) { print "taco\n";}; my ($header, $body); { local $/ = ""; $header = ; undef $/; $body = ; } open(MAIL,"|$sendmail $vacation_forward"); print MAIL "header: $header\n"; print MAIL "body: $body\n\n"; close (MAIL); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/