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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

hieloCommented:
>>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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shafer23Author Commented:
thanks!
0
hieloCommented:
you are welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Servers

From novice to tech pro — start learning today.