473,400 Members | 2,163 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,400 software developers and data experts.

send attachment without upload using mail()

123 100+
Below code works however it seems it will upload and save the attachment to
server, how can send attachment without save the file into my server?
and how to limit the file size to 3mb?

Expand|Select|Wrap|Line Numbers
  1. $email = $_POST['email']; 
  2. $from = $_POST['from']; 
  3. $subject = $_POST['subject']; 
  4. $message = $_POST['message']; 
  5.  
  6. // Obtain file upload vars 
  7. $fileatt = $_FILES['fileatt']['tmp_name']; 
  8. $fileatt_type = $_FILES['fileatt']['type']; 
  9. $fileatt_name = $_FILES['fileatt']['name']; 
  10.  
  11. $headers = "From: $from"; 
  12.  
  13. if (file($fileatt)) { 
  14. // Read the file to be attached ('rb' = read binary) 
  15. $file = fopen($fileatt,'rb'); 
  16. $data = fread($file,filesize($fileatt)); 
  17. fclose($file); 
  18.  
  19. // Generate a boundary string 
  20. $semi_rand = md5(time()); 
  21. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
  22.  
  23. // Add the headers for a file attachment 
  24. $headers .= "\nMIME-Version: 1.0\n" . 
  25. "Content-Type: multipart/mixed;\n" . 
  26. " boundary=\"{$mime_boundary}\""; 
  27.  
  28. // Add a multipart boundary above the plain message 
  29. $message = "This is a multi-part message in MIME format.\n\n" . 
  30. "--{$mime_boundary}\n" . 
  31. "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
  32. "Content-Transfer-Encoding: 7bit\n\n" . 
  33. $message . "\n\n"; 
  34.  
  35. // Base64 encode the file data 
  36. $data = chunk_split(base64_encode($data)); 
  37.  
  38. // Add file attachment to the message 
  39. $message .= "--{$mime_boundary}\n" . 
  40. "Content-Type: {$fileatt_type};\n" . 
  41. " name=\"{$fileatt_name}\"\n" . 
  42. //"Content-Disposition: attachment;\n" . 
  43. //" filename=\"{$fileatt_name}\"\n" . 
  44. "Content-Transfer-Encoding: base64\n\n" . 
  45. $data . "\n\n" . 
  46. "--{$mime_boundary}--\n"; 
  47.  
  48. mail ("$email", "$subject", "$message", "$headers"); 
  49. echo("A confirmation email has been sent to: $email");  
Feb 8 '10 #1
1 7413
Atli
5,058 Expert 4TB
Hey.

I see nothing in that code that would permanently save the file on the server. It will be uploaded temporarily to your server, at the location $_FILES['fileatt']['tmp_name'] specifies, and once the script is done executing, it will be disregarded.

and how to limit the file size to 3mb?
You check the size of the temporary file and exit if it is larger than your ceiling.

P.S.
You don't have to quote variables if you are using them by themselves. It's pointless.
Expand|Select|Wrap|Line Numbers
  1. // This:
  2. mail ("$email", "$subject", "$message", "$headers")
  3. // Should be:
  4. mail ($email, $subject, $message, $headers)
  5.  
Feb 8 '10 #2

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

Similar topics

6
by: Edo van der Zouwen | last post by:
I have the following problem. On a website there's a (simple) feedback form. This is used also by Polish visitors who (of course) type Polish text using special characters. However, when I...
3
by: vishal | last post by:
i am getting following error. Warning: mail() : Failed to connect to mailserver at "smtp.gmail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in...
2
by: Andrew Williams | last post by:
Hi, I have just created a web form that should send its details to an e-mail address. The problem is that I was working on the pretense that CDONTS would suffice my needs. However, after...
1
by: saravanakumar | last post by:
Hi How to send email in php without using mail() ? Let me know the methods used to send intranet (local email) in php - somas
5
by: =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= | last post by:
I have never sent attachment with webservices. Yesterday I got the challenge to redesign my solution that sends large XML structures to a Java webservice. The reason was that the Java SOAP...
5
by: serena9131 | last post by:
Dear all, I having problem for sending subject in cheses language using mail(), Can anyone let me know how to send send non unicode subject ? Thanks Serena
1
by: ganesandeiav | last post by:
Dear frnds I am using jsp.I have a problem file upload using html control.I want filter seleted file such as *.gif&*.jpeg images .It should be list out jpeg gif image only. iam try this code but...
1
by: swetha123 | last post by:
hello, can any tell me how to send mails from site using mail() in php do we need to change in the php.ini these are in php.ini
1
by: sunraj | last post by:
With the Following Fields can Anybody Help me, How do I send email from ASP using SMTP Authentication <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.