in reply to Re^2: Mail::Mailer in a sub question.
in thread Mail::Mailer in a sub question.

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.

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

      Hmm, running that again does the expected thing for me, puts

      ============================================== Begining program The order was e-mailed successfully (OK) The report was e-mailed successfully (OK) Completed program (OK) ==============================================
      into the log,
      content-type: text/plain
      once on the command line, and sends two mails, one to each address. Sorry, no idea what could be wrong. You're calling this from the command line, right?

      All dogma is stupid.
        Yea, I am calling it from the command line and nothing is giving an error. Thank you for testing it though, being it is working for you I can quit banging my head on the wall with the script. Been wrestling with this all day. Now I can at least move on and look for other causes. Thank you for taking the time to help ;)