By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,484 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,484 IT Pros & Developers. It's quick & easy.

how to browse and attach file in email?

nirmalsingh
100+
P: 218
hi everybody,
my code for sending email is
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. </head>
  7. <body>
  8. <?php $to = "nirmal3210@yahoo.co.in";
  9. $subject = "Test mail";
  10. $message = "Hello! This is a simple email message. hjhjkhk jk hk";
  11. $from = "nirmalsingh@bcnsgroup.com";
  12. $headers = "From: $from";
  13. mail($to,$subject,$message,$headers);
  14. echo "Mail Sent.";
  15. ?> 
  16. </body>
  17. </html>
  18.  
it works well, here i want to know that how to browse and attach a file and send to recipent? and what's the maximum size we can attach through this? help me with sample code.
Jan 24 '07 #1
Share this Question
Share on Google+
1 Reply


ronverdonk
Expert 2.5K+
P: 4,258
Althought it is not the purpose of this forum to show code for all requests, it is easier in this case to show a complete send script than to insert code in all different places into your script. So here it is for a plain text file attachment.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   $to      = "";
  3.   $from    = "";
  4.   $filein  = "";
  5.   $message = "whatever text";
  6.   $headers = "From: $from";
  7.   // Read the file to be attached ('rb' = read binary)
  8.   $file = fopen($filein,'rb');
  9.   $data = fread($file,filesize($filein));
  10.   fclose($file);
  11.   // Generate a boundary string
  12.   $semi_rand = md5(time());
  13.   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  14.   // Add the headers for a file attachment
  15.   $headers .= "\nMIME-Version: 1.0\n" .
  16.               "Content-Type: multipart/mixed;\n" .
  17.               " boundary=\"{$mime_boundary}\"";
  18.   // Add a multipart boundary above the plain message
  19.   $message = "This is a multi-part message in MIME format.\n\n" .
  20.              "--{$mime_boundary}\n" .
  21.              "Content-Type: text/plain; charset=\"UTF-8\"\n" .
  22.              "Content-Transfer-Encoding: 7bit\n\n" .
  23.              $message . "\n\n";
  24.   // Base64 encode the file data
  25.   $data = chunk_split(base64_encode($data));
  26.   // Add file attachment to the message
  27.   $message .= "--{$mime_boundary}\n" .
  28.               "Content-Type: text-plain;\n" .
  29.               " name=\"{$filein}\"\n" .
  30.               "Content-Disposition: attachment;\n" .
  31.               " filename=\"{$filein}\"\n" .
  32.               "Content-Transfer-Encoding: base64\n\n" .
  33.               $data . "\n\n" .
  34.               "--{$mime_boundary}--\n";
  35. // Send the message
  36. if (!mail($to, "subject text", $message, $headers))
  37.   die("Error sending");
  38. else
  39.   echo "Message sent successfully";
  40.   exit;
  41. ?>
Ronald :cool:
Jan 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.