hi i have attached a mail_attachment file which contains the mail function in my code i have used the below code

<? 
include 'mail_attachment.php';
 $sent = mail_attachment($from, $to, $subject1, $body1, $attachment);?>

when i tried printing the variable ..........all the values are getting printed but the mail function is not working
it is not working.showing 2 warning

Warning: filesize() [function.filesize]: stat failed for http://www.website.com/profiles/resumes/158/llllll.doc in /home/website/public_html/mail_attachment.php on line 17

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/website/public_html/mail_attachment.php on line 17

Attachments
<?php
function mail_attachment ($from , $to, $subject, $message, $attachment){
    $fileatt = $attachment; // Path to the file                  
    $fileatt_type = "application/octet-stream"; // File Type 
    $start=    strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
    $fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the     attachment 

    $email_from = $from; // Who the email is from 
    $email_subject =  $subject; // The Subject of the email 
    $email_txt = $message; // Message that the email has in it 
    
    $email_to = $to; // Who the email is to

    $headers = "From: ".$email_from;

    $file = fopen($fileatt,'rb'); 
    $data = fread($file,filesize($fileatt)); 
    fclose($file); 
    $msg_txt="\n\nMail created using free code from 4word systems : http://4wordsystems.com";

    $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}\""; 

    $email_txt .= $msg_txt;
    
    $email_message .= "This is a multi-part message in MIME format.\n\n" . 
                "--{$mime_boundary}\n" . 
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
               "Content-Transfer-Encoding: 7bit\n\n" . 
    $email_txt . "\n\n"; 

    $data = chunk_split(base64_encode($data)); 

    $email_message .= "--{$mime_boundary}\n" . 
                  "Content-Type: {$fileatt_type};\n" . 
                  " name=\"{$fileatt_name}\"\n" . 
                  //"Content-Disposition: attachment;\n" . 
                  //" filename=\"{$fileatt_name}\"\n" . 
                  "Content-Transfer-Encoding: base64\n\n" . 
                 $data . "\n\n" . 
                  "--{$mime_boundary}--\n"; 


    $ok = @mail($email_to, $email_subject, $email_message, $headers); 

    if($ok) { 
    } else { 
        die("Sorry but the email could not be sent. Please go back and try again!"); 
    } 
}
?>

Hi Lydia, I guess that you're calling mail_attachment() with wrong information in $attachment parameter.
You're passing url while it should be a full path in the server's file system.
E.g.
/var/www/vhosts/website.com/httpdocs/profiles/resumes/....
or
./profiles/resumes....
You have to find out yourself what the path is (I don't know where your calling script (not the library) is located.
In case relative path doesn't work and you need to use absolute path try out phpinfo() command.

If I'm wrong in my assumption then please post what are mail_attachment() parameters when you call it.

hi the path is getting printed correctly when i tried echo statement
eg my wesite name is webs

when i tried printing the pathname,i got the below path as output
home/webb/public_html/profiles/9/BusinessProposal.doc

sorry,output is home/webs/public_html/profiles/9/BusinessProposal.doc

sorry,output is home/webs/public_html/profiles/9/BusinessProposal.doc

Mhm... I really doubt that.
The first error means that it cannot get file size for file:
http://www.website.com/profiles/resumes/158/llllll.doc
The file path is taken directly from the function call, so it is what you pass to the function.

But you claim that you call it with

mail_attachment(..., ..., ..., ..., '/home/webs/public_html/profiles/9/BusinessProposal.doc');

(path like this must start with /, you forgot about it in your post)

If I ignore the fact that profiles/resumes/158/llllll.doc plainly isn't profiles/9/BusinessProposal.doc, even then I don't believe you are giving me what you called the function with. I'm sure you think you do but then you most likely have a bug in the program that calls the function, not the function itself.
There is no silent translation from /home/... to http://....
Your script is passing http://... and not /home/...

Try creating a new file with only include and a function call like this:

mail_attachment('sender@pepa.info', 'petr.pavel@pepa.info', 'test subject', 'test body', '/home/webs/public_html/profiles/9/BusinessProposal.doc');

And double check that the file really exist.

I'll let you know if I receive the file. Also post error messages if you receive any.

This article has been dead for over six months. Start a new discussion instead.