File attachment size limits php mail() or mime function

the mail() function

Moderators: egami, macek, gesf

Post Reply
User avatar
New php-forum User
New php-forum User
Posts: 15
Joined: Tue Nov 20, 2007 6:27 pm
Location: Maryland, USA

Tue Sep 09, 2008 7:51 am

Hello all,

I am receiving a memory error: Allowed memory size of 33554432 bytes exhausted

This error occurs when trying to attach a backup file to the email message. The size of the backup file is 8MB, and here is the code I am using:

Code: Select all

	$headers = "From: " . $EMAIL_FROM . " <root@localhost>";

	// Generate a boundary string
	$rnd_str = md5(time());
	$mime_boundary = "==Multipart_Boundary_x{$rnd_str}x";

	// Add headers for file attachment
	$headers .= "\nMIME-Version: 1.0\n" .
		"Content-Type: multipart/mixed;\n" .
		" boundary=\"{$mime_boundary}\"";

	// Add a multipart boundary above the plain message
	$body = "This is a multi-part message in MIME format.\n\n" .
		"--{$mime_boundary}\n" .
		"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
		"Content-Transfer-Encoding: 7bit\n\n" .
		file_get_contents($log_path) . "\n\n";

	// make Base64 encoding for file data
	$data = chunk_split(base64_encode(file_get_contents($BACKUP_DEST.'/'.$BACKUP_NAME)));

	// Add file attachment to the message
	$body .= "--{$mime_boundary}\n" .
		"Content-Type: {$backup_mime};\n" .
		" name=\"{$BACKUP_NAME}\"\n" .
		"Content-Disposition: attachment;\n" .
		" filename=\"{$BACKUP_NAME}\"\n" .
		"Content-Transfer-Encoding: base64\n\n" .
		$data . "\n\n" .

	$res = mail( $EMAIL_ADDR, $EMAIL_SUBJECT, $body, $headers );

	if ( !$res ) {
		error( true, 'FAILED to email mysql dumps.' );
Does anyone see an error in the coding, or know of a php file size limitation using mail() or mime encoding?

Post Reply