Can anybody help me to send attatchment with mail...Pls help me...Very urgent...

Recommended Answers

All 9 Replies

Hello ...
Try this code..

<?php
   2.
      $fileatt = ""; // Path to the file
   3.
      $fileatt_type = "application/octet-stream"; // File Type
   4.
      $fileatt_name = ""; // Filename that will be used for the file as the attachment
   5.
       
   6.
      $email_from = ""; // Who the email is from
   7.
      $email_subject = ""; // The Subject of the email
   8.
      $email_txt = ""; // Message that the email has in it
   9.
       
  10.
      $email_to = ""; // Who the email is too
  11.
       
  12.
      $headers = "From: ".$email_from;
  13.
       
  14.
      $file = fopen($fileatt,'rb');
  15.
      $data = fread($file,filesize($fileatt));
  16.
      fclose($file);
  17.
       
  18.
      $semi_rand = md5(time());
  19.
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  20.
       
  21.
      $headers .= "\nMIME-Version: 1.0\n" .
  22.
      "Content-Type: multipart/mixed;\n" .
  23.
      " boundary=\"{$mime_boundary}\"";
  24.
       
  25.
      $email_message .= "This is a multi-part message in MIME format.\n\n" .
  26.
      "--{$mime_boundary}\n" .
  27.
      "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
  28.
      "Content-Transfer-Encoding: 7bit\n\n" .
  29.
      $email_message . "\n\n";
  30.
       
  31.
      $data = chunk_split(base64_encode($data));
  32.
       
  33.
      $email_message .= "--{$mime_boundary}\n" .
  34.
      "Content-Type: {$fileatt_type};\n" .
  35.
      " name=\"{$fileatt_name}\"\n" .
  36.
      //"Content-Disposition: attachment;\n" .
  37.
      //" filename=\"{$fileatt_name}\"\n" .
  38.
      "Content-Transfer-Encoding: base64\n\n" .
  39.
      $data . "\n\n" .
  40.
      "--{$mime_boundary}--\n";
  41.
       
  42.
      $ok = @mail($email_to, $email_subject, $email_message, $headers);
  43.
       
  44.
      if($ok) {
  45.
      echo "<font face=verdana size=2>The file was successfully sent!</font>";
  46.
      } else {
  47.
      die("Sorry but the email could not be sent. Please go back and try again!");
  48.
      }
  49.
      ?>
Thanks...

Thank you shanthi....thank you so much...

Shanthi...Can you give me an example?

You just fill up that variables and execute that code...
put echo before $email_message.
Then you will get what is going to our mail...
Try and ask if any errors arise...

I do not think you will not find many people here who will spoon feed you everything to complete your homework, put some effort first.

I do not think you will not find many people here who will spoon feed you everything to complete your homework, put some effort first.

echo

Hi.
Thanks for the code,
Just it is working, and the mail sends with the attachment, but i have the following errors while sending:

Warning: fread(): supplied argument is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\email\email.php on line 11

Warning: fclose(): supplied argument is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\email\email.php on line 12
The file was successfully sent!

and secondly, I dont have the email_txt in my mail_box, and also the attachment files not open.

what can be the problem

You just fill up that variables and execute that code...
put echo before $email_message.
Then you will get what is going to our mail...
Try and ask if any errors arise...

HI.
if you have something useful, then you can post it.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.