-2

I am running the following script, which is phpmailer, at the end of each for each run I want it to clear the contents of $name , either that or $row->['leadname'] , I have tried using unset at the bottom of the script but this seems to have no effect and I am just getting from a list of 3 people 2 of them saying dear bob , and the other saying dear {name} , even though the contacts are called, bob, fred and wendy.

            <?php

                 $formid = mysql_real_escape_string($_GET[token]);
            $templatequery = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addmailinglistmessage WHERE cf_id = '$formid'") or die(mysql_error());
            $templateData = mysql_fetch_object($templatequery);

            $gasoiluserTemplate = $templateData->gasoilusers;
            $dervuserTemplate = $templateData->dervusers;
            $kerouserTemplate = $templateData->kerousers;
            $templateMessage = $templateData->mailinglistgroupmessage;
            $templatename = $templateData->mailinglistgroupname;
                                ?>  
                    <?php
            require_once('./send/class.phpmailer.php');
            //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

            $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

            // $body                = file_get_contents('contents.html');

            $body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <style>#title {padding-left:120px;padding-top:10px;font-family:"Times New Roman", Times, serif; font-size:22px; font-weight:bold; color:#fff;}</style>
            </head>

            <body>
            <div style="background:
                                              none repeat scroll 0% 0% rgb(6, 38,
                                              97); width:780px;">
            <img id="_x0000_i1030" style="padding-left:100px;padding-right:100px;"
                                                  src="http://www.chandlersoil.com/images/newsletter/header.gif"
                                                  alt="Chandlers Oil and Gas"
                                                  border="0" height="112"
                                                  width="580">
                                                  <div id="title">{message}</div>

                                                  </div>
            </body>
            </html>
            ';

                            // $body = preg_replace('/\\\\/i', $body);

                            $mail->SetFrom('crea@cruiseit.co.uk', 'Chandlers Oil & Gas');
                            $mail->AddReplyTo('crea@cruiseit.co.uk', 'Chandlers Oil & Gas');

                            $mail->Subject       = "Your Fuel Prices From Chandlers Oil & Gas";

                            $query  = "SELECT leadname,businessname,email FROM hqfjt_chronoforms_data_addupdatelead WHERE keromailinglist='$kerouserTemplate' AND dervmailinglist='$dervuserTemplate' AND gasoilmailinglist='$gasoiluserTemplate'";
                            $result = mysql_query($query);

                            // Bail out on error 
            if (!$result)  
              { 
                trigger_error("Database error: ".mysql_error()." Query used was: ".htmlentities($query), E_USER_ERROR); 
                die();
               }                                    

            while ($row = mysql_fetch_array ($result)) {
              $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";

               // THIS PULLS THE CLIENTS FIRST NAME OUT ON EACH LOOP
               $firstname = $row["leadname"];

               //THIS PULLS THE CLIENTS BUSSINESS NAME OUT ON EACH LOOP
               $businessname = $row["businessname"];

               // IF THE FIRST NAME FIELD IS BLANK USE THE BUSINESS NAME INSTEAD
               if ($firstname = '')
               {$name = $row["businessname"];}
               else 
               {$name = $row["leadname"];}

               // THIS REPLACES THE {NAME} IN THE PULLED IN TEMPLATE MESSAGE WITH THE CLIENTS NAME DEFINED IN $name
               $body = str_replace('{name}', $name, $body);

               // THIS REPLACES {fuel} IN THE PULLED IN TEMPLATE WITH THE TEMPLATE NAME (WHICH IS THE TYPE OF FUEL)
               $body = str_replace('{fuel}', $templatename, $body); 

               // THIS REPLACES THE {message} IN THE $body ARRAY WITH THE TEMPLATE MESSAGE HELD IN $templateMessage
               $body = str_replace('{message}', $templateMessage, $body);


              $mail->MsgHTML($body);
              $mail->AddAddress($row["email"], $row["leadname"]);




              if(!$mail->Send()) {
                echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>';
              } else {
                echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "&#64;", $row["email"]) . ')<br>';
              }
              // Clear all addresses and attachments for next loop
              $mail->ClearAddresses();
              $mail->ClearAttachments();
              unset ($row['leadname']);
                              unset ($name;)
            }
            ?>
Iain Simpson
  • 441
  • 4
  • 13
  • 29
  • My last question was related to the same script but a different command, I am trying to use unset to get rid as the last solution didnt work – Iain Simpson Jan 11 '12 at 11:42

2 Answers2

2

It looks like you need to copy the contents of body into a new variable within your loop. Currently you're overwriting the variable on the first run, removing the {name} placeholders.

           // THIS REPLACES THE {NAME} IN THE PULLED IN TEMPLATE MESSAGE WITH THE CLIENTS NAME DEFINED IN $name
           $newBody= str_replace('{name}', $name, $body);

           // THIS REPLACES {fuel} IN THE PULLED IN TEMPLATE WITH THE TEMPLATE NAME (WHICH IS THE TYPE OF FUEL)
           $newBody = str_replace('{fuel}', $templatename, $newBody); 

           // THIS REPLACES THE {message} IN THE $body ARRAY WITH THE TEMPLATE MESSAGE HELD IN $templateMessage
           $newBody = str_replace('{message}', $templateMessage, $newBody);


          $mail->MsgHTML($newBody);
          $mail->AddAddress($row["email"], $row["leadname"]);

Also you can actually use arrays in str_replace, e.g.

$newBody = str_replace(array('{name}','{fuel}'),array($name,$fuel),$body);
Paul Bain
  • 4,364
  • 1
  • 16
  • 30
  • Hi, I tried using this as suggested by someone else, but it doesnt solve the problem >> $bodyTemp = str_replace('{name}', $name, $body); $bodyTemp = str_replace('{fuel}', $templatename, $bodyTemp); – Iain Simpson Jan 11 '12 at 11:44
  • I'm not familiar with PHPMailer, it looks like it maps to the PHP mail functions, however it's object may maintain the content. Why not try using PHP's mail() function? See http://php.net/manual/en/function.mail.php#example-3073 – Paul Bain Jan 11 '12 at 11:47
0

the ; is in a wrong position. Try unset($name); instead of unset($name;),

Kispy
  • 3
  • 1
  • I tried this, but still no go, it doesnt seem to want to unset the value back to its defualt, I thought thats what this command was meant to do ?. – Iain Simpson Jan 11 '12 at 11:46