error follows:

The email was sent.
Warning: fopen(C://Program Files/xampp/htdocs/samplemuthu) [function.fopen]: failed to open stream: Permission denied in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 58

Warning: fread(): supplied argument is not a valid stream resource in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 59

Warning: fclose(): supplied argument is not a valid stream resource in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 60

coding

<?php


$to="admin@localhost";
        $from = "John-Smith <john.smith@domain.com>"; 
        $subject = "Here is your attachment"; 
    
        $fileatt = "C://Program Files/xampp/htdocs/samplemuthu";
        $fileatttype = "application/pdf"; 
        $fileattname = "user.pdf";
    
        $headers = "From: $from";
        
        
        if( mail( $to, $subject, $message, $headers ) )
 {
         
            echo "<p>The email was sent.</p>"; 
         
        }
        else 
{ 
        
            echo "<p>There was an error sending the mail.</p>"; 
         
        }
    



    
        $semi_rand = md5( time() ); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    
        $headers .= "\nMIME-Version: 1.0\n" . 
                    "Content-Type: multipart/mixed;\n" . 
                    " boundary=\"{$mime_boundary}\"";
    
        $message = "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" . 
                $message . "\n\n";
    
        $data = chunk_split( base64_encode( $data ) );
                 
        $message .= "--{$mime_boundary}\n" . 
                 "Content-Type: {$fileatttype};\n" . 
                 " name=\"{$fileattname}\"\n" . 
                 "Content-Disposition: attachment;\n" . 
                 " filename=\"{$fileattname}\"\n" . 
                 "Content-Transfer-Encoding: base64\n\n" . 
                 $data . "\n\n" . 
                 "--{$mime_boundary}--\n"; 
                 

        $file = fopen( $fileatt, 'rb' ); 
        $data = fread( $file, filesize( $fileatt ) ); 
        fclose( $file );


?>

Dani AI

Generated

The warnings mean PHP couldn't open the file, so the subsequent fread/fclose calls fail. In 's snippet the path stored in $fileatt points to the folder under "Program Files" (no filename shown) and mail() is called before the multipart body and attachment are assembled. That combination explains the permission/stream errors and why the attachment never reaches the mail. 's example demonstrates the right multipart layout and base64 encoding, but the attachment must be read and the headers/message fully built before calling mail().

Fix checklist (apply in this order):

  • Point $fileatt to the actual file (include the filename and extension) and avoid protected system folders when testing. Prefer C:/xampp/htdocs/yourapp/user.pdf or a file inside your project directory rather than C:\Program Files\....
  • Verify the file exists and is readable before trying to open it (use realpath(), file_exists(), is_readable() or is_file()).
  • Read and base64-encode the file before you append the attachment part to $message. Only call mail() after headers and the full multipart body are complete.
  • On Windows, use CRLF (\r\n) for header and boundary line endings when using mail().

Minimal example of the safe order (illustrative):

$file = realpath(__DIR__ . '/user.pdf');
if (!$file || !is_file($file) || !is_readable($file)) { /* handle error */ }
$raw = file_get_contents($file);
$encoded = chunk_split(base64_encode($raw));
// build headers and multipart body (use "\r\n")
// append $encoded as the attachment part
// call mail() only after the complete $headers and $message exist

Additional notes: avoid calling mail() at the top of the script (remove that early call), and prefer file_get_contents()+chunk_split() over multiple fopen/fread calls for simplicity. If mail still arrives without the attachment, confirm PHP/SMTP settings in php.ini and that the webserver account has read access to the file location.

Salaam Hello, im FJ:
Try this code and it will give u the best results...

//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: [email]webmaster@example.com[/email]\r\nReply-To: [email]webmaster@example.com[/email]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.