Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:34:03+00:00 2026-06-12T11:34:03+00:00

I am getting Fatal error: Using $this when not in object context when running

  • 0

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";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:34:04+00:00Added an answer on June 12, 2026 at 11:34 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting the old familiar Fatal error: Using $this when not in object context
I am getting a Fatal error: Using $this when not in object context in
Suddenly I started getting the following error: Fatal error: Class 'Mongo' not found in
I'm getting this exception from facebook SDK: Fatal error: Uncaught OAuthException: (#506) Duplicate status
Why am I getting the following error in my code? Fatal error: Can't use
I have question. I have some app on facebook and getting this error Fatal
I am getting a Fatal Error on using a ExecuteNonQuery(). I'm new to working
I am getting a fatal error from my image resize script that takes jpegs
Using Zend FW based Youtube api. Often getting errors on page load Fatal error:
I'm getting Fatal error: Class 'Form_Login' not found in /route/to/project/application/controllers/AuthController.php on line XX when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.