-1

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";
}
ashajf
  • 199
  • 3
  • 4
  • 9

3 Answers3

0

$this only exists inside methods that are part of classes. $this does not exist in functions or anywhere else that is not part of a class. Take a look at the PHP manual page about basic OOP for more about this.

If you want to refer to the global variable $connection instead, you can do that by changing the beginning of the function to:

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

Do note though that using global variables is bad programming practice in any language.

rid
  • 61,078
  • 31
  • 152
  • 193
  • Bad karma for suggesting "global" as a solution instead of a function parameter. – Sven Oct 04 '12 at 18:25
  • It's his code, not ours. We're only making suggestions for how to make his code not fail. – Adam Plocher Oct 04 '12 at 18:27
  • @Sven, I'm not suggesting that as a solution, I'm illustrating how that variable can be used. Added disclaimer. – rid Oct 04 '12 at 18:35
0

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

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
  • Thank you. It worked. I have another problem as well. I wanted to use the results of the function in the mail method. the $results variable. But wondering how to use it. The way which I have done now is returning an error message undefined variable. Please help. – ashajf Oct 05 '12 at 02:43
0

As all others said, you are not inside an object, so you cannot use $this.

But you are inside a function, so you also cannot access any variables outside of this function unless you make it accessible.

The solution using global works, but it will bring you to hell if you someday decide that your global variable $connection should be renamed.

A better solution is to pass it as a second parameter to your function.

function Message_Parse($id) should become function Message_Parse($id, $connection)

if (is_resource($this->connection)) should become if (is_resource($connection))

$id=1; Message_Parse($id); should become $id=1; Message_Parse($id, $connection);

Done.

Sven
  • 69,403
  • 10
  • 107
  • 109