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

Distinguished monks, I am trying to patch together a simple little app to forward e-mails through Evolution (pipe message to shell command) back out into the world though a SMTP server. The command line format is: perl smtp_mail somebody@somewhere.com Message I've got this in a shell script: /usr/bin/perl /misc/smtp_mail somebody@somewhere.com $1 I can execute the code below from the command-line and it works fine. I can pipe the e-mail through Evolution (ie /bin/cat $1 > some.log) to capture the message. But I can not get the code to forward any part of the message. I'm sure I'm missing something obvious. Please help.
#!/usr/bin/perl # # # use strict; use warnings; use lib '/usr/lib/perl5/site_perl/5.8.0/MIME'; use MIME::Lite; unless (@ARGV) { die "1"; } if ($ARGV[0] !~ /@.*?\..*?/) { die "1"; } my $to = $ARGV[0]; my $message; my $counter = 0; foreach my $inline (@ARGV) { if ($counter >= 1) $message += $inline . "\n"; ++$counter; } my $sendemail = MIME::Lite->new( From => 'me@whereiam.net', To => $to, Data => $message, Type => 'TEXT' ); $sendemail->send('smtp', 'valid.smtp.server') or die "1";

Replies are listed 'Best First'.
Re: Passing an array, or not? MIME::Lite
by pg (Canon) on Oct 11, 2004 at 22:45 UTC

    Your code does not pass perl -c. That line with if shall be:

    if ($counter >= 1) {$message += $inline . "\n"};
    "my $counter = 0; foreach my $inline (@ARGV) { if ($counter >= 1) $message += $inline . "\n"; ++$counter; #a wasting, although not a big one ;-) }"

    Looks like the whole purpose of using $counter is to skip the first element, why not just:

    for my $index (1 .. $#ARGV) { $message += $ARGV[$index] . "\n"; }

    Now why you skip the first element? Try this:

    use strict; use warnings; for my $index (0 .. $#ARGV) { print $ARGV[$index] . "\n"; }

    Run it with perl -w blah.pl msg1 msg2 msg3, the output is:

    msg1 msg2 msg3

    What are you try to skip ;-?

      Thank you, venerable monk. The first input is the one to whom the e-mail is sent. The subsequent input is the message to be sent. That is processed separately.

      So, perl -w blah.pl you@whereyouare.com MessageBodyHere

      I think the question I have is, how can I capture what is processed by /bin/cat (ie the message itself) but not being input into the command-line script? Where am I losing the data?

Re: Passing an array, or not? MIME::Lite
by kvale (Monsignor) on Oct 11, 2004 at 22:56 UTC
    One problem is that you are not concatenating the message, but instead converting the @ARGV components to numbers and adding them. To concatentae the strings, try
    if ($counter >= 1) { $message .= "$inline\n"; }

    -Mark