473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to browse and attach file in email?

nirmalsingh
218 100+
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
1 5453
ronverdonk
4,258 Expert 4TB
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

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

Similar topics

15
by: tabonni | last post by:
Hi I try to grab the checked files from HTML page and then send those PDF files as attachments. It can just send email, there are no PDF files attached. Can anybody point out my error? My...
6
by: a | last post by:
What is the best way to construct an email in python and also attach a html file the html file to be attached is not on disk, but should be dynamically constructed in the python script I want...
3
by: oopaevah | last post by:
I want to have a separate button which invokes the "browse" button on an input type=file. In internet explorer the following code works ok, in firefox nothing happens. All I do is call click()...
2
by: calebmichaud | last post by:
Hi I am having trouble understanding how to attach a file or files to a record. I want to (in form view0 allow the user to attach one or more than one file to the associated record, and then...
6
by: naima.mans | last post by:
Hello :) I'm newbie to XSLT and i need some advice please: Here the action: I have to browse an XML file with xslt : For each node i have to determinate if it is a node where i need to add...
3
by: Malaya | last post by:
Hi All, Could you please help me out with a piece of code. I need to write a code for attaching files using Browse option.Say when you click the Browse button,it should take me to a page where I...
5
by: rrkrishna24 | last post by:
Can someone guide how to code in simple HTML, how to browse the files and attach them. That is, when I click a button say "Attach", a window should be opened with which one can browse the files in...
14
by: sreemathy2000 | last post by:
My requirement is to generate and attach a file and send in an email automatically. i have a existing functionality where i using the code below to write a CSV file in my local system. ...
9
by: dpatel75 | last post by:
I am trying to copy a database from a SQL 2000 SP3 Windows 2000 server to a 2005 SP2 Windows 2003 server. I am trying to use detach and attach method (have tried both within Management Studio and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.