|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22058 Sending mail with large attachments uses unreasonable amounts of memory
Submitted: 2003-02-04 15:41 UTC Modified: 2003-02-04 17:06 UTC
From: brienfwd at bigfoot dot com Assigned:
Status: Not a bug Package: PEAR related
PHP Version: 4.2.0 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: brienfwd at bigfoot dot com
New email:
PHP Version: OS:


 [2003-02-04 15:41 UTC] brienfwd at bigfoot dot com
I've just tracked down a long standing problem I've had with the IMP webmail 
problem and large attachments.  I've configure IMP 3.1/PEAR 1.0.1 to use SMTP 
to talk to a local mail server.  Sending a message with a 13MB attachment (19 
MB encoded) causes the memory usage to shoot up to 122MB. This seems to be due 
to the handling of the message body with regards to string copies and regular 
expression replacements.

This effectively limits the size of attachements that are useable within imp 
not to mention puts a pretty big strain on my server.

In particular, my installation seems to crap out consistently on the line 
inside the data() function: 

$data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data);

There are a couple of ways to solve this problem.  The best solution, which is 
probably the hardest, is to refactor the SMTP api to be more aware of file 
attachments and avoid doing the read-file/encode/write-to-network on the entire 
file.  If the SMTP layer was aware of file attachments, it could do the read-
file/encode/write-to-network on reasonably sized blocks.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-04 16:12 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

This is nothing usual or something that can be addressed. When dealing with large strings in PHP, the memory usage will nearly always be few times larger then the original string especially if you are performing various string mangling functions on it.
Your best solution may to switch from PEAR/IMP solution to something else not OO based that will make fewer copies of the string.
 [2003-02-04 16:53 UTC] brienfwd at bigfoot dot com
wouldn't a more powerful mail api that is attachment aware and doesn't load the entire file into memory be feasible?
 [2003-02-04 17:06 UTC]
Loading entire file into memory is needed because without it string mangling operations may not work as expected.
You could of course try using SMTP PECL extension, which is C code, meaning that it should be far more memory effecient then PHP equivalent and not to mention being much faster.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Sep 24 17:01:24 2023 UTC