2012-01-12 93 views
0

我正在使用下面的代碼發送電子郵件,我遇到的問題是當我發送電子郵件時,人員名稱只要導入燃料類型就好,但有些理由似乎是兩次拉入$ name值。名字字段被插入到電子郵件中兩次

例如,我得到這樣的電子郵件發送:

「親愛的湯姆·索亞湯姆·索亞,這裏是你的柴油價格:」

「親愛的威爾·辛普森威爾·辛普森,這是你們的柴油&天然氣石油價格'

以下是對我的價值的一個快速解釋: $ templatename =電子郵件模板,它存儲在消息中,發送給客戶,加上一個大括號{name}和另一個名爲{汽油}。

{name}被拉出$ name並替換爲人名,{fuel}拉取模板名稱,這是所用燃料的名稱。這是通過str_replace函數完成的。

所以結果應該是親愛的,人名,這裏是你的燃料類型的價格,但我實際得到的是由於某種原因重複兩次人名?

   <?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:[email protected]">[email protected]</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

我想我該在這裏看到:

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

不要你的意思是?

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

:)

非常簡單地,這是級聯基於所需條件不希望的變量。

+0

什麼是應該做的是,如果商家名稱欄是空白的,它應該使用名字字段代替,最好應該shomething像....... if businessname ==''$ name = $ salutation&$ leadname else $ name = $ businessname 但是我不確定如何編碼在那裏的稱呼和主角名稱,例如,如果公司領域是空白使用MR JONES,如果不是空白使用JONES CORP。 – 2012-01-12 19:27:27

+0

遠高於此之後你獲取對象,你已經設置了$ name字段並且已經填充了$ row-> leadname,並且在這裏你的基本說法是「如果businessname不是空白的,那麼請在其中放置另一個$ row->主角名稱」 。=你不再替代,你的修正,所以在我的答案中的替換代碼你得到像「瓊斯瓊斯公司」的東西。那麼當businessname不是空的時候,你的說法是什麼,使用businessname而不是leadname權限?或者我錯了? – RedactedProfile 2012-01-12 19:37:10

+0

Ahh我想我可能錯過了編碼它開始,我的意思是如果主角是空白的,應該使用商業名稱,而不是空白,請使用saluteation,leadname。 – 2012-01-12 19:50:51

1

變化:

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

要:

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

它應該做的是如果商業名稱字段是空白的,它應該使用firstname字段,而不是理想情況下它應該是像.......的東西如果businessname ==''$ name = $ salutation&$ leadname else $ name = $ businessname 但我不確定如何在那裏編寫致敬和主角名稱,例如,如果company字段爲空白,請使用MR JONES,如果它不是空白使用JONES CORP。 – 2012-01-12 19:27:33

+0

然後讓如果'$ name ='而不是'$ name。='... – Wrikken 2012-01-12 19:44:28

+0

現在明白了,感謝您的幫助:-) – 2012-01-12 20:07:27