i am trying to attach a file with mail and getting following error.how can i solve?

Reply

Join Date: Apr 2008
Posts: 10
Reputation: muthu raj is an unknown quantity at this point 
Solved Threads: 0
muthu raj muthu raj is offline Offline
Newbie Poster

i am trying to attach a file with mail and getting following error.how can i solve?

 
0
  #1
May 31st, 2008
error follows:
  1. The email was sent.
  2. Warning: fopen(C://Program Files/xampp/htdocs/samplemuthu) [function.fopen]: failed to open stream: Permission denied in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 58
  3.  
  4. Warning: fread(): supplied argument is not a valid stream resource in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 59
  5.  
  6. Warning: fclose(): supplied argument is not a valid stream resource in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 60
coding
  1. <?php
  2.  
  3.  
  4. $to="admin@localhost";
  5. $from = "John-Smith <john.smith@domain.com>";
  6. $subject = "Here is your attachment";
  7.  
  8. $fileatt = "C://Program Files/xampp/htdocs/samplemuthu";
  9. $fileatttype = "application/pdf";
  10. $fileattname = "user.pdf";
  11.  
  12. $headers = "From: $from";
  13.  
  14.  
  15. if( mail( $to, $subject, $message, $headers ) )
  16. {
  17.  
  18. echo "<p>The email was sent.</p>";
  19.  
  20. }
  21. else
  22. {
  23.  
  24. echo "<p>There was an error sending the mail.</p>";
  25.  
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. $semi_rand = md5( time() );
  33. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  34.  
  35. $headers .= "\nMIME-Version: 1.0\n" .
  36. "Content-Type: multipart/mixed;\n" .
  37. " boundary=\"{$mime_boundary}\"";
  38.  
  39. $message = "This is a multi-part message in MIME format.\n\n" .
  40. "--{$mime_boundary}\n" .
  41. "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  42. "Content-Transfer-Encoding: 7bit\n\n" .
  43. $message . "\n\n";
  44.  
  45. $data = chunk_split( base64_encode( $data ) );
  46.  
  47. $message .= "--{$mime_boundary}\n" .
  48. "Content-Type: {$fileatttype};\n" .
  49. " name=\"{$fileattname}\"\n" .
  50. "Content-Disposition: attachment;\n" .
  51. " filename=\"{$fileattname}\"\n" .
  52. "Content-Transfer-Encoding: base64\n\n" .
  53. $data . "\n\n" .
  54. "--{$mime_boundary}--\n";
  55.  
  56.  
  57. $file = fopen( $fileatt, 'rb' );
  58. $data = fread( $file, filesize( $fileatt ) );
  59. fclose( $file );
  60.  
  61.  
  62. ?>
Last edited by peter_budo; May 31st, 2008 at 6:27 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: muhammadfj is an unknown quantity at this point 
Solved Threads: 0
muhammadfj muhammadfj is offline Offline
Newbie Poster

Re: i am trying to attach a file with mail and getting following error.how can i solve?

 
0
  #2
May 31st, 2008
Salaam Hello, im FJ:
Try this code and it will give u the best results...
  1. //define the receiver of the email
  2. $to = 'youraddress@example.com';
  3. //define the subject of the email
  4. $subject = 'Test email with attachment';
  5. //create a boundary string. It must be unique
  6. //so we use the MD5 algorithm to generate a random hash
  7. $random_hash = md5(date('r', time()));
  8. //define the headers we want passed. Note that they are separated with \r\n
  9. $headers = "From: [email]webmaster@example.com[/email]\r\nReply-To: [email]webmaster@example.com[/email]";
  10. //add boundary string and mime type specification
  11. $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
  12. //read the atachment file contents into a string,
  13. //encode it with MIME base64,
  14. //and split it into smaller chunks
  15. $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
  16. //define the body of the message.
  17. ob_start(); //Turn on output buffering
  18. ?>
  19. --PHP-mixed-<?php echo $random_hash; ?>
  20. Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
  21.  
  22. --PHP-alt-<?php echo $random_hash; ?>
  23. Content-Type: text/plain; charset="iso-8859-1"
  24. Content-Transfer-Encoding: 7bit
  25. Hello World!!!
  26. This is simple text email message.
  27.  
  28. --PHP-alt-<?php echo $random_hash; ?>
  29. Content-Type: text/html; charset="iso-8859-1"
  30. Content-Transfer-Encoding: 7bit
  31.  
  32. <h2>Hello World!</h2>
  33. <p>This is something with <b>HTML</b> formatting.</p>
  34.  
  35. --PHP-alt-<?php echo $random_hash; ?>--
  36.  
  37. --PHP-mixed-<?php echo $random_hash; ?>
  38. Content-Type: application/zip; name="attachment.zip"
  39. Content-Transfer-Encoding: base64
  40. Content-Disposition: attachment
  41.  
  42. <?php echo $attachment; ?>
  43. --PHP-mixed-<?php echo $random_hash; ?>--
  44.  
  45. <?php
  46. //copy current buffer contents into $message variable and delete current output buffer
  47. $message = ob_get_clean();
  48. //send the email
  49. $mail_sent = @mail( $to, $subject, $message, $headers );
  50. //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
  51. echo $mail_sent ? "Mail sent" : "Mail failed";
Last edited by peter_budo; May 31st, 2008 at 6:28 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC