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

标签 mysql arrays phpmailer php

我正在使用 phpmailer 发送新闻通讯,它似乎可以正常发送纯文本电子邮件,但由于某种原因,发送 html 电子邮件时会删除人名,而纯文本则可以正常工作并显示姓名。

当它发送电子邮件时,它会将其显示在结果页面上,所以它似乎没有获取名称,但我不明白为什么?。

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,我得到:亲爱的,这是您的 {fuel} 价格: 通过纯文本我得到:亲爱的鲍勃,这是您的 {fuel} 价格:

下面的代码看起来没问题吗?

              <?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 />";
              }

              ?>

最佳答案

您的代码:

$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);

关于mysql - Phpmailer 在 html 电子邮件中删除名称值,但使用纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825090/

相关文章:

php - 使用AJAX从MySQL数据库更新PHP页面:用于计算最近添加的数据的算法

php - summernote + PHP 邮件程序

java - 如何在showMessageDialog中打印双二维数组?

javascript - 如何使用 javascript 添加特定对象?

phpmailer - 在 "To"标题中使用 php mailer 时在邮件中显示两次相同的地址

phpmailer - SMTP -> 错误 : RCPT not accepted from server

php - 有没有办法在从插入时间算起的一定时间后从 mysql 中删除一行

mysql - peewee:配置不当错误

创建数据库时 MySQL 错误 #1089

c++ - 对结构数组使用构造函数