Solved

URGENT HELP!! sendmail is stuck in a loop & won't stop!

Posted on 2003-11-20
12
346 Views
Last Modified: 2013-12-25


I wrote this to send mail to a mysql database, then am using cron to send every 15 minutes, but when I tested it it's sending mail non stop, 0ver 8,000 messages in under a half hour. . . what can I do to make it stop, then to fix it?  Here's the script

&OPENdb;

$grab = "SELECT * FROM mail_two where id > '0'";
$results = $dbh->prepare($grab);
$results->execute();
while ($ref = $results->fetchrow_hashref()) {
$mid = $ref->{'id'};
$msub = $ref->{'subject'};
$mmes = $ref->{'message'};



$table = "mail_info";
$checkit = "SELECT * FROM $table WHERE list_verified='yes' AND want_mail='yes'";
$results = $dbh->prepare($checkit);
$results->execute();
$ref = $results->fetchrow_hashref();
$id = $ref->{'id'};
$email = $ref->{'list'};
$first = $ref->{'first_name'};
$last = $ref->{'last_name'};


 
        $message = "$mmes";
      $message =~ s/~first~/$first/g;
      $message =~ s/~last~/$last/g;
      $subject = "$msub";
        $subject =~ s/~first~/$first/g;
        $subject =~ s/~last~/$last/g;

 

  open (MAIL, "|$mailprog -t -oi");
  print MAIL "To: $email($first $last)\n";
  print MAIL "From: ($progname)\n";
  print MAIL "Subject:$subject\n\n";
  print MAIL "==========================================$br\n$br\n";
  print MAIL "$message\n\n";
  print MAIL "==========================================$br\n$br\n";
  print MAIL "$adminad\n\n";
  print MAIL "==========================================$br\n$br\n";
  print MAIL "$disclaimer\n";
  print MAIL "Sent by member #$mid\n\n";
  print MAIL "==========================================$br\n$br\n";  
  close (MAIL);}


&CLOSEdb;


PLEASE HELP!



0
Comment
Question by:ednit
  • 7
  • 4
12 Comments
 
LVL 5

Expert Comment

by:djplaistow
ID: 9791013
Variables  $results. You are attempting to iterate through the results of the query "SELECT * FROM mail_two where id > '0'" in the while loop that starts with the line:

    while ($ref = $results->fetchrow_hashref()) {

But within the while loop you alter $results and $ref with the section of code that looks like
    $checkit = "SELECT * FROM $table WHERE list_verified='yes' AND want_mail='yes'";
    $results = $dbh->prepare($checkit);
    $results->execute();
    $ref = $results->fetchrow_hashref();

You need to use different variables within the while loop.
0
 
LVL 5

Expert Comment

by:djplaistow
ID: 9791062
Oops, change the incoherent giberish, " Variables  $results." to:

The variables $ref and $results cannot be modified within the while loop.

Also, to stop the mail from sending kill the process. Do you know how to do this?
     kill -9 <pid>

The kill command is platform dependent, but should work on *nix variants which I assume you are running as since you are running sendmail. If the script has already queued up millions of emails I would kill the sendmail daemon also. Do you have admin privileges?
0
 

Author Comment

by:ednit
ID: 9791063


Then if I change the $results and run the script again, will it stop the sending. . . I am over 20,000 now. . .
0
 

Author Comment

by:ednit
ID: 9791084
No, I can't get into the shell then if that's what you mean. . . and my host typcally responds to emails sent about 5 hours from now.  Can I run the kill -9 from the cgi script? never heard of it before.
0
 
LVL 5

Expert Comment

by:djplaistow
ID: 9791376
>>> Then if I change the $results and run the script again, will it stop the sending. . . I am over 20,000 now. . .
No, the $results and $ref correction is only to stop this from happening again.

OK, are you hosted on some ISP? Probably on Linux using Apache and virtual hosts?  You don't have access to a shell, but you have access to cron?  I am having difficulties understanding your environment, so let me throw some ideas out there:

kill is a standard *nix command. Depending on the *nix flavor the "-9" may or may not work or be necessary, but kill should be available. If you don't have access to the shell, then execute the kill via cron.  You will need the process ID.    The *nix command is ps (for process show); try "ps -ef" or "ps -aux". OR, if you don't have access to a shell, then write a perl script that uses the system command to shell out to the OS and excute them. If my hunch was right about being virtually hosted at some ISP, then you will run into limitations on what you can do when you shell out to the system, so I don't know how far you will get. The ISP will probably have limitations in place that controls how long Apache will execute a CGI script; if the script runs longer than X seconds then it terminates. The assumption with this policy is that the script is in an infinite loop. However, as I mentioned earlier, I am not even sure if you are executing in a CGI environment. If they do have time limits in place, then your script has probably been killed, and the emails that are being sent are the ones that the script was able to queue before it was killed.
0
 

Author Comment

by:ednit
ID: 9791506
I have access to cron, I have hosting through 5dollarwebs.com I don't think it's virtual hosting, but I am unsure.  It's a unix server, using apache.  To run the process using cron, do I type in the kill -9 <pid> only?  I am not very familiar with cron, only that if I set a time to run a script then it does it when I type in the path. . .like /home/domain/public_html/cgi-bin/script.cgi I am very new to this all, and I am way lost at this moment.  I don't want to try to run the kill -9 <pid> until I know for sure that I don't need anything else typed in there.

Thank you so much for your help.


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ednit
ID: 9791612
Ok, this is what I can do with cron:

 Cron Jobs

This is a web interface to the crontab program.
For example, * * * * * would mean every min and 0 0 * * * would mean at midnight.


Minute Hour Day Month Weekday Command
             
 
 
 These are my only choices. . . I can't run show process. . . or if I can I don't understand how to.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 9791804
How do you access/update your crontab?  Do you have a web admin interface?
0
 

Author Comment

by:ednit
ID: 9791845
cpanel 6 or 5

I cut and pasted the display above, all it's missing are the text input box and the buttons.  it's pretty plain, I can only type in the path of the script I want to run and set the time intervals, that's all know that I can do with it.
0
 

Author Comment

by:ednit
ID: 9791990
OK. . . Well after receiving over 100,00 emails to my outlook express I figured out that I could just delete those accounts, and so now all my mail is staying on the server. . . .  The host will get with me in a few hours (I hope). . .  So, I will get that part fixed soon, but do you know how I can loop through the 2 tables successfully?  I didn't understand the  

"The variables $ref and $results cannot be modified within the while loop."

So. .  Can I rename the variables in the loop like this?

$grab = "SELECT * FROM mail_two where id > '0'";
$results = $dbh->prepare($grab);
$results->execute();
while ($ref = $results->fetchrow_hashref()) {
$mid = $ref->{'id'};
$msub = $ref->{'subject'};
$mmes = $ref->{'message'};



$table = "mail_info";
$checkit = "SELECT * FROM $table WHERE list_verified='yes' AND want_mail='yes'";
$results2 = $dbh->prepare($checkit);
$results2->execute();
$ref2 = $results2->fetchrow_hashref();
$id = $ref2->{'id'};
$email = $ref2->{'list'};
$first = $ref2->{'first_name'};
$last = $re2f->{'last_name'};

. . . and so on?

I only ask before I try it cuz I do not want to tie up another server (trying it on a different website). . .

Like I said, I will figure out the never-ending mail situation later, I just want to be sure on the correct syntax before trying it though. . .

0
 
LVL 5

Accepted Solution

by:
djplaistow earned 500 total points
ID: 9797747
Yes, changing $results and $ref to $results2 and $ref2 within the while loop is what I was intending.

    However, I think you should go about testing this a different way. I never test on a box for which I do not have admin rights, for the reasons you just encountered. Test it locally on some box you admin, and get it working there before you move it to the ISP. If you can't do that, at least modify the script so it won't email. Comment out the email portion of the code and just have it output something to the screen. If you try this on the ISP you won't be able to see the console screen, so change the script to write to a file, or redirect STDOUT to a file.

That is, comment out:

   open (MAIL, "|$mailprog -t -oi");
   print MAIL "To: $email($first $last)\n";
   print MAIL "From: ($progname)\n";
   print MAIL "Subject:$subject\n\n";
   print MAIL "==========================================$br\n$br\n";
   print MAIL "$message\n\n";
   print MAIL "==========================================$br\n$br\n";
   print MAIL "$adminad\n\n";
   print MAIL "==========================================$br\n$br\n";
   print MAIL "$disclaimer\n";
   print MAIL "Sent by member #$mid\n\n";
   print MAIL "==========================================$br\n$br\n";  
   close (MAIL);}


And Add:

   #Before the while loop, open the debug file
   open (DEBUG_FILE, ">$debugDir/debug.txt");

   # Now, instead of sending the email, print enough information so you can tell that the db access worked
   print DEBUG_FILE "To: $email($first $last)\n";
   print DEBUG_FILE "From: ($progname)\n";
   print DEBUG_FILE "Subject:$subject\n";
   print DEBUG_FILE "--------------------------------------------------\n\n";

   #After the while loop, close the debug file
   close (DEBUG_FILE);
0
 

Author Comment

by:ednit
ID: 9799044
Thank you.  The mail has stopped, and I will definitly be printing the output while testing, makes much more sense(don't want that mess again!).  Thanks again for your help, it is much appreciated.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now