[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1027
  • Last Modified:

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;


0
shafer23
Asked:
shafer23
  • 3
  • 3
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
shafer23Author Commented:
thanks!
0
 
hieloCommented:
you are welcome
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now