mail () breaks loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Culus

    mail () breaks loop

    I seem to be having troubles with this page....


    It's an automated mailing script...
    Steps:
    - system checks on x dates the variables linked to a maintenance
    contract (query on db)
    - when variable matches given element is queries the db again to
    retrieve the contact data linked to the maintenance system
    - I then sends an email to the different contacts linked to that
    maintenance contract
    - then the script schoul continue looping to the next maintenance
    contract


    BUT: somehow, the script stop after sending the first email.
    Why?


    <?
    // global variables
    require("DbConn ect.class.php") ;


    // -------------- calculate dates ------


    $yearNow = date(Y);
    $monthNow = date(m);
    //$monthNow = 12;
    $dayNow = date(d);
    $now = date(Ymd);


    $mydateBeginMon th = date("Ym01");
    $mydateHalfMont h = date("Ym15");


    $monthPlusOne = $monthNow + 1;
    $monthPlusTwo = $monthNow + 2;
    $yearPlusOne = $yearNow;
    $yearPlusTwo = $yearNow;


    // ------------------------- Plus 1
    month ------------------------------------
    //
    ---------------------------------------------------------------------------


    // December = NOW
    if ($monthPlusOne == 13){
    $monthPlusOne = "01";
    $yearPlusOne = $yearNow + 1;
    }


    $myDatePlusOneB eginMonth = $yearPlusOne."" .$monthPlusOne. "01";
    $myDatePlusOneH alfMonth = $yearPlusOne."" .$monthPlusOne. "15";


    // ------------------------- Plus 2
    months ------------------------------------
    //
    ---------------------------------------------------------------------------


    // November = NOW
    if ($monthPlusTwo == 13){
    $monthPlusTwo = "01";
    $yearPlusTwo = $yearNow + 1;
    }


    // December = NOW
    if ($monthPlusTwo == 14){
    $monthPlusTwo = "02";
    $yearPlusTwo = $yearNow + 1;
    }


    $myDatePlusTwoB eginMonth = $yearPlusTwo."" .$monthPlusTwo. "01";
    $myDatePlusTwoH alfMonth = $yearPlusTwo."" .$monthPlusTwo. "15";


    echo $mydateBeginMon th." - ".$mydateHalfMo nth."<br><br><h r><br>";
    echo $myDatePlusOneB eginMonth." -
    ".$myDatePlusOn eHalfMonth."<br ><br><hr><br> ";
    echo $myDatePlusTwoB eginMonth." -
    ".$myDatePlusTw oHalfMonth."<br ><br><hr><br> ";


    //*************** *************** *************** *************** *******


    $db->open() or die($db->error());
    $db->query("SELEC T maintenanceRefN umber, endDate, contactIDdistri butor,


    contactIDresell er, serialID, endUserID FROM TBL_maintenance ORDER BY
    endDate
    ASC") or die($db->error());
    $i = 1;
    while ($row=$db->fetcharray() ) {
    $maintenanceRef Number = $row[0];
    $endDate = $row[1];
    $contactIDdistr ibutor = $row[2];
    $contactIDresel ler = $row[3];
    $serialID = $row[4];
    $endUserID = $row[5];


    //echo "$i - $maintenanceRef Number - $endDate<br>";


    list($day, $month, $year) = explode(".", $endDate);
    $myMaintenanceD ate = $year."".$month ."".$day;


    if (($myMaintenanc eDate >= $mydateBeginMon th) && ($myMaintenance Date <


    $mydateHalfMont h)) {
    // ----------------- GET DATA
    ----------------------------------------
    //
    -------------------------------------------------------------------


    $db->query("SELEC T tbl_product.pro ductNaam,
    tbl_product.pro ductVersie,
    tbl_product.pro ductType FROM tbl_product, tbl_serials WHERE
    (tbl_serials.se rialID = $serialID) AND (tbl_serials.pr oductID =
    tbl_product.pro ductID)") or die($db->error());
    while ($row2=$db->fetcharray() ) {
    $productNaam = $row2[0];
    $productVersie = $row2[1];
    $productType = $row2[2];
    }


    $db->query("SELEC T email, mailings FROM tbl_contacts WHERE contactID
    =
    $contactIDdistr ibutor") or die($db->error());
    while ($row3=$db->fetcharray() ) {
    $distrimail = $row3[0];
    $distrmailok = $row3[1];
    }


    $db->query("SELEC T email, mailings FROM tbl_contacts WHERE contactID
    =
    $contactIDresel ler") or die($db->error());
    while ($row4=$db->fetcharray() ) {
    $dreselmail = $row4[0];
    $reselmailok = $row4[1];
    }


    $db->query("SELEC T email, mailings, companyName, firstName, lastName
    FROM
    tbl_contacts WHERE contactID = $endUserID") or die($db->error());
    while ($row5=$db->fetcharray() ) {
    $endusmail = $row5[0];
    $endusmailok = $row5[1];
    $companyName = $row5[2];
    $firstName = $row5[3];
    $lastName = $row5[4];
    }


    //mail
    $subject = "Maintenanc e Renewal: $maintenanceRef Number";
    $message = "Dear Madam,\nDear Sir,\n\nWe would like to inform you
    that
    your Maintenance contract for $productNaam $productType $productVersie
    with
    Maintenance Reference ID: $maintenanceRef Number will expire on
    $endDate.\n
    Etc....";
    $sendto = "";


    // send email
    if ($distrmailok == "1") {
    $sendto = $distrimail;
    mail($sendto, $subject, $message);
    //echo $distrimail."($ maintenanceRefN umber distr), ";
    }


    if ($reselmailok == "1") {
    $sendto = $sendto.", ".$dreselma il;
    mail($sendto, $subject, $message);
    //echo $dreselmail."($ maintenanceRefN umber resel), ";
    }


    $sendto = $sendto.", ".$endusmai l;
    mail($sendto, $subject, $message);
    //echo $endusmail."($m aintenanceRefNu mber endus)<br>";


    echo "$maintenanceRe fNumber - $myMaintenanceD ate - $distrimail -
    $dreselmail - $endusmail<br>" ;
    //echo "$maintenanceRe fNumber - $myMaintenanceD ate<br>";
    }


    if (($myMaintenanc eDate >= $myDatePlusOneB eginMonth) &&
    ($myMaintenance Date
    < $myDatePlusOneH alfMonth)) {
    $db->query("SELEC T tbl_product.pro ductNaam,
    tbl_product.pro ductVersie,
    tbl_product.pro ductType FROM tbl_product, tbl_serials WHERE
    (tbl_serials.se rialID = $serialID) AND (tbl_serials.pr oductID =
    tbl_product.pro ductID)") or die($db->error());
    while ($row2=$db->fetcharray() ) {
    $productNaam = $row2[0];
    $productVersie = $row2[1];
    $productType = $row2[2];
    }


    $db->query("SELEC T email, mailings FROM tbl_contacts WHERE contactID
    =
    $contactIDdistr ibutor") or die($db->error());
    while ($row3=$db->fetcharray() ) {
    $distrimail = $row3[0];
    $distrmailok = $row3[1];
    }


    $db->query("SELEC T email, mailings FROM tbl_contacts WHERE contactID
    =
    $contactIDresel ler") or die($db->error());
    while ($row4=$db->fetcharray() ) {
    $dreselmail = $row4[0];
    $reselmailok = $row4[1];
    }


    $db->query("SELEC T email, mailings, companyName, firstName, lastName
    FROM
    tbl_contacts WHERE contactID = $endUserID") or die($db->error());
    while ($row5=$db->fetcharray() ) {
    $endusmail = $row5[0];
    $endusmailok = $row5[1];
    $companyName = $row5[2];
    $firstName = $row5[3];
    $lastName = $row5[4];
    }


    //mail
    $subject = "Maintenanc e Renewal: $maintenanceRef Number";
    $message = "Dear Madam,\nDear Sir,\n\nWe would like to inform you
    that
    your Maintenance contract for $productNaam $productType $productVersie
    with
    Maintenance Reference ID: $maintenanceRef Number will expire on
    $endDate.\n
    Etc...";
    $sendto = "";


    // send email
    if ($distrmailok == "1") {
    $sendto = $distrimail;
    //mail($sendto, $subject, $message);
    //echo $distrimail."($ maintenanceRefN umber +1/+2 distr), ";
    }


    if ($reselmailok == "1") {
    $sendto = $sendto.", ".$dreselma il;
    //mail($sendto, $subject, $message);
    //echo $dreselmail."($ maintenanceRefN umber +1/+2 resel), ";
    }


    if ($endusmailok == "1") {
    $sendto = $sendto.", ".$endusmai l;
    mail($sendto, $subject, $message);
    //echo $endusmail."($m aintenanceRefNu mber +1/+2 endus)<br>";
    } else {
    $sendtoOrder = "t...@test.com" ;
    $message = "TESTTESTTESTET ESTE\nPlease forward message to
    ECU\n\nFollowin g Maintenance needs to be renewed automatically:
    $maintenanceRef Number\nYour contact is $companyName\n$ firstName
    $lastName\n$end usmail.";
    mail($sendtoOrd er, $subject, $message);
    mail($sendto, $subject, $message);
    }


    echo "$maintenanceRe fNumber - $myMaintenanceD ate - $distrimail -
    $dreselmail - $endusmail<br>" ;
    //echo "------ +1 ------ $maintenanceRef Number -
    $myMaintenanceD ate<br>";
    }


    if (($myMaintenanc eDate >= $myDatePlusTwoB eginMonth) &&
    ($myMaintenance Date
    < $myDatePlusTwoH alfMonth)) {
    $db->query("SELEC T tbl_product.pro ductNaam,
    tbl_product.pro ductVersie,
    tbl_product.pro ductType FROM tbl_product, tbl_serials WHERE
    (tbl_serials.se rialID = $serialID) AND (tbl_serials.pr oductID =
    tbl_product.pro ductID)") or die($db->error());
    while ($row2=$db->fetcharray() ) {
    $productNaam = $row2[0];
    $productVersie = $row2[1];
    $productType = $row2[2];
    }


    $db->query("SELEC T email, mailings FROM tbl_contacts WHERE contactID
    =
    $contactIDdistr ibutor") or die($db->error());
    while ($row3=$db->fetcharray() ) {
    $distrimail = $row3[0];
    $distrmailok = $row3[1];
    }


    $db->query("SELEC T email, mailings FROM tbl_contacts WHERE contactID
    =
    $contactIDresel ler") or die($db->error());
    while ($row4=$db->fetcharray() ) {
    $dreselmail = $row4[0];
    $reselmailok = $row4[1];
    }


    $db->query("SELEC T email, mailings, companyName, firstName, lastName
    FROM
    tbl_contacts WHERE contactID = $endUserID") or die($db->error());
    while ($row5=$db->fetcharray() ) {
    $endusmail = $row5[0];
    $endusmailok = $row5[1];
    $companyName = $row5[2];
    $firstName = $row5[3];
    $lastName = $row5[4];
    }


    //mail


    $subject = "Maintenanc e Renewal: $maintenanceRef Number";
    $message = "Dear Madam,\nDear Sir,\n\nWe would like to inform you
    that
    your Maintenance contract for $productNaam $productType $productVersie
    with
    Maintenance Reference ID: $maintenanceRef Number will expire on
    $endDate.\n
    Etc...";
    $sendto = "";


    // send email
    if ($distrmailok == "1") {
    $sendto = $distrimail;
    //mail($sendto, $subject, $message);
    //echo $distrimail."($ maintenanceRefN umber +1/+2 distr), ";
    }


    if ($reselmailok == "1") {
    $sendto = $sendto.", ".$dreselma il;
    //mail($sendto, $subject, $message);
    //echo $dreselmail."($ maintenanceRefN umber +1/+2 resel), ";
    }


    if ($endusmailok == "1") {
    $sendto = $sendto.", ".$endusmai l;
    mail($sendto, $subject, $message);
    //echo $endusmail."($m aintenanceRefNu mber +1/+2 endus)<br>";
    } else {
    $sendtoOrder = t...@test.com";
    $message = "TESTTESTTESTET ESTE\nPlease forward message to
    ECU\n\nFollowin g Maintenance needs to be renewed automatically:
    $maintenanceRef Number\nYour contact is $companyName\n$ firstName
    $lastName\n$end usmail.";
    mail($sendtoOrd er, $subject, $message);
    mail($sendto, $subject, $message);
    }


    echo "$maintenanceRe fNumber - $myMaintenanceD ate - $distrimail -
    $dreselmail - $endusmail<br>" ;
    //echo "------ +2 ------ $maintenanceRef Number -
    $myMaintenanceD ate<br>";
    }


    }


    ?>


  • Oli Filth

    #2
    Re: mail () breaks loop


    Eric Culus wrote:[color=blue]
    > I seem to be having troubles with this page....
    >
    >
    > It's an automated mailing script...
    > Steps:
    > - system checks on x dates the variables linked to a maintenance
    > contract (query on db)
    > - when variable matches given element is queries the db again to
    > retrieve the contact data linked to the maintenance system
    > - I then sends an email to the different contacts linked to that
    > maintenance contract
    > - then the script schoul continue looping to the next maintenance
    > contract
    >
    >
    > BUT: somehow, the script stop after sending the first email.
    > Why?
    >[/color]
    <...SNIP 300 LINES OF CODE...>

    Dude, NO-ONE is going to trawl through 300+ lines of your code to try
    to figure out what's wrong.

    Post a *minimal* test script, and we might be able to help...

    --
    Oli

    Comment

    • Samuel

      #3
      Re: mail () breaks loop

      I agree with Oli. If you give us a "short but complete" script it will
      be sooo much easier. Read this article from John Skeet to know how to
      make it...



      All credit to that great guy.

      Comment

      • Eric Culus

        #4
        Re: mail () breaks loop

        Hope,

        This' shorter...

        <?

        $db->open() or die($db->error());
        $db->query("myquery ") or die($db->error());
        $i = 1;
        while ($row=$db->fetcharray() ) {
        output db data

        if (($myMaintenanc eDate >= $mydateBeginMon th) && ($myMaintenance Date <
        $mydateHalfMont h)) {

        $db->query("Start another query") or die($db->error());
        while ($row2=$db->fetcharray() ) {
        $productNaam = $row2[0];
        $productVersie = $row2[1];
        $productType = $row2[2];
        }

        Etc... more queries until all data recovered

        //mail
        $subject = "mysubject" ;
        $message = "mynessage" ;
        $sendto = "myemail";

        // send email
        if ($distrmailok == "1") {
        $sendto = $distrimail;
        mail($sendto, $subject, $message);
        }

        if ($reselmailok == "1") {
        $sendto = $sendto.", ".$dreselma il;
        mail($sendto, $subject, $message);
        }

        $sendto = $sendto.", ".$endusmai l;
        mail($sendto, $subject, $message);
        }
        }

        loop until next matching record found....

        ?>


        Comment

        • Mark Rees

          #5
          Re: mail () breaks loop

          > <?[color=blue]
          >
          > $db->open() or die($db->error());
          > $db->query("myquery ") or die($db->error());[/color]

          Don't know what's going on in your $db class, but do you have to assign the
          result set to something?

          e.g. $result=$db->query("Start another query") or die($db->error());
          [color=blue]
          > $i = 1;
          > while ($row=$db->fetcharray() ) {
          > output db data
          >
          > if (($myMaintenanc eDate >= $mydateBeginMon th) && ($myMaintenance Date <
          > $mydateHalfMont h)) {
          >
          > $db->query("Start another query") or die($db->error());[/color]

          Does this call to $db overwrite the result set you are looping on - the one
          you initialise with query("myquery" ) ?

          [color=blue]
          > while ($row2=$db->fetcharray() ) {
          > $productNaam = $row2[0];
          > $productVersie = $row2[1];
          > $productType = $row2[2];
          > }
          >
          > Etc... more queries until all data recovered
          >
          > //mail
          > $subject = "mysubject" ;
          > $message = "mynessage" ;
          > $sendto = "myemail";
          >
          > // send email
          > if ($distrmailok == "1") {
          > $sendto = $distrimail;
          > mail($sendto, $subject, $message);
          > }
          >
          > if ($reselmailok == "1") {
          > $sendto = $sendto.", ".$dreselma il;
          > mail($sendto, $subject, $message);
          > }
          >
          > $sendto = $sendto.", ".$endusmai l;
          > mail($sendto, $subject, $message);
          > }
          > }
          >
          > loop until next matching record found....
          >
          > ?>[/color]


          Comment

          • Eric Culus

            #6
            Re: mail () breaks loop

            >> $db->open() or die($db->error());[color=blue][color=green]
            >> $db->query("myquery ") or die($db->error());[/color]
            >
            > Don't know what's going on in your $db class, but do you have to assign
            > the
            > result set to something?[/color]

            No, the class is build in such a way that it is not necessary to use
            "$result = "
            [color=blue][color=green]
            >> $db->query("Start another query") or die($db->error());[/color]
            >
            > Does this call to $db overwrite the result set you are looping on - the
            > one
            > you initialise with query("myquery" ) ?[/color]

            This is a query request inside the first loop... simply pulling new data
            from the db when a matching variable is found


            "Mark Rees" <mrees@itsagood price.com> wrote in message
            news:79u2f.1179 4$Nv6.2702@news fe6-win.ntli.net...[color=blue][color=green]
            >> <?
            >>
            >> $db->open() or die($db->error());
            >> $db->query("myquery ") or die($db->error());[/color]
            >
            > Don't know what's going on in your $db class, but do you have to assign
            > the
            > result set to something?
            >
            > e.g. $result=$db->query("Start another query") or die($db->error());
            >[color=green]
            >> $i = 1;
            >> while ($row=$db->fetcharray() ) {
            >> output db data
            >>
            >> if (($myMaintenanc eDate >= $mydateBeginMon th) && ($myMaintenance Date <
            >> $mydateHalfMont h)) {
            >>
            >> $db->query("Start another query") or die($db->error());[/color]
            >
            > Does this call to $db overwrite the result set you are looping on - the
            > one
            > you initialise with query("myquery" ) ?
            >
            >[color=green]
            >> while ($row2=$db->fetcharray() ) {
            >> $productNaam = $row2[0];
            >> $productVersie = $row2[1];
            >> $productType = $row2[2];
            >> }
            >>
            >> Etc... more queries until all data recovered
            >>
            >> //mail
            >> $subject = "mysubject" ;
            >> $message = "mynessage" ;
            >> $sendto = "myemail";
            >>
            >> // send email
            >> if ($distrmailok == "1") {
            >> $sendto = $distrimail;
            >> mail($sendto, $subject, $message);
            >> }
            >>
            >> if ($reselmailok == "1") {
            >> $sendto = $sendto.", ".$dreselma il;
            >> mail($sendto, $subject, $message);
            >> }
            >>
            >> $sendto = $sendto.", ".$endusmai l;
            >> mail($sendto, $subject, $message);
            >> }
            >> }
            >>
            >> loop until next matching record found....
            >>
            >> ?>[/color]
            >
            >[/color]


            Comment

            Working...