Page 1 of 1

[SOLVED] Allowed memory size of 8388608 bytes exhausted

Posted: Mon Jul 09, 2007 3:12 am
by JayBird
Why would i get the following error when trying to send a PDF file that is 2,095,438 bytes

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 2867818 bytes) in /var/www/html/includes/swift/Swift.php on line 503

I'm using version 1.1.2-php4

Yes, i know i should update, but i don't wanna have to update any code...it's only for a simple formail with attachment. Unless you tell me i can update without any issues.

Posted: Mon Jul 09, 2007 6:25 am
by superdezign
The allowed memory size isn't caused by physical data, but by using too many resources by running VERY hefty scripts or infinite loops. It's likely an issue of efficiency.

Re: Allowed memory size of 8388608 bytes exhausted

Posted: Mon Jul 09, 2007 8:44 am
by Chris Corbyn
JayBird wrote:Why would i get the following error when trying to send a PDF file that is 2,095,438 bytes

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 2867818 bytes) in /var/www/html/includes/swift/Swift.php on line 503

I'm using version 1.1.2-php4

Yes, i know i should update, but i don't wanna have to update any code...it's only for a simple formail with attachment. Unless you tell me i can update without any issues.
You're not using the cache. You can't send an 8MB attachment just because you have 8MB memory. Swift has to do a lot of encoding and caching in memory so the 8MB soon disappears. Use the Disk cache and all troubles will be gone (I sent 100MB attachments with the disk cache and only hit ~1MB of PHP's memory doing so).

Posted: Mon Jul 09, 2007 8:46 am
by Chris Corbyn
Ah, you're screwed if you're not using v3. This was one of the big reasons for shifting to v3. Versions 1 and 2 didn't deal with large attachments very well at all.

Posted: Mon Jul 09, 2007 8:47 am
by feyd
It's a 2MB file in an 8MB memory environment. ;)

Re: Allowed memory size of 8388608 bytes exhausted

Posted: Mon Jul 09, 2007 8:48 am
by superdezign
JayBird wrote:Yes, i know i should update, but i don't wanna have to update any code...it's only for a simple formail with attachment. Unless you tell me i can update without any issues.
BTW, PHP5 is backwards-compatible for PHP 4 applications. The only things you'll need to change are if you have constants with reserved word names (private, protected, etc.). You should also upgrade Swiftmailer... d11's on v3 already.

Posted: Mon Jul 09, 2007 8:55 am
by JayBird
Damn, i guess i will have to upgrade. Didn't want to spend any time on this little app if i didnt need to. I use the latest version on all other project i do. THis was just something i threw together ages ago.

Chris, can i just drop the v3 stuff in without changing my current code (below)

Code: Select all

$mailer = new Swift(new Swift_SMTP_Connection('mail.xxxx.com'));
			
				//Add attachments as raw strings
				foreach($fileUpload->uploadedFiles as $file)
				{
					//echo chunk_split(base64_encode(file_get_contents($fileUpload->uploadDir.$file['name'])));
					$mailer->addAttachment(file_get_contents($fileUpload->uploadDir.$file['name']), $file['name'], $file['mime']);
				}
			
			//If anything goes wrong you can see what happened in the logs
			if (!$mailer->hasFailed())
			{
				//Add as many parts as you need here
				$plainText 	= "Name: ".$_POST['fullName']."\nEmail: ".$_POST['emailAddress'];
				$htmlText	= "<strong>Name:</strong> ".$_POST['fullName']."<br /><strong>Email:</strong> ".$_POST['emailAddress'];
				
				$mailer->addPart($plainText);
				$mailer->addPart($htmlText, 'text/html');
				
				//Leaving the body out of send() makes the mailer send a multipart message
				$mailer->send(
					'xxx@xxx.com',
					'"'.$_POST['fullName'].'" <'.$_POST['emailAddress'].'>',
					'xxxxxxx'
				);
				
				$mailer->close();
			}
			else echo "The mailer failed to connect. Errors: ".print_r($mailer->errors, 1).". Log: ".print_r($mailer->transactions, 1);

Posted: Mon Jul 09, 2007 9:42 am
by Chris Corbyn
You'll have to change your code sorry. To be honest, we're talking about a 5 minute job though ;)

Code: Select all

Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp"); //or somewhere else
$mailer =& new Swift(new Swift_SMTP_Connection('mail.xxxx.com'));
$message =& new Swift_Message('xxxxxxx');
$plainText = "Name: ".$_POST['fullName']."\nEmail: ".$_POST['emailAddress'];;
$htmlText = "<strong>Name:</strong> ".$_POST['fullName']."<br /><strong>Email:</strong> ".$_POST['emailAddress'];
$message->attach(new Swift_Message_Part($htmlText, "text/html"));
$message->attach(new Swift_Message_Part($plainText));

 //Add attachments as raw strings
foreach($fileUpload->uploadedFiles as $file)
{
  $message->attach(new Swift_Message_Attachment(
    new Swift_File($fileUpload->uploadDir.$file['name']), $file['name'], $file['mime']));
}
                       
if (!$mailer->send($message, $to, $from))
{
  echo "Sending failed";
}
                      
$mailer->disconnect();

Posted: Mon Jul 09, 2007 9:57 am
by JayBird
Cool, thanks Chris.

Yeah, as i said, it is something small i didn't wanna spend any time on, but as you have highlighted, it is a quick fix and i will upgrade right away.

Thanks again