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

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

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

    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 ;)