...

View Full Version : Multiple Attachments Path Errors



MichigamJim
08-25-2011, 07:48 PM
I'm using the found script below to try to send multiple attachments from a web server. The array of files varies but Im handling that okay. I can't get past a couple of errors though and would appreciate any sharper eyes than mine. The errors appear to be path related and repeat for every attached file; one is posted after the script.

The path in the array works to load the files in a browser when the domain is switched with the DOCUMENT_ROOT.


$files = array($_SERVER['DOCUMENT_ROOT'].'/images/classes/dc2010.jpg',$_SERVER['DOCUMENT_ROOT'].'/images/classes/dc2011.jpg');
$to = "me@mydomain.com";
$from = "me@mydomain.com";
$sub ="Testing for multiple attachment";
$msg = "Attachment mail testing";
$headers = "From: $from";

$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
$msg = "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" . $msg . "\n\n";

// attachments code starts
for($x=0;$x<count($files);$x++)
{
$msg .= "{$mime_boundary}\n";
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$msg .= "{$mime_boundary}\n";
$res = @mail($to, $sub, $msg, $headers);

if($res)
{
echo "<p>mail sent with attachments</p>";
}
else
{
echo "<p>error in processing email!</p>";
}




Warning: fopen(/images/classes/dc2010.jpg): failed to open stream: No such file or directory in /private/var/folders/cO/cOJR83ytEkeFACn7Oaltu++++TM/-Tmp-/TemporaryItems/pn.2011.08.25.14.24.10.400.php on line 24

Warning: filesize(): stat failed for /images/classes/dc2010.jpg in /private/var/folders/cO/cOJR83ytEkeFACn7Oaltu++++TM/-Tmp-/TemporaryItems/pn.2011.08.25.14.24.10.400.php on line 25

Warning: fread() expects parameter 1 to be resource, boolean given in /private/var/folders/cO/cOJR83ytEkeFACn7Oaltu++++TM/-Tmp-/TemporaryItems/pn.2011.08.25.14.24.10.400.php on line 25

Warning: fclose() expects parameter 1 to be resource, boolean given in /private/var/folders/cO/cOJR83ytEkeFACn7Oaltu++++TM/-Tmp-/TemporaryItems/pn.2011.08.25.14.24.10.400.php on line 26

Fou-Lu
08-25-2011, 08:24 PM
How is this file being executed? By a web browser attaching to your web server, or through something like CLI or cron?
The problem is your $_SERVER['DOCUMENT_ROOT'] is not parsing. Being a SERVER superglobal, there is no guarantee that it ever will be available. DOCUMENT_ROOT is generally defined by your web server itself, and is not available in a CLI environment (including cron), and given the path you have here being temp leads me to believe this may be a cron executed script.
The first error is the only one that's relevant really. The last two are because of the first, and the filesize is not actually necessary. This is from lack of error handling, and can be fixed with:


$iFiles = count($files);
for($x=0;$x< $iFiles; ++$x)
{
if ($file = @fopen($files[$x], 'rb'))
{
$msg .= "{$mime_boundary}\n";
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}


Please note that you're headers are pushed into $message and not $msg (which you'll need to change).

MichigamJim
08-28-2011, 08:45 PM
Thanks, the path was my issue. The execution was via a cgi app on a Mac server so I need to specify the absolute document path for UNIX.

I corrected the $message as well. Thanks for catching that.

Fou-Lu
08-28-2011, 08:51 PM
Thanks, the path was my issue. The execution was via a cgi app on a Mac server so I need to specify the absolute document path for UNIX.

I corrected the $message as well. Thanks for catching that.

You bet.
This is why I typically stick to relative filepaths based off of dirname(__FILE__) instead. This ensures that you don't require an absolute path and you don't need to rely on a server or environment variable that may not be set in your context of execution.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum