473,840 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

send attachment without upload using mail()

123 New Member
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 7547
Atli
5,058 Recognized Expert Expert
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
10196
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 receive the text in my mailbox, all special characters have been turned into mess...... For example: "wspólprace" is turned into "współprace". It seems PHP is handling the Unicode-8 strings quite well (when I
3
3315
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 D:\LUGUBA\root\site\temp5.php on line 8 i have done the following (1) in php.ini file
2
2319
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 working with it for a few days (and nights), it finally came to my attention that it will only send 'To, From, Subject, Body' information and not
1
1399
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
3261
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 implementation had problems consuming too large files that way. They encountered a memory leak in the parser that made the whole mainframe environment to crash! So the decided they should go for DIME attachments instead. I use VB.Net in VS2005.
5
11462
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
3476
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 display the all files only <form method="post" ACTION="upload.jsp" name="uploadForm" ENCTYPE="multipart/form-data"> <input id="uploadfile" name="uploadfile" type="file" accept="image/gif,image/png">
1
1503
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
2676
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 http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title></title>
0
9702
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10607
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10302
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7845
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7023
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4080
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.