You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Ken Miller <kl...@shetlandsoftware.com> on 2005/02/16 21:58:55 UTC
Strange problem with Mail::Send
My web app sends notification emails when a task transitions from one
part of the work flow to another. I use Mail::Send to send mail, using
a smtp server to transfer the mail. The code looks something like this:
my $mail = Mail::Send->new( Subject => 'a subject', To =>
'someone@example.com );
my $fh = $mail->open( 'smtp', Server => 'some.host.name' );
print $fh "A message\n";
$fh->close;
To ensure that the recipient receives the mail from a recognizeable
email address, I set the MAILADDRESS environment variable as follows:
use constant FROM_ADDRESS => 'somebody@example.com';
and then, just before the above code to send the mail, I do this:
local $ENV{ MAILADDRESS } = FROM_ADDRESS
This all works fine, except for one situation:
Ocassionally, the 'From' email address is not the constant address as
shown above. Instead, it's the 'from' email address from another part
of the application completely unrelated to this notification email. The
code that sends this email uses this paradigm:
eval {
local $ENV{ MAILADDRESS } = $some_other_email_address;
(same mail boiler plate as above);
};
It almost seems that the mail address is getting stuck.
I don't have my mod-perl book handy right now, but I seem to remember
that %ENV is restored after each request to the server startup state -
is this correct? If so, i should not need 'local' in front of the
environment assignment, assuming that I'm not worries about the ENV
changing during a request (which I'm not).
Is there some strange interplay going on between eval, local, and
mod_perl that could cause this behaviour? I've been unable to duplicate
the problem in my test environment, so I'm just taking shots in the dark
hoping to hit something.
Suggestions?
Cheers!
-klm.
Re: Strange problem with Mail::Send
Posted by Stas Bekman <st...@stason.org>.
Ken Miller wrote:
> My web app sends notification emails when a task transitions from one
> part of the work flow to another. I use Mail::Send to send mail, using
> a smtp server to transfer the mail. The code looks something like this:
>
> my $mail = Mail::Send->new( Subject => 'a subject', To =>
> 'someone@example.com );
> my $fh = $mail->open( 'smtp', Server => 'some.host.name' );
> print $fh "A message\n";
> $fh->close;
>
> To ensure that the recipient receives the mail from a recognizeable
> email address, I set the MAILADDRESS environment variable as follows:
>
> use constant FROM_ADDRESS => 'somebody@example.com';
>
> and then, just before the above code to send the mail, I do this:
>
> local $ENV{ MAILADDRESS } = FROM_ADDRESS
>
> This all works fine, except for one situation:
>
> Ocassionally, the 'From' email address is not the constant address as
> shown above. Instead, it's the 'from' email address from another part
> of the application completely unrelated to this notification email. The
> code that sends this email uses this paradigm:
>
> eval {
> local $ENV{ MAILADDRESS } = $some_other_email_address;
> (same mail boiler plate as above);
> };
>
> It almost seems that the mail address is getting stuck.
>
> I don't have my mod-perl book handy right now, but I seem to remember
> that %ENV is restored after each request to the server startup state -
> is this correct? If so, i should not need 'local' in front of the
> environment assignment, assuming that I'm not worries about the ENV
> changing during a request (which I'm not).
>
> Is there some strange interplay going on between eval, local, and
> mod_perl that could cause this behaviour? I've been unable to duplicate
> the problem in my test environment, so I'm just taking shots in the dark
> hoping to hit something.
>
> Suggestions?
1) which mod_perl version are you using, Ken?
2) why do you use %ENV to pass the 'from' address, rather than pass it
expicitly?
local() has the effect only for the block you defined it in, but if you've
created a closure later on (not in %ENV at all), it may be the reason.
just add debug prints to test it. I have a feeling that that's exactly
what you are doing: creating a closure.
If you want us to look at it, please reduce the problematic code to the
very minimum and post it here.
--
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com