943,771 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1396
  • PHP RSS
May 31st, 2008
0

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

Expand Post »
error follows:
PHP Syntax (Toggle Plain Text)
  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
php Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muthu raj is offline Offline
10 posts
since Apr 2008
May 31st, 2008
0

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

Salaam Hello, im FJ:
Try this code and it will give u the best results...
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muhammadfj is offline Offline
1 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Select where clause with "IN"
Next Thread in PHP Forum Timeline: Need help fast





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC