in reply to Mail::Mailer in a sub question.

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:

Hope that helps somewhat.


All dogma is stupid.

Replies are listed 'Best First'.
Re^2: Mail::Mailer in a sub question.
by docster (Novice) on Feb 09, 2006 at 17:43 UTC
    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.

        Well it seems to be the mail sub there above. I can blank it out and this repeating stops, like this:
        sub mail { }

      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.
        Ok here is a complete program that for me sends 1 extra email and prints 3 "extra" loops. Sorry for the simple questions and thanks for the help! I am trying here ;)
        #!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Mail::Mailer; my $ourEmail = 'our email'; my $theirEmail = 'their email'; my $mailServer = 'our smtp'; my $logFile="logfile"; my $orderSubject = "Order Information"; my $reportSubject = "Order Report"; # begin main program &log("\n=============================================="); &log("\nBegining program"); # send out emails &mailOrder; &mailReport; print "content-type: text/plain\n\n"; &log("Completed program (OK)"); &log("=============================================="); # end main program sub exit_app { my $why=shift; &log("$why"); exit(1); } sub log { my $logText=shift; open LOG, ">>$logFile"; print LOG "$logText\n"; close LOG; } sub mailOrder { my $orderBody = "Welcome"; eval { my $orderMailer = new Mail::Mailer 'smtp', Server => $mailServer; $orderMailer->open ( { From => $ourEmail, To => $theirEmail, Subject => $orderSubject, } ); print $orderMailer $orderBody; $orderMailer->close(); }; if ($@) { # sending mail failed &exit_app("Could not send order e-mail! (FAILED): $@\n"); } else { # mail was sent &log("The order was e-mailed successfully (OK)"); } } sub mailReport { my $reportBody = "This is the order report"; eval { my $reportMailer = new Mail::Mailer 'smtp', Server => $mailServer; $reportMailer->open ( { From => $ourEmail, To => $ourEmail, Subject => $reportSubject, } ); print $reportMailer $reportBody; $reportMailer->close(); }; if ($@) { # sending mail failed &exit_app("Could not send report e-mail! (FAILED): $@\n"); } else { # mail was sent &log("The report was e-mailed successfully (OK)"); } }
        Running this gives me this in the logfile:
        ============================================== Begining program The order was e-mailed successfully (OK) The report was e-mailed successfully (OK) Completed program (OK) ============================================== The order was e-mailed successfully (OK) The report was e-mailed successfully (OK) Completed program (OK) ============================================== The report was e-mailed successfully (OK) Completed program (OK) ============================================== The report was e-mailed successfully (OK) Completed program (OK) ==============================================
        Running this gives me this at the console:
        content-type: text/plain content-type: text/plain content-type: text/plain content-type: text/plain