note 39504 deleted from function.mail by irchtml
| From: | irchtml@php.net | Date: | Sat, 28 Feb 2004 20:02:34 +0000 |
| Subject: | note 39504 deleted from function.mail by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65942@lists.php.net to get a copy of this message | ||
Note Submitter: Hubert Ley <hley@anl.gov>
----
The previous two examples for attaching files to a message have a significant flaw that shows up
when attaching binary files (e.g. zipped or gzipped files).
This is due to the improper use of a construct similar to
$data=implode("",file("filename.extension"))
to read the attachment into a scalar string variable. As can be found in the documentation of the
file() function, the content of the file are returned as "lines", each line being
represented by an array element with the newline characters still attached (see how that makes only
sense for text files and not for binary files?). Implode then "reassembles" the lines into
a scalar string variable. This may succeed randomly even with binary files, but it will also create
faulty attachments for no obvious reason. While the original binary file is of course fine, the
attached copy may be internally corrupted. This has nothing to do with the actual attachment process
(the obvious first potential bug a user of these functions may tackle) - debugging the reason for
this faulty transmission is extremely difficult, so the examples really shouldn't be posted in
their current incarnations.
I would advise users to load the binary data from the file using an alternative method. Using the
functions fopen and fread will lead to a proper and clean approach. You can write your own
binary-safe reading routine with a few lines of code, so I will not elaborate on this in this
context. The newest PHP versions seem to have a function "file_get_contents" that seems to
do the right thing, but I am unable to verify this.