2012-01-12 3 views
1

phpmailer를 사용하고 있으며 필요에 맞게 사용자 정의했습니다. $ templatemessage 내부는 {name} 및 {fuel} 필드가있는 고객에게 보내는 메시지입니다.하나 이상의 str_replace 인스턴스가 PHP 코드를 깨뜨림

이 두 필드는 str_replace 함수로 대체됩니다.

이 문제가 나타나기 전에 str_replace가 {name}을 (를) 대체했으며이 위치로 가져 오는 사람의 이름과 잘 일치했습니다. 여분의 str_replace 함수를 추가하여 {fuel}를 $ templatemessage 값으로 대체 할 때 문제가 발생했습니다. 이제는 {fuel} 값을 가져 오지만 {name} 함수에 더 이상 이름을 가져 오지 않고 전자 메일에 {name} 만 표시하는 것처럼 보입니다. 이전에 { 연료} 그것은 효과가 있었다. 두 사람이 어떻게 서로 잘 어울리게 할 수 있습니까?

    <?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('{name}', $name, $htmlBody); 
     // the line below is the new one I have added 
         $mailer->Body = str_replace('{fuel}', $templatename, $htmlBody); 
         $mailer->isHTML(true); 
         $mailer->AltBody = str_replace('{name}', $name, $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('{name}', $name, $textBody); 
    // the line below is the new one I have added 
         $mailer->Body = str_replace('{fuel}', $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 />"; 
       } 

       ?> 

답변

3

나는이 시도하여 시작할 것 : 검색하고, 배열이 교체되면 docs

에서

$mailer->Body = str_replace(array('{name}', '{fuel}'), array($name, $templatename), $textBody); 

는, str_replace()는 각각의 배열 및 용도에서 값 소요 그들을 검색하고 주제를 대체 할 수 있습니다. 대체 값이 검색 값보다 적 으면 대체 문자열에 빈 문자열이 사용됩니다. 검색이 배열이고 replace가 문자열이면이 대체 문자열은 검색의 모든 값에 사용됩니다. 그러나 대화는 의미가 없습니다.

검색 또는 바꾸기가 배열 인 경우 해당 요소는 먼저 까지 처리됩니다. 에서는 HTMLBody

//Here you set $mailer->Body = $htmlBody (while replacing {name}) 
$mailer->Body = str_replace('{name}', $name, $htmlBody); 
// the line below is the new one I have added 
//Here you set $mailer->Body = $htmlBody (while replacing {fuel}) 
$mailer->Body = str_replace('{fuel}', $templatename, $htmlBody); 

문제는 당신이해야 할 일을 두 번째 str_replace()에 $ mailer-> 바디를 재정의되어 사용의 $의 mailer-> 바디 대신 $의이다 :

+0

당신의 도움에 감사드립니다. 완벽하게 작동합니다. 그냥 templatename을 $ templame으로 tweek 할 필요가 있었지만 위와의 작업 :-) –

3

문제는 코드입니다

$mailer->Body = str_replace('{fuel}', $templatename,$mailer->Body); 

더 나은 솔루션은 위에서 게시 된 (않는 str_replace 배열을 사용하여())하지만 난 당신의 코드에서 실제 버그를 지적하고 싶다고 할 것이다.