每次运行时取消设置 php 值

发布于 2024-12-26 06:43:54 字数 5827 浏览 1 评论 0原文

我正在运行以下脚本,即 phpmailer,在每次运行结束时我希望它清除 $name 的内容,或者 $row->['leadname'] ,我尝试使用 unset at脚本的底部,但这似乎没有效果,我只是从 3 个人的列表中得到信息,其中 2 人说亲爱的 bob ,另一个说亲爱的 {name} ,即使联系人被称为 bob、fred 和温迪。

            <?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('[email protected]', 'Chandlers Oil & Gas');
                            $mail->AddReplyTo('[email protected]', '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;)
            }
            ?>

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('[email protected]', 'Chandlers Oil & Gas');
                            $mail->AddReplyTo('[email protected]', '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("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>';
              } else {
                echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "@", $row["email"]) . ')<br>';
              }
              // Clear all addresses and attachments for next loop
              $mail->ClearAddresses();
              $mail->ClearAttachments();
              unset ($row['leadname']);
                              unset ($name;)
            }
            ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凶凌 2025-01-02 06:43:54

看起来您需要将 body 的内容复制到循环中的新变量中。
目前,您将在第一次运行时覆盖变量,删除 {name} 占位符。

           // 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"]);

您实际上也可以在 str_replace 中使用数组,例如

$newBody = str_replace(array('{name}','{fuel}'),array($name,$fuel),$body);

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);
千仐 2025-01-02 06:43:54

这 ;位置错误。
尝试
取消设置($名称);
而不是
未设置($名称;),

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文