[SOLVED] Allowed memory size of 8388608 bytes exhausted

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

[SOLVED] Allowed memory size of 8388608 bytes exhausted

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Allowed memory size of 8388608 bytes exhausted

Post 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).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's a 2MB file in an 8MB memory environment. ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Allowed memory size of 8388608 bytes exhausted

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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();
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
Post Reply