一次对PHPMailer的debug » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

一次对PHPMailer的debug

使用的是PHP 5.4 版。

error_reporting(E_ALL | E_STRICT);

问题:发送出去的邮件只有标题,没有内容和附件。

$success = Email::factory('测试PHPMailer','test content测试中文内容',array('options'=>$options))
        ->from('xxxx@ihacklog.com','荒野无灯')
        ->sender('xxxx@ihacklog.com','荒野无灯')
        ->to('xxxx@xxxx.com','荒野')
        ->attach_file('~/Downloads/colorscheme.png')
        ->send($failed);
        if( !$success )
        {
            var_dump($failed);
        }
        else
        {
            echo 'Mail sent.';
        }

结果显示邮件发送成功,收件方也收到。不过只有标题。。。邮件正文和附件都消失了。
Debug:
在send()函数处下断点,跟踪 $body (CreateBody()函数中的) , $this->error_count , $attachment 并时刻关注其变化。
发现执行完CreateBody()之后, 其局部变量 $body 值变为空。
这里执行的switch子句是case ‘alt_attachments’,

/**
   * Assembles the message body.  Returns an empty string on failure.
   * @access public
   * @return string The assembled message body
   */
  public function CreateBody() {
    $body = '';

    if ($this->sign_key_file) {
      $body .= $this->GetMailMIME();
    }

    $this->SetWordWrap();

    switch($this->message_type) {
      case 'alt':
        $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
        $body .= $this->EncodeString($this->AltBody, $this->Encoding);
        $body .= $this->LE.$this->LE;
        $body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
        $body .= $this->EncodeString($this->Body, $this->Encoding);
        $body .= $this->LE.$this->LE;
        $body .= $this->EndBoundary($this->boundary[1]);
        break;
      case 'plain':
        $body .= $this->EncodeString($this->Body, $this->Encoding);
        break;
      case 'attachments':
        $body .= $this->GetBoundary($this->boundary[1], '', '', '');
        $body .= $this->EncodeString($this->Body, $this->Encoding);
        $body .= $this->LE;
        $body .= $this->AttachAll();
        break;
      case 'alt_attachments':
        $body .= sprintf("--%s%s", $this->boundary[1], $this->LE);
        $body .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
        $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
        $body .= $this->EncodeString($this->AltBody, $this->Encoding);
        $body .= $this->LE.$this->LE;
        $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
        $body .= $this->EncodeString($this->Body, $this->Encoding);
        $body .= $this->LE.$this->LE;
        $body .= $this->EndBoundary($this->boundary[2]);
        $body .= $this->AttachAll();
        break;
    }

//注意这里,这就是邮件正文和附件都消失的地方。。。
    if ($this->IsError()) {
      $body = '';
    } elseif ($this->sign_key_file) {
      try {
        $file = tempnam('', 'mail');
        file_put_contents($file, $body); //TODO check this worked
        $signed = tempnam("", "signed");
        if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) {
          @unlink($file);
          @unlink($signed);
          $body = file_get_contents($signed);
        } else {
          @unlink($file);
          @unlink($signed);
          throw new phpmailerException($this->Lang("signing").openssl_error_string());
        }
      } catch (phpmailerException $e) {
        $body = '';
        if ($this->exceptions) {
          throw $e;
        }
      }
    }

    return $body;
  }

继续跟踪进$body .= $this->AttachAll();这一句,在AttachAll() 中执行完

$mime[] = $this->EncodeFile($path, $encoding);

这一句时,

$this->error_count

值突然由0变为了1 .
继续跟踪进EncodeFile()函数,
执行到

      if (PHP_VERSION < 6) {
        $magic_quotes = get_magic_quotes_runtime();
        set_magic_quotes_runtime(0);
      } 

时debug 直接结束了。。。
应该是由于 set_magic_quotes_runtime 这一句引起的。
set_magic_quotes_runtime
Warning
This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

As of PHP 5 the error reporting constant E_STRICT is available, with the value 2048. When enabled, messages will be issued to warn you about code usage which is deprecated or which may not be future-proof.

Note: E_ALL does not include E_STRICT, so it's not enabled by default. You must explicitly set the error reporting level to include E_STRICT in order to see these messages.

各错误等级详细介绍可见这里:http://www.php.net/manual/en/errorfunc.constants.php
注释掉php5.4中deprecated的函数之后,问题解决。

Tagged in : Debug,PHPMailer

All Comments (0)
Gravatar image
No Comments