2012-10-04 72 views
-1

我遇到致命错误:在运行以下代码时,在不在对象上下文中时使用$ this。我正在编写一个脚本将我的电子邮件转发到另一个帐户。请帮帮我。我是PHP的新手。该错误指向if (is_resource($this->connection))行。但据我所知事情很好。

$hostname = '{imap.xyz.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$password = 'xxxxxxxxx'; 

$connection = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error()); 

function Message_Parse($id) 
{ 
    if (is_resource($this->connection)) 
    { 
     $result = array 
     (
      'text' => null, 
      'html' => null, 
      'attachments' => array(), 
     ); 
    $structure = imap_fetchstructure($this->connection, $id, FT_UID); 

    if (array_key_exists('parts', $structure)) 
    { 
     foreach ($structure->parts as $key => $part) 
     { 
      if (($part->type >= 2) || (($part->ifdisposition == 1) && ($part->disposition == 'ATTACHMENT'))) 
      { 
       $filename = null; 

       if ($part->ifparameters == 1) 
       { 
        $total_parameters = count($part->parameters); 

        for ($i = 0; $i < $total_parameters; $i++) 
        { 
         if (($part->parameters[$i]->attribute == 'NAME') || ($part->parameters[$i]->attribute == 'FILENAME')) 
         { 
          $filename = $part->parameters[$i]->value; 

          break; 
         } 
        } 

        if (is_null($filename)) 
        { 
         if ($part->ifdparameters == 1) 
         { 
          $total_dparameters = count($part->dparameters); 

          for ($i = 0; $i < $total_dparameters; $i++) 
          { 
           if (($part->dparameters[$i]->attribute == 'NAME') || ($part->dparameters[$i]->attribute == 'FILENAME')) 
           { 
            $filename = $part->dparameters[$i]->value; 

            break; 
           } 
          } 
         } 
        } 
       } 

       $result['attachments'][] = array 
       (
        'filename' => $filename, 
        'content' => str_replace(array("\r", "\n"), '', trim(imap_fetchbody($this->connection, $id, ($key + 1), FT_UID))), 
       ); 
      } 

      else 
      { 
       if ($part->subtype == 'PLAIN') 
       { 
        $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID); 
       } 

       else if ($part->subtype == 'HTML') 
       { 
        $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID); 
       } 

       else 
       { 
        foreach ($part->parts as $alternative_key => $alternative_part) 
        { 
         if ($alternative_part->subtype == 'PLAIN') 
         { 
          echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>'; 

          $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID); 
         } 

         else if ($alternative_part->subtype == 'HTML') 
         { 
          echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>'; 

          $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID); 
         } 
        } 
       } 
      } 
     } 
    } 

    else 
    { 
     $result['text'] = imap_body($this->connection, $id, FT_UID); 
    } 

    $result['text'] = imap_qprint($result['text']); 
    $result['html'] = imap_qprint(imap_8bit($result['html'])); 

    return $result; 
     } 

return false; 
} 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'test'; 

$id=1; 
Message_Parse($id); 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
$mail = mail($to, $subject, $result, $headers); 
if($mail){ 
echo "YES"; 
} else{ 
echo "NO"; 
} 
+2

注意错误信息,你不在一个对象内,所以你不能使用$ this。 – Deleteman

+0

是的,我知道了。非常感谢 – ashajf

回答

0

只需将$this->connection更改为$connection即可。 $this仅适用于你在课堂上。另外,您可能需要在函数顶部调用global $connection;。我相信任何没有传入参数的变量都需要调用这个关键字。

看到的“全球关键字”的信息:http://php.net/manual/en/language.variables.scope.php

+0

谢谢。有效。我还有另一个问题。我想在邮件方法中使用函数的结果。 $ results变量。但想知道如何使用它。我现在所做的方式是返回未定义变量的错误消息。请帮忙。 – ashajf

0

$this只存在于作为类的一部分的方法中。 $this不存在于功能或不属于类的任何其他地方。查看关于basic OOP的PHP手册页面,了解更多信息。

如果你想引用全局变量$connection相反,你可以做到这一点通过改变功能的开始:

function Message_Parse($id) 
{ 
    global $connection; 
    if (is_resource($connection)) 
    { 

做不过,请注意使用全局变量是不好的编程习惯用任何语言。

+0

将“全局”建议为解决方案而不是函数参数的不良业力。 – Sven

+0

这是他的代码,不是我们的。我们只是提出如何让他的代码不会失败的建议。 –

+0

@Sven,我并不是说这是一个解决方案,我正在说明如何使用这个变量。新增免责声明。 – rid

0

由于所有的人说,你是不是一个对象里面,这样你就不能使用这个$。

但是你在一个函数内部,所以你也不能访问这个函数以外的任何变量,除非你让它可访问。

该解决方案使用global的作品,但它会带你到地狱,如果你有一天决定你的全局变量$connection应该重命名。

更好的解决方案是将它作为第二个参数传递给你的函数。

function Message_Parse($id)应该成为function Message_Parse($id, $connection)

if (is_resource($this->connection))应该成为if (is_resource($connection))

$id=1; Message_Parse($id);应该成为$id=1; Message_Parse($id, $connection);

完成。

相关问题