名字字段将被插入电子邮件两次

时间:2012-01-12 19:11:06

标签: php mysql html preg-replace str-replace

我正在使用以下代码发送电子邮件,我遇到的问题是,当我发送电子邮件时,人员名称导入就好了,只要燃料类型,但有些原因似乎是拉动在$ name值中两次。

例如,我收到了这样的电子邮件:

亲爱的Tom Sawyer Tom Sawyer,这是你的Derv价格:'

亲爱的辛普森威尔辛普森,这是你的Derv&天然气价格'

以下是我的价值观的快速解释: $ templatename =电子邮件模板,它在其中存储了向客户发送的消息,以及名为{name}的大括号和另一个名为{fuel}的大括号。

{name}被取出$ name并替换为人名,{fuel}拉入模板的名称,这是所用燃料的名称。这是通过str_replace函数完成的。

所以结果应该是亲爱的,PERSONS NAME,这是您的FUEL TYPE价格,但我实际得到的是由于某种原因重复两次的人名?

              <?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;


                require_once('./send/class.phpmailer.php');

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

               // Grab the FreakMailer class
                require_once('./send/MailClass.inc');

                // Grab our config settings
                require_once('./send/config.php');

              // Setup body
              $htmlBody = '<!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 {text-align:center;font-family:"Times New Roman", Times, serif; font-size:130% !important; font-weight:bold; color:#fff;} .address {color:#fff; font-size:60%; font-family:Verdana, Geneva, sans-serif;}</style>
                              </head>

                              <body>
                              <div style="background:
                                                                none repeat scroll 0% 0% rgb(6, 38,
                                                                97); width:650px; height:auto;">
                              <img id="_x0000_i1030" style="padding-left:5%;padding-right:5%"
                                                                    src="http://www.chandlersoil.com/images/newsletter/header.gif"
                                                                    alt="Chandlers Oil and Gas"
                                                                    border="0" height="112"
                                                                    width="580">
                                                                    <div id="title">' . $templateMessage . '</div>
                                                                    <div style="background:#344ea2; width:501px; height:65px; margin-left:70px;"></div>

                                                                    <div style="background:#13155C; height:30px; width:501px; margin-left:70px;"></div>

                                                                    <div style="background:#fff; width:501px; height:365px; margin-left:70px;"></div>

<div style="background:
                                                                none repeat scroll 0% 0% rgb(6, 38,
                                                                97); height:60px; width:501px; margin-left:70px;"></div>    

                                                                <div style="background:#000139;height:100px; width:580px; margin-left:35px;" >
                                                                  <table cellspacing="0" cellpadding="0" class="address" style="margin-left:5px;">
                                                                    <tr>
                                                                      <td valign="top" width="21%"><p><strong>Chandlers                                                    Depots</strong></p>
                                                                        <table class="address" border="0" cellpadding="0" cellspacing="0" width="87%">
                                                                          <tbody>
                                                                            <tr>
                                                                              <td width="57%"><p>Grantham</p></td>
                                                                              <td width="43%"><p>Spalding</p></td>
                                                                            </tr>
                                                                            <tr>
                                                                              <td><p>Lincoln</p></td>
                                                                              <td><p>Corby</p></td>
                                                                            </tr>
                                                                            <tr>
                                                                              <td><p> Spilsby</p></td>
                                                                              <td><p>Retford</p></td>
                                                                            </tr>
                                                                          </tbody>
                                                                        </table></td>
                                                                      <td valign="top" width="79%"><p><strong>Address<br />
                                                                        <br />
                                                                      </strong>Chandlers                                                  Oil &amp; Gas, Warren                                                  Way, Alma Park,                                                  Grantham, Lincolnshire,                                                  NG31 9SE<br />
                                                                        <br />
                                                                        <strong>TEL: </strong>08456                                                  202010 <strong>FAX:</strong> 01476                                                  568147 <strong>E-Mail: </strong><a href="mailto:websitesales@chandlersoil.com">websitesales@chandlersoil.com</a></p>
                                                                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                                                          <tbody>
                                                                            <tr>
                                                                              <td><p align="right"> </p></td>
                                                                            </tr>
                                                                          </tbody>
                                                                        </table></td>
                                                                    </tr>
                                                                  </table>
                                                                </div>                                                              
                              </div>
                              </body>
                              </html>
                              ';
         $textBody = "$templateData->mailinglistgroupmessage";


              // instantiate the class
              $mailer = new FreakMailer();

              // Get the user's Email
              $sql = mysql_query("SELECT leadname,businessname,email,mailtype FROM hqfjt_chronoforms_data_addupdatelead WHERE keromailinglist='$kerouserTemplate' AND dervmailinglist='$dervuserTemplate' AND gasoilmailinglist='$gasoiluserTemplate'");

              while($row = mysql_fetch_object($sql))
              {
                  // Send the emails in this loop.
                  $name = $row->leadname;
                   $businessname = $row->businessname;
                    $to_email = $row->email;
                    $mailtype = $row->mailtype;

                  if(!empty($row->businessname))
                  {
                      $name .= ' '.$row->leadname;
                  }

                  $to_name = $name;

                  if($row->mailtype == 'html')
                  {
                     $mailer->Body = str_replace(array('{name}', '{fuel}'), array($name, $templatename), $htmlBody);
                      $mailer->isHTML(true);
                      $mailer->AltBody = str_replace(array('{name}', '{fuel}'), array($name, $templatename), $textBody);
                      $mailer->AddAddress($to_email, $name);
                      $mailer->Subject = "Your Fuel Prices From Chandlers Oil & Gas";
                      $mailer->FromName = "Chandlers Oil & Gas";
                  }
                  else
                  {
                      $mailer->Body = str_replace(array('{name}', '{fuel}'), array($name, $templatename), $textBody);
                      $mailer->isHTML(false);
                      $mailer->Subject = "Your Fuel Prices From Chandlers Oil & Gas";
                      $mailer->FromName = "Chandlers Oil & Gas";
                          $mailer->AddAddress($to_email, $name);                                  
                  }

                  $mailer->Send();
                  $mailer->ClearAddresses();
                  $mailer->ClearAttachments();
                  $mailer->IsHTML(false);
                  echo "Mail sent to: $name - $to_email<br />";
              }

              ?>

2 个答案:

答案 0 :(得分:2)

我想我在这里看到了这个:

                  if(!empty($row->businessname))
                  {
                      $name .= ' '.$row->leadname;
                  }

不是吗?

                  if(!empty($row->businessname))
                  {
                      $name .= ' '.$row->businessname;
                  }

:)

非常简单,这是根据所需条件连接不需要的变量。

答案 1 :(得分:1)

变化:

              if(!empty($row->businessname))
              {
                  $name .= ' '.$row->leadname;
              }

要:

              if(!empty($row->businessname))
              {
                  $name .= ' '.$row->businessname;
              }