Search code examples
phpmime

Fatal error: Using $this when not in object context error


I am getting Fatal error: Using $this when not in object context when running this below code. I am writing a script to forward my emails to another account. Please help me. I am a newbie to php. The error points the if (is_resource($this->connection)) line. but as far as I know things are fine there.

$hostname = '{imap.xyz.com:993/imap/ssl}INBOX';
$username = 'aaaaa@xyz.com';
$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      = 'aaaa@gmail.com';
$subject = 'the subject';
$message = 'test';

$id=1;
Message_Parse($id);
$headers = 'From: aaa@xyz.com' . "\r\n" .
    'Reply-To: aaa@xyz.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$mail = mail($to, $subject, $result, $headers);
if($mail){
echo "YES";
} else{
echo "NO";
}

Solution

  • Just change $this->connection to $connection. $this only applies when you're in a class. Also, you may need to call global $connection; at the top of your function. I believe any variables that aren't passed in to the parameters need this keyword called.

    See information on "global keyword" at: http://php.net/manual/en/language.variables.scope.php