Jump to content

PHP mail attachment max size


php_nub_qq

Recommended Posts

Hi, I recently made a function to my website where users can download a full chat history with a certain friend of theirs instead of just showing the whole history in the browser and most probably crashing it ( if they've chatted for quite some time ). So everything works well except the fact that I can't send large files. I suppose the largest I could send was 10mb or something like that ( could be lying ). Anyway that's way less than I need so it doesn't really matter. What I need is to be able to send around 50mb of data and I believe the changes that need to be done are a matter of the mailing server. Unfortunately I'm using a hosting service and I have limited access to those things. Can anyone help me out?

 

EDIT: I just saw that the allowed size is in the error:

 

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

 

EDIT 2: Actually the allowed is larger than the filesize, now I'm confused

Edited by php_nub_qq
Link to comment
Share on other sites

Chunk the data to 1MB and start the php mail function to send 1 MB in every 1 min with sleep ( int $seconds ).

 

Also, you need to know what is the value of max execution time provided by your hosting provider in the php.ini file. 

Link to comment
Share on other sites

Chunk the data to 1MB and start the php mail function to send 1 MB in every 1 min with sleep ( int $seconds ).

 

Also, you need to know what is the value of max execution time provided by your hosting provider in the php.ini file. 

 

How do I chunk it to 1MB and wouldn't that send 50 emails ( for 50 mb file ) ?

 

Here's the code as well:

$file = 'ads2.zip'; // 60mb file for testing purposes
$file_name = $file; // file name and file path match
$to      = 'myemail@asd.com';
$subject = 'the subject';
$from = 'dgd@dgd.dgd';
	$message = "<h1>some HTML</h1>";
	$headers = "From: $from";
	 
	// boundary 
	$semi_rand = md5(time()); 
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
	 
	// headers for attachment 
	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
	 
	// multipart boundary 
	$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"utf-8\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
	$message .= "--{$mime_boundary}\n";
	 
	// preparing attachments
		$content = chunk_split(base64_encode(file_get_contents($file)));
		$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file_name\"\n" . 
		"Content-Disposition: attachment;\n" . " filename=\"$file_name\"\n" . 
		"Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
	 
	// send

	$ok = @mail($to, $subject, $message, $headers); 
	if ($ok) { 
		echo "<p>mail sent to $to!</p>"; 
	} else { 
		echo "<p>mail could not be sent!</p>"; 
	}  

the code is gotten from here https://gist.github.com/optikalefx/5149537 and was modified a little

Link to comment
Share on other sites

 

 

.....and wouldn't that send 50 emails ( for 50 mb file )

 

I know that's not very practical, but you need to be aware about few things.

 

Most of the share hosting providers limits their outcoming mail size to 25 - 30 MB, on the other hand there is also a limit per email message you receive (incoming mail size)

 

Many mail providers and ISPs will reject mail larger than 5-10 MB.

Link to comment
Share on other sites

That's not an option because the conversation is in the database and if I were to create a file on demand I would not be able to detect when it's download has been finished thus I cannot delete it, and even if I could if the user for some reason loses the file he has to generate a new one and that's just wasting resources and bandwidth, which is limited in my case. I just need to figure what's causing this error and how to prevent it and I'll manage from then on.

 

 

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

Edited by php_nub_qq
Link to comment
Share on other sites

Because your script is taking too long to process due to larger files

 

http://www.php.net/manual/en/ini.core.php#ini.memory-limit

 

 

Either adding this at the top of your page

ini_set('memory_limit', '-1');

or refactor your code :P

 

when I do 

 

ini_set('memory_limit', '-1');

 

there is no error but the mail doesn't get sent, which I suppose is due to the mail size limit. I will contact my hosting provider to see if I can negotiate something. Thank you guys for all the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.