| | |
i am trying to attach a file with mail and getting following error.how can i solve?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2008
Posts: 10
Reputation:
Solved Threads: 0
i am trying to attach a file with mail and getting following error.how can i solve?
0
#1 May 31st, 2008
error follows:
coding
PHP Syntax (Toggle Plain Text)
The email was sent. 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 Warning: fread(): supplied argument is not a valid stream resource in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 59 Warning: fclose(): supplied argument is not a valid stream resource in C:\Program Files\xampp\htdocs\samplemuthu\mail2.php on line 60
php Syntax (Toggle Plain Text)
<?php $to="admin@localhost"; $from = "John-Smith <john.smith@domain.com>"; $subject = "Here is your attachment"; $fileatt = "C://Program Files/xampp/htdocs/samplemuthu"; $fileatttype = "application/pdf"; $fileattname = "user.pdf"; $headers = "From: $from"; if( mail( $to, $subject, $message, $headers ) ) { echo "<p>The email was sent.</p>"; } else { echo "<p>There was an error sending the mail.</p>"; } $semi_rand = md5( time() ); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $data = chunk_split( base64_encode( $data ) ); $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $file = fopen( $fileatt, 'rb' ); $data = fread( $file, filesize( $fileatt ) ); fclose( $file ); ?>
Last edited by peter_budo; May 31st, 2008 at 6:27 am. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
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...
Try this code and it will give u the best results...
PHP Syntax (Toggle Plain Text)
//define the receiver of the email $to = 'youraddress@example.com'; //define the subject of the email $subject = 'Test email with attachment'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: [email]webmaster@example.com[/email]\r\nReply-To: [email]webmaster@example.com[/email]"; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; //read the atachment file contents into a string, //encode it with MIME base64, //and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/zip; name="attachment.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 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
![]() |
Other Threads in the PHP Forum
- Previous Thread: Select where clause with "IN"
- Next Thread: Image upload using Ajax
| Thread Tools | Search this Thread |
action address ajax apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher display domain dynamic echo email error errorlog fatalerror file files folder form format forms function functions google href htaccess html image include insert interactive ip java javascript joomla limit link load login mail malfunctioning masterthesis menu mlm multiple mysql nodes oop paypal pdf php popup problem query radio ram random record recursion reference remote return script search server sessions sms source space sql syntax system table tutorial unset update upload url validation validator variable video web websitecontactform youtube





