473,396 Members | 1,815 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,396 software developers and data experts.

Send attachment with PHP mail

32
I have read through the manual on php dot net, and a number of tutorials around the net, however when I create an email and attempt to attach a file, I always get the hex (textual) representation, rather then having the file be attached.

Below is a test that I completed, hard coding all the values necessary to test if it was miscellaneous beginning reasons why i was having difficulty

Expand|Select|Wrap|Line Numbers
  1.  
  2. $path = "/path/to/Test_file.pdf";
  3. $name = "Test_file.pdf";
  4. $mime_type = "application/pdf";
  5.  
  6. $boundary = "VS". md5(uniqid(time()));
  7. $mime = sprintf("--%s%s", $boundary, "\n");
  8.  
  9. $mime .= sprintf("Content-Type: %s; name=\"%s\"%s", $mime_type, $name, "\n");
  10. $mime .= sprintf("Content-Transfer-Encoding: %s%s", "base64", "\n");
  11. $mime .= sprintf("Content-Disposition: attachment; filename=\"%s\"%s", $name, "\n\n");
  12.  
  13. $file = fopen($path, 'rb');
  14. if( !$file )
  15. {
  16.      $mime .= "cannot open file\n\n";
  17. }
  18. else
  19. {
  20.     $data = fread($file, filesize($path));
  21.     if(!$data)  // added for debugging - doesnt show
  22.     {
  23.         $mime .= "cannot read file\n\n";  
  24.     }
  25.     else
  26.     {
  27.         $data = chunk_split(base64_encode($data),76,"\n");
  28.         fclose($file);
  29.  
  30.         $mime .= sprintf("%s%s",$data,"\n\n");
  31.         $mime .= sprintf("--%s--%s", $boundary, "\n");
  32.         $mime .= "file present";  // added for debugging - shows
  33.     }
  34. }
  35.  
  36. $to = "myaddress@domain.com";
  37. $subject = "Testing Attachments";
  38. $headers = "\r\nFrom: site@domain.com\r\n";
  39.  
  40. if(!mail($to, $subject,$mime,$headers))
  41. {
  42.     echo "main not sent";
  43. }
  44.  
what I receive is all the $mime variables as they should be, but the file gets replaced with the hex equivalent, and the "file present" message at the bottom as I would expect

my question is, what am I doing wrong that its not attaching the file (in this case a pdf) to the email but rather parsing it in the body ?
Apr 28 '08 #1
1 1603
Atli
5,058 Expert 4TB
Hi.

Could it be that the debug message at the bottom is messing this all up?

Anyways, I do not really have much experience with this. I usually prefer using either Swift Mailer or PHPMailer when I need to send complex emails.
Much easier :P
May 8 '08 #2

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

Similar topics

3
by: marsandys | last post by:
Hello, I have this code below, which I am trying to have it send HTML formatted mail with embedded images. I can get this to send the mail, but it spits out a bunch of junk . I know this should...
2
by: Robbie De Sutter | last post by:
Hello, How do I open a new, empty e-mail message from the default e-mail client whereby the sender is given and a file is attached? Currently I use the command (vb.net): ---...
2
by: m_evergreen | last post by:
Errors: An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll Additional information: Could not access 'CDO.Message' object. innerexception is "Exception has...
2
by: Cimento Cola | last post by:
Hello all. Hope anyone can help me! I have this form, with: Please choose your file: (browse) The main purpose is the user to select a local file and then when he submits a mail should be...
4
by: Max | last post by:
hi I am using System.Web.Mail.MailMessage with System.Web.Mail.SmtpMail to send mail. now the my question is, Is it compulsory to add fields smtpauthenticate, sendusername and sendpassword...
3
by: =?Utf-8?B?SHVnaA==?= | last post by:
Hi There, I use follow code to send email inside VB.NET 2005. It does not work well. Error message of "Failure sending email" would occue. However, email was sent out sometimes. I am confused...
1
by: Chitu03 | last post by:
Hi I am already send a mail using Php with some attachement into it. My Problem is the attachement file is in my Database(mysql). I don't know how can i get from database and then add to my mail....
1
by: maxxxxel | last post by:
Hi Can anyone help me with some asp code , I changed the code to use CDO.message instead of the old cdont.sys to send mail from a ASP webpage which works fine. Our problem is that when we send...
1
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the results to just the email addresses that the query...
1
by: deepaks85 | last post by:
Dear All, I want to send some data through a form with Multiple attachment in an HTML Format. I have tried it but it is not working for me. I am able to send data without attachment but with the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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,...

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.