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

Brother monks ... in all humbleness, I beg for assistance ...

I am trying to retrieve the individual components of an e-mail message and then re-write it (to a database or to another e-mail box). I am using Mail:Internet which retrieves the "To", "From" and "Subject" by using the get() command. I tried this for the body ... didn't work. Turns out I have to use body(); . Fine, easy enough. I assigned the values which body() calls to a scalar ($body, for instance). Then I tried to
print MAIL '$body\n';
, but it didn't work. It turns out that $body is some kind of an array b/c when I
print MAIL "$body[0]\n";
, I get the first line of the body. I tried to save the output of body() to @body, but the program wouldn't run.

The problem is that I now I want to retrieve and print out a bunch of different e-mail bodies, all of different lengths. So I was going to run a for loop using $#body as the last value to run the loop. But it turns out I can't use this value because (I'm suppossing) its a scalar ($body).

Argghhhh! If I could just get the body contents as a scalar I could just print them out with print "$body\n"; or even if I had it as an array I could run a for loop through the last value of the array and just print it out, but I can't define the last value with $#body because it's not a "@"!! I could just run the for loop through a set value of 1000 lines, but how pretty is that? Not very.

If there is another way to retrieve the body of an e-mail as a usable variable (scalar or array) please let me know.


I beg forgiveness for any misused terminology ... I am still yet a neophyte to these waters.

cdherold

2001-04-06 Edit by Corion : Added CODE tags

Replies are listed 'Best First'.
Re: Retrieval of e-mail body
by tadman (Prior) on Apr 06, 2001 at 04:01 UTC
    The documentation is pretty specific about what you get back, but it might not be easily understood by the average Initiate. From Mail::Internet:
    Body The value of this option should be a reference to an array which contains the lines for the body of the message. Each line should be terminated with `\n' (LF). If Body is given then `Mail::Internet' will not attempt to read the body from `ARG' (even if it is specified).
    So, you get an array reference, which is kind of like an array, but isn't really one unless you "dereference" it. Otherwise, it's just a funny looking scalar that prints out as something like 'ARRAY(0x80f24a0)'.
    my ($body) = $message->Body(); foreach (@$body) { } # Or, if you prefer: my (@body) = @{$message->Body()}; foreach (@body) { } # Or, furthermore: my ($body) = join ('', @{$message->Body()});
    You would certainly want to read up on references.
(jeffa) Re: Retrieval of e-mail body
by jeffa (Bishop) on Apr 06, 2001 at 04:06 UTC
    Sounds like $body is not an array, it is a REFERENCE to an array. You have to treat it a little differently:
    for(0..$#$body) { print $body->[$_], "\n"; # or whaterver you wish to do with the el +ement }
    But why do it that way? I like this better myself:
    foreach (@$body) { print "$_\n"; }

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Retrieval of e-mail body
by geektron (Curate) on Apr 06, 2001 at 04:28 UTC
    i have to agree with jeffa -- the  $#$body syntax ends up being really clunky.

    i'd do something similar to what tadmans's already done:

    my @body = @{ $message->Body }; # force array context foreach my $line ( @body ){ ## process to your heart's content }