Avoiding timeout while mass mailing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roger Dodger

    #1

    Avoiding timeout while mass mailing

    I have to send out 2500 e-mails to student survey recipients. The
    e-mails will appear to come from the specific departments that they
    belong to. I'm utilizing PHPmailer in the scripts I wrote to
    accomplish this. I've written several pages to edit emails utilizing
    placeholders so each individual email can be personalized for the
    recipient, and selection page to choose recipients from the database.
    The code I've written works just fine for small batches, but I'm unable
    to test the mass mail as I don't have a suitable test list of email
    addresses. I've adjusted the max_execution_t ime in the php.ini to 300
    seconds, which should probably be long enough to send them all. I'll
    start off sending in small batches, but eventually will want to just
    send them out all at once for future mailings. Can you look at the
    code below and suggest any other timeout issues that might appear?
    Thanks all.

    require("class. phpmailer.php") ;
    require("dbconn .php");

    session_start() ;

    $mail = new PHPMailer();

    $mail->Subject = $_SESSION['subject'];
    $mail->Host = "smtp.*******.e du";
    $mail->Mailer = "smtp";
    $mail->AddBCC($_SESSI ON['bcc'],"SURVEY BCC");
    $mail->SMTPKeepAliv e = true;

    $enabled_only = true;
    $rs = getRecips($conn , $enabled_only); //Record set contains recipient
    data

    $i=0;

    foreach ($rs as $row) {
    $mail->From = $row['DEPT_CONTACT_E MAIL'];
    $mail->FromName = $row['DEPT_CONTACT'];
    $mail->AddReplyTo($ro w['DEPT_CONTACT_E MAIL'],$row['DEPT_CONTACT']);

    // HTML body
    $body = processHtmlBody ($_SESSION['body'], $row, true);


    // Plain text body (for mail clients that cannot read HTML)

    $text_body = processTextBody ($_SESSION['body'], $row, true);

    $mail->Body = $body;
    $mail->AltBody = $text_body;
    $fullname = $row['FIRST_NAME']." ".$row['LAST_NAME'];
    $mail->AddAddress($ro w['EMAIL'], $fullname);


    if(!$mail->Send()) {
    $_SESSION['send_result'][$i] = "Mail error sending to " .
    $row['EMAIL'] . "<br />\nError Message: ".$mail->ErrorInfo."< br />\n";
    }
    else {
    $_SESSION['send_result'][$i] = "Mail sent to ".$row['EMAIL']."
    from department: ".$row['DEPT_NAME']."<br />\n";
    }
    $i++;
    // Clear all addresses and reply-tos for next loop
    $mail->ClearAddresses ();
    $mail->ClearReplyTos( );

    }
    $mail->SmtpClose();
    clearRecips($co nn);

    header("locatio n: http://localhost:8080/phpmailer/mailresult.php" );

  • Jake

    #2
    Re: Avoiding timeout while mass mailing

    Try adding set_time_limit( 0); to the top of your script. This sets the
    execution timelimit for this script to be infinite. Alternativly, you
    could rewrite the script to send the email in batches to squeeze under
    the normal timelimit restrictions.

    -Jake

    Comment

    • Roger Dodger

      #3
      Re: Avoiding timeout while mass mailing

      I've considered breaking it up into batches, but right now am relying
      on the redirect and session variable to show send results and errors.
      Maybe I should drop that information into a log file instead? If I
      batched it, would that require some user input to continue the process,
      or could it be set up to just load an intermediate page which redirects
      back to the sending page if more emails are in the queue?

      Jake wrote:[color=blue]
      > Try adding set_time_limit( 0); to the top of your script. This sets the
      > execution timelimit for this script to be infinite. Alternativly, you
      > could rewrite the script to send the email in batches to squeeze under
      > the normal timelimit restrictions.
      >
      > -Jake[/color]

      Comment

      • Satya

        #4
        Re: Avoiding timeout while mass mailing

        What about writing a script such way that it send like 1000 mail and
        sleep for 1 sec and start again

        Comment

        • Satya

          #5
          Re: Avoiding timeout while mass mailing

          What about writing a script such way that it send like 1000 mail and
          sleep for 1 sec and start again

          Comment

          • Jerry Stuckle

            #6
            Re: Avoiding timeout while mass mailing

            Satya wrote:[color=blue]
            > What about writing a script such way that it send like 1000 mail and
            > sleep for 1 sec and start again
            >[/color]

            sleep() doesn't reset the timeout clock, so this doesn't work. In fact, since
            you're sleeping during part of your total allowed time, you'll get even less done.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Roger Dodger

              #7
              Re: Avoiding timeout while mass mailing


              Jerry Stuckle wrote:[color=blue]
              > Satya wrote:[color=green]
              > > What about writing a script such way that it send like 1000 mail and
              > > sleep for 1 sec and start again
              > >[/color]
              >
              > sleep() doesn't reset the timeout clock, so this doesn't work. In fact, since
              > you're sleeping during part of your total allowed time, you'll get even less done.
              >
              > --
              > =============== ===
              > Remove the "x" from my email address
              > Jerry Stuckle
              > JDS Computer Training Corp.
              > jstucklex@attgl obal.net
              > =============== ===[/color]

              So I went with the suggestion to use set_time_limit( 0) at the top of
              the script. That works just fine. I can send 2500 e-mails plus bcc
              each one to a separate account in 4-6 minutes. To insure that there is
              some record of what has been sent if the program were to crash, I open
              a log file before the main loop, and record a success or error message
              in the if (! $mail->send() ) { } else { } clause. It works great!

              I've also managed to embed images in the mails as well, so if you are
              looking for a mass mailer, I highly recommend the open source PHPmailer
              class.

              Comment

              • twanglee
                New Member
                • May 2006
                • 2

                #8
                hi i'm new to this forum and have a problem i own a small business and wish to xpand but i'm havin problems sending bulk emails can any one please help me

                Comment

                • twanglee
                  New Member
                  • May 2006
                  • 2

                  #9
                  hi

                  hi i'm new to this forum and have a problem i own a small business and wish to xpand but i'm havin problems sending bulk emails can any one please help me

                  Comment

                  Working...