PHPMailer function set_magic_quotes_runtime() is deprecated

时间:2019-04-17
本文章向大家介绍PHPMailer function set_magic_quotes_runtime() is deprecated,主要包括PHPMailer function set_magic_quotes_runtime() is deprecated使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

测试代码

$mailer = new Mailer();
$mailer->FromName = 'BigData Tech Team';
$mailer->Subject = "【报警提醒】广告主[{$adv_id}]充值异常";
$body = "广告主:{$adv_id}<br>";

$mailer->MsgHTML($body);
$mailer->AddAddress('bigdate-adsta@sohu-inc.com');
$mailer->AddAddress('clarechen@sohu-inc.com');
$mailer->AddAttachment('/tmp/test.txt');
$mailer->Send();
$mailer->ClearAddresses();

结果正文信息正常,无附件信息,然后使用$mailer->ErrorInfo打印错误信息,提示
function set_magic_quotes_runtime() is deprecated

方法在php 5.3后弃用,查看具体调用的地方,发现

private function EncodeFile($path, $encoding = 'base64') {
  try {
    if (!is_readable($path)) {
      throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE);
    }
    if (function_exists('get_magic_quotes')) {
      function get_magic_quotes() {
        return false;
      }
    }
    if (PHP_VERSION < 6) {
      $magic_quotes = get_magic_quotes_runtime();
      set_magic_quotes_runtime(0);
    }
    $file_buffer  = file_get_contents($path);
    $file_buffer  = $this->EncodeString($file_buffer, $encoding);
    if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); }
    return $file_buffer;
  } catch (Exception $e) {
    $this->SetError($e->getMessage());
    return '';
  }
}

在上述方法中调用,但是PHP_VERSION < 6 判断有问题,即 字符串 ‘5.6.3’ < 6 为true
所以导致这个问题出现。

鉴于无法修改PHP_VERSION, 故引入swiftmailer扩展支持当前业务。