php mail function - attaching files to email

Hi - I want to attach files using php's mail function.  Is the below code, specifically listed under 'if attachment' and 'if inline' correct?  I suspect that I probably have some errors in it, but I have been unable to find a complete example anywhere.

Thanks.

Pete




$filename = $file['name'];            // name with path
$filetype = $file['type'];


$handle      =fopen($filename, 'rb');
$contents    =fread($handle, filesize($filename));            // get file contents string
$attachment  =chunk_split(base64_encode($contents));      // split encoded string
fclose($handle);


// *** if attachment

$files .= "--$boundary\n";
$files .= "Content-type: $filetype; name=\"$filename\"\n";
$files .= "Content-Transfer-Encoding: base64\n";
$files .= "Content-Disposition: attachment; filename=\"$filename\"\n";
$files .= $attachment;


// *** if inline

$files .= "--$boundary\n";
$files .= "Content-type: $filetype; name=\"$filename\"\n";
$files .= "Content-Transfer-Encoding: base64\n";
$files .= "Content-Disposition: inline; filename=\"$filename\"\n";
$files .= $attachment;


shafer23Asked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
>>do you know why they embedded the attachment in the header
the php mail function does not have an argument to accept "attachments". It only accepts:
mail( TO, SUBJECT, MESSAGE, OTHER);

In OTHER you need to provide whatever else you need for the email (BCC,CC,REPLY-TO, etc), including attachments. If you look closer at what you posted, you are doing the same thing. YOUR variable is named $files, but it is equivalent to the $header variable in the other code.

>>is it possible to include the attachment language within either the header or the message?
You can use only valid Email headers. If you look at the documentation:
http://tools.ietf.org/html/rfc4021#page-49

Content-Language is a valid header. So, to answer you question directly, yes! Content-Language is what you need to use.
0
 
hieloCommented:
>>but I have been unable to find a complete example anywhere
Refer to:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23863367.html

what you need is just function mail_attachment(). If nothing else you will be able to customize it. I tested that and it works.
0
 
shafer23Author Commented:
great - thanks - one other thing, should I just assume that the file type is 'application/octet-stream'?  or is there some way to determine that based upon the file name?

Thanks.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
shafer23Author Commented:
also - in that example code, do you know why they embedded the attachment in the header - i.e. is it possible to include the attachment language within either the header or the message?

thanks!
0
 
shafer23Author Commented:
thanks!
0
 
hieloCommented:
you are welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.