Web Analytics Made Easy -
StatCounter Multiple Attachments Path Errors - CodingForum

Announcement

Collapse
No announcement yet.

Multiple Attachments Path Errors

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple Attachments Path Errors

    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.

    PHP Code:
    $files = array($_SERVER['DOCUMENT_ROOT'].'/images/classes/dc2010.jpg',$_SERVER['DOCUMENT_ROOT'].'/images/classes/dc2011.jpg');
    $to "[email protected]";
    $from "[email protected]";
    $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

  • #2
    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:
    PHP Code:
    $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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      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.

      Comment


      • #4
        Originally posted by MichigamJim View Post
        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.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment

        Working...
        X