Phpmailer在html电子邮件上删除名称值,但使用纯文本?

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

标签: mysql arrays phpmailer php

我正在使用phpmailer发送简报,它似乎可以解决并发送纯文本电子邮件确定,但由于某些原因,当发送HTML电子邮件时丢弃了人名,其中纯文本工作正常并显示名称。< / p>

当它发送电子邮件时,它会在结果页面上显示它,所以它似乎没有得到这个名字,但我不明白为什么?。

Mail sent to: - crea@cruiseit.co.uk
 Mail sent to: Toms Tackle Tom Sawyer - crea@cruiseit.co.uk
Mail sent to: - crea2k@o2.co.uk

使用HTML我得到:亲爱的,这是您的{燃料}价格: 我得到明文:亲爱的鲍勃,这是你的{燃油}价格:

以下代码看起来不错吗?

              <?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 {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">" . $templateMessage . "</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;
                   $name = $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);
                      $mailer->IsHTML(true);
                      $mailer->AltBody = str_replace('{name}', $name, $textBody);
                      $mailer->AddAddress($to_email, $name);
                  }
                  else
                  {
                      $mailer->Body = str_replace('{name}', $name, $textBody);
                      $mailer->isHTML(false);
                          $mailer->AddAddress($to_email, $name);
                  }
                  $mailer->Send();
                  $mailer->ClearAddresses();
                  $mailer->ClearAttachments();
                  $mailer->IsHTML(false);
                  echo "Mail sent to: $name - $to_email<br />";
              }

              ?>

2 个答案:

答案 0 :(得分:1)

您的代码:

$name = $row->leadname;
$name = $row->businessname;

...这导致我假设你的变量被覆盖....

这是一个很小的代码部分,如果你在更精细的代码中遇到它,而你不知道变量在哪里被改变,你可以使用这个小技巧

class WhereIsThisAltered{
     protected $value;
     function __construct($value){
          $this->value = $value;
     }
     function __toString(){
         return $this->value;
     }
     function __destruct(){
         echo "Instance with value:{$this->value} desctructed";
         debug_print_backtrace();
     }
}

$name = new WhereIsThisAltered($row->leadname);

答案 1 :(得分:0)

你正在混合单引号和双引号。改变这一行:

<div id="title">" . $templateMessage . "</div>

<div id="title">' . $templateMessage . '</div>