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

Hi everyone. I have an issue using Mail::Mailer in a sub routine, it duplicates the emails up to 4 times like it is looping through. Im fairly new to perl and this is my first time dealing with Mail::Mailer. Does anyone have any ideas on this? I added the eval hoping to be able to log what happens but it seems to make no difference when removed. If you call the sub it will generate 1 to 4 emails.
sub mailThis { $mailServer = "serverName"; $ourEmail = 'ourEmail'; $theirEmail = 'theirEmail'; $subject = "Test Email"; $body = "Welcome $username"; eval { $mailer = new Mail::Mailer 'smtp', Server => $mailServer; $mailer->open ( { From => $ourEmail, To => $theirEmail, Subject => $subject, } ); print $mailer $body; $mailer->close(); }; if ($@) { # sending mail failed } else { # mail was sent } }

Replies are listed 'Best First'.
Re: Mail::Mailer in a sub question.
by tirwhan (Abbot) on Feb 09, 2006 at 16:44 UTC

    Nope, just tried it and it sends just one email, as expected. You are probably calling the sub multiple times (since you're not showing the calling code we cannot know). A couple of pointers to help you determine what is wrong:

    • use strict; and use warnings;. This will require you to declare your variables (usually with "my $variable"), but will uncover bugs that are otherwise hidden.
    • Case in point: you assign the value "Test Email" to $subject, but then use $Subject later on. Case matters, and use strict would have caught this.
    • When assigning an email address to your variable, you should use single quotes or q(). Otherwise the @ sign in the email address wil result in perl thinking you've got an array variable in there which is almost certainly not what you want.

    Hope that helps somewhat.


    All dogma is stupid.
      What seems to be happening is this (Forgive me if I cannot explain it correctly, I am not a programmer by trade.). When I call the subroutine the script runs the subroutine, returns to the line under it and continues. What is happening is when I call the subroutine the script goes to it, but then continues on, then returns from the sub and continues again, running what ever lines are underneath it again. I hope that made sense. Maybe there is a time out on waiting on a sub to complete? Say I have this:
      &mail; print "Hi!\n";
      I get:
      Hi! Hi!
      Does that make sense? Sorry if the question is so simple that everyone else on the planet already knows it, I seem to have missed it somewhere. What happened was I had 2 subs, one for a report and one for a copy. the top one causes everything under it to repeat itself thus, duplicating mail.

        Sounds like something's calling fork to do something in a child process, but that child's not exiting and it's running the same code as the parent after the parent's gone on. If you have any such code double check it.

        Can you put together a minimal example of your code that runs and exhibits this problem? The code you posted is fine, and I suspect you have something different in the code which fails for you (for example, in the original code the subroutine is called mailThis, but in above code it's just called mail), and that's where the error lies somewhere


        All dogma is stupid.