For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > July 2004 > Help with Mail::Audit module









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with Mail::Audit module
Ichthus

2004-07-19, 7:11 pm

I am trying to use the Mail::Audit module for a small project i am working on, mainly using the resend() function. I have that working, but I would like to add a footer to the email before resending it. Is this possible using the reference that body() returns? The documentation states the following:
quote:
body

Returns a reference to an array of lines in the body of the email.


I am a beginner perl programmer, but have extensive experience with PHP and C++. This description indicates to me that it returns an actual reference (pointer) to the array, so I should be able to modify it. I tried every possile combination of array references that I could think of, but I could not get it to edit the body of the email that gets resent.

If you could let me know the two lines of code that I need to get this reference and then just add a line to the end of the email body, I can take it from there.

Thanks SO much in advance,
Kevin
Jim Keenan

2004-07-20, 3:56 pm

Ichthus <Ichthus.19o6dn@mail.codecomments.com> wrote in message news:<8f830e4f284320247cb748593bed0b1a@news.thenewsgroups.com>...
> I am trying to use the 'Mail::Audit'
> (http://search.cpan.org/~simon/Mail-Audit-2.1/Audit.pm) module for a
> small project i am working on, mainly using the resend() function. I
> have that working, but I would like to add a footer to the email before
> resending it. Is this possible using the reference that body() returns?
> The documentation states the following:
> I am a beginner perl programmer, but have extensive experience with PHP
> and C++. This description indicates to me that it returns an actual
> reference (pointer) to the array, so I should be able to modify it. I
> tried every possile combination of array references that I could think
> of, but I could not get it to edit the body of the email that gets
> resent.


Perl references are not the same as C/C++ pointers. A reference is a
scalar variable whose value is the address in memory of its referent.
You don't modify references; you de-reference them and modify the
underlying variable -- in this case, the underlying array.

Could you post the code you were having trouble with? Thanks.

jimk
Kevin Grubbs

2004-07-22, 3:55 am

> Perl references are not the same as C/C++ pointers. A reference is a
> scalar variable whose value is the address in memory of its referent.
> You don't modify references; you de-reference them and modify the
> underlying variable -- in this case, the underlying array.
>
> Could you post the code you were having trouble with? Thanks.
>
> jimk


Hi Jim,

I thought I replied to this yesterday, but I guess something happened.
Anyway, that is exactly how references (pointers) work in C++. I just
left out the dereferencing step in my explanation, oops :)

Anyway, here's my code:
--------------------------------------------
#!/usr/bin/perl

use Mail::Audit;
use strict;

my $mail = Mail::Audit->new();

my $emailbody = $mail->body();

// try to change the first line of the email
@$emailbody->[0] = "new first line text";
// try to add a line to the end of the email
// this is how I would do in PHP
@$emailbody->[] = "new end of email text";

$mail->resend('me@domain.com');
--------------------------------------------

So I think the trouble I am having is with @$emailbody. Anyways,
thanks for your help in advance!

-Kevin (Ichthus)
Kevin Grubbs

2004-07-22, 8:55 am

>
> Hi Jim,
>
> I thought I replied to this yesterday, but I guess something happened.
> Anyway, that is exactly how references (pointers) work in C++. I just
> left out the dereferencing step in my explanation, oops :)
>
> Anyway, here's my code:
> --------------------------------------------
> #!/usr/bin/perl
>
> use Mail::Audit;
> use strict;
>
> my $mail = Mail::Audit->new();
>
> my $emailbody = $mail->body();
>
> // try to change the first line of the email
> @$emailbody->[0] = "new first line text";
> // try to add a line to the end of the email
> // this is how I would do in PHP
> @$emailbody->[] = "new end of email text";
>
> $mail->resend('me@domain.com');
> --------------------------------------------
>
> So I think the trouble I am having is with @$emailbody. Anyways,
> thanks for your help in advance!
>
> -Kevin (Ichthus)


I should also add... I wrote out @$emailbody to a log file before I
did the resend and it WAS changed (the first line... I didnt try the
[] line). So my problem seems to be, how do I change the body of the
email that gets sent? Its like I am getting the body "by value" rather
than "by reference".

Also I noticed that I used // instead of # for comments when I posted
my code... you can ignore that :)

Thanks in advance!
Kevin
Sisyphus

2004-07-22, 3:56 pm

Kevin Grubbs wrote:

>
> Anyway, here's my code:
> --------------------------------------------
> #!/usr/bin/perl
>


use warnings;

> use Mail::Audit;
> use strict;
>
> my $mail = Mail::Audit->new();
>
> my $emailbody = $mail->body();
>
> // try to change the first line of the email
> @$emailbody->[0] = "new first line text";


@$emailbody->[0] is deprecated (though it works ok). Better to write it
as $$emailbody[0].

> // try to add a line to the end of the email
> // this is how I would do in PHP
> @$emailbody->[] = "new end of email text";
>
> $mail->resend('me@domain.com');
> --------------------------------------------
>
> So I think the trouble I am having is with @$emailbody. Anyways,
> thanks for your help in advance!
>
> -Kevin (Ichthus)


This should illustrate the basics:

use strict;
use warnings;

my @body = ('original first line', 'second line', 'third line',
'original last line');
my $emailbody = \@body;

for(@$emailbody) {print $_, "\n"}
print "\n";

# To change the first element:
$$emailbody[0] = 'new first line';

for(@$emailbody) {print $_, "\n"}
print "\n";

# To change the last element:
$$emailbody[-1] = 'new last line';

for(@$emailbody) {print $_, "\n"}
print "\n";

# To add an additional element to the end:
push @$emailbody, 'an additional line';

for(@$emailbody) {print $_, "\n"}
print "\n";

# To add an additional element to the beginning
unshift @$emailbody, 'a new beginning !!';

for(@$emailbody) {print $_, "\n"}
print "\n";

__END__

Where I have written '@$emailbody' in the above example code, you could
just as well replace it with '@body'.

However, looking at the module's documentation, it seems to me that the
changes you make will *not* be reflected in the message that gets
resent. I think that the body() method merely copies the body of the
email into an array, and returns a reference to that array. You can
change the elements that make up that array, but that has no effect on
the actual body of the email that gets re-sent (which remains
unchanged). I don't see any documentation there that suggests it's
possible to alter the body of the email that gets re-sent - though I
haven't used this module and, faik, it may in fact be possible to edit
the email. Otherwise you need to use a different module - MIME::Lite or
Net::SMTP for example (which do allow you to compose and send emails).

Cheers,
Rob


--
To reply by email u have to take out the u in kalinaubears.

Jim Keenan

2004-07-22, 3:56 pm

kevinviagoogle@mailinator.com (Kevin Grubbs) wrote in message news:<9592e51f.0407211849.24ea06b1@posting.google.com>...
> I just
> left out the dereferencing step in my explanation, oops :)
>
> Anyway, here's my code:
> --------------------------------------------
> #!/usr/bin/perl
>
> use Mail::Audit;
> use strict;
>
> my $mail = Mail::Audit->new();
>
> my $emailbody = $mail->body();
>
> // try to change the first line of the email
> @$emailbody->[0] = "new first line text";
> // try to add a line to the end of the email
> // this is how I would do in PHP
> @$emailbody->[] = "new end of email text";
>
> $mail->resend('me@domain.com');
> --------------------------------------------
>
> So I think the trouble I am having is with @$emailbody. Anyways,
> thanks for your help in advance!
>
> -Kevin (Ichthus)


I don't have Mail::Audit installed at the box where I'm currently
sitting, so the following suggestion is UNTESTED with Mail::Audit.
But I think you've got your sigils (the punctuation characters in
front of the variable names) wrong. In Perl 5:

$emailbody # is a reference, and therefore a scalar
@{$emailbody} # is an array which is the referent of $emailbody
@$emailbody # [same thing]
${$emailbody}[0] # is the scalar sitting in index position 0 of
# @{$emailbody}
$$emailbody[0] # [same thing]

So I suspect your code should read either:

${$emailbody}[0] = "new first line text";

or

$$emailbody[0] = "new first line text";

Granted, this has been a long-time source of confusion and I believe
it is slated for revision in Perl 6.

jimk
Joachim Schrod

2004-07-22, 3:56 pm

Sisyphus wrote:
>
> However, looking at the module's documentation, it seems to me that the
> changes you make will *not* be reflected in the message that gets
> resent. I think that the body() method merely copies the body of the
> email into an array, and returns a reference to that array. You can
> change the elements that make up that array, but that has no effect on
> the actual body of the email that gets re-sent (which remains
> unchanged). I don't see any documentation there that suggests it's
> possible to alter the body of the email that gets re-sent - though I
> haven't used this module and, faik, it may in fact be possible to edit
> the email. Otherwise you need to use a different module - MIME::Lite or
> Net::SMTP for example (which do allow you to compose and send emails).


I don't have time to try it out, sorry. But I use it, so here is what I
remember:

Mail::Audit is either a subclass of MIME::Entity or Mail::Internet. You
can check with the method is_mime(). If the object is not a MIME mail,
then you can alter the email body by passing a new array to the body()
method. That is described in "perldoc Mail::Internet". If the object is
a MIME mail, you need other methods to change it -- obviously, as a MIME
body is a more complex object.

Cheers,
Joachim

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Joachim Schrod Email: jschrod@acm.org
Roedermark, Germany
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com