7

Possible Duplicate:
Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

I got the following error when executing my code.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

Tried the ini_set('memory_limit', '128M'); as well but didn't work. The memory limit in my php.ini file is 128MB. Pleas help.

I am adding the code. I am new to php and please help me in solving this problem.

<?php
require_once 'lib/swift_required.php';
$hostname = '{imap.abc.com:993/imap/ssl}INBOX';
$username = 'username';
$password = 'password';

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


function Message_Parse($id)

{

global $connection;

    if (is_resource($connection))
    {
        $result = array
        (
            'text' => null,
            'html' => null,
            'attachments' => array(),
        );


                $structure = imap_fetchstructure($connection, $id, FT_UID);
        //print_r($structure);
//array_key_exists — Checks if the given key or index exists in the array
        if (is_array($structure) && 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($connection, $id, ($key + 1), FT_UID))),
                    );
                }

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

                    else if ($part->subtype == 'HTML')
                    {
                        $result['html'] = imap_fetchbody($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($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($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                            }
                        }
                    }
                }
            }
        }

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

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

        return $result;
        //echo $result;
    }

    return false;
}
 $emails = imap_search($connection,'ALL');
rsort($emails);

 foreach($emails as $email_number) {

$result = Message_Parse($email_number);
//print_r($structure);
   // echo $result['text'];
    //echo $result['html'];
    //echo $result['attachments'];

// Create the Transport
$transport = Swift_MailTransport::newInstance();
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Bismillahhirrahmanirraheem')
  ->setFrom(array('as@aaa.com' => 'jf faz'))
  ->setTo(array('acc@aa.com'))
  ->setBody($result['text'], 'Here is the message itself');

$result1 = $mailer->send($message);


?>
Community
  • 1
  • 1
ashajf
  • 169
  • 3
  • 4
  • 9
  • What did you try, some code example. – Dmytro Zarezenko Oct 05 '12 at 16:46
  • have you tried increasing the memory limit more? then memory size PHP is trying to allocate is 134mb – jordanandree Oct 05 '12 at 16:46
  • 2
    I expect your code is going into an infinite loop. Could you add your code to the question? – andrewsi Oct 05 '12 at 16:46
  • 1
    Why do you have your function definition inside your while loop? – andrewsi Oct 05 '12 at 16:55
  • Why do you call `Message_Parse` twice? First you call it but ignore the value, then you call it again and assign to `$result`. – Barmar Oct 05 '12 at 17:11
  • My guess is that some code you're not showing us is saving each message in an array, so when you have too many large messages it fills up memory. Make sure that once you finish processing a message, you drop all references to it so the memory can be GCed. – Barmar Oct 05 '12 at 17:12
  • @andrewsi I did it because I had to call the email number to parse as the argument. yes it is wrong. I have removed the while loop and added it at the bottom where I call the function. But still i am getting it. – ashajf Oct 05 '12 at 17:20
  • @Barmar I have changed the call of function twice as well. Thanks for pointing out that mistake. but still i am getting it. I don't have any other code, except the swift mailer library. – ashajf Oct 05 '12 at 17:23
  • I wonder if the imap library is trying to download everything. How big is the biggest message with attachments? – Barmar Oct 05 '12 at 17:30
  • A k. I've got it. There was an 5MB sized audio file and it has caused the issue. I removed it and now got rid of that error. Thank you. – ashajf Oct 05 '12 at 17:42
  • In my opinion **this is not a duplicate**. The error message says that the memory is insufficient to allocate 134217728 bytes, but also that _tried to allocate 32 bytes_ which is less than the limit. This seems to be a different kind of issue. – ant0nio Aug 20 '19 at 09:28

2 Answers2

21

Well try ini_set('memory_limit', '256M');

134217728 bytes = 128 MB

Or rewrite the code to consume less memory.

ficuscr
  • 6,634
  • 2
  • 28
  • 47
  • setting this to 256M takes more time and give maximum execution time exceeded error – ashajf Oct 05 '12 at 16:48
  • 2
    Should use an increased timeout with `set_time_limit()` then as well. But reading edits sounds more like an issue of design. But, if its kosher and just takes a bit more than 128 MB and a bit longer than 30 seconds to run, then what I said. – ficuscr Oct 05 '12 at 16:56
7

128M == 134217728, the number you are seeing.

The memory limit is working fine. When it says it tried to allocate 32 bytes, that the amount requested by the last operation before failing.

Are you building any huge arrays or reading large text files? If so, remember to free any memory you don't need anymore, or break the task down into smaller steps.

Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
  • Yes. i am trying to forward emails with attachments. May be i think that's why this issue arises. I am new to php an don't know how to rewrite them. Please help me. – ashajf Oct 05 '12 at 17:02