note 39504 added to function.mail

From: Date: Fri, 30 Jan 2004 19:08:41 +0000
Subject: note 39504 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-64452@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.mail.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39504 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39504&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39504&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#64452) next »