Jump to content

Archived

This topic is now archived and is closed to further replies.

sun14php

attachement format problem...with php

Recommended Posts

ok anyhow i managed to sent attachemnet with mail using php, but problem is that , my attachement is displayed in below format, though i attached a excle file & i tried on notepad file also.

This is a multi-part message in MIME format.
--==Multipart_Boundary_xa7bb2d8b920cbe5c4753085189780635x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit



--==Multipart_Boundary_xa7bb2d8b920cbe5c4753085189780635x
Content-Type: application/x-msdownload;
name="extraction.xls"
Content-Disposition: attachment;
filename="extraction.xls"
Content-Transfer-Encoding: base64

CiAKIAogCiAKCiAKbG9naWQJdGRhdGUJdHRpbWUJc3J2aWQJcHJvdHlwZQlkZWF0aWxzCWFjdGlv
bglhdHNzCXN3Zm0Jb3MJdHQJcmVtYXJrcwkKIjEiCSIyMDA2LTA3LTA3IgkiMTU6MTI6NDkiCSJz
ZGxhOSIJImNwdSIJIjkxIgkic2VydmVyIHN0YWZmIGluZm9ybWVkIgkiYWN0aW9uIHRha2VuIgki
cmVzbG92ZWQiCSJvayIJIjciCSJvayIKIjIiCSIyMDA2LTA3LTA3IgkiMTc6NDk6MjMiCSJzZGxh
NyIJImNwdSIJIjgyIgkic2VydmVyIHN0YWZmIGluZm9ybWVkIgkiYWN0aW9uIHRha2VuIgkibm90
IHJlc2xvdmVkIgkib2siCSIyNSIJIm9rIgoiMyIJIjIwMDYtMDctMDciCSIxODoxNDoyNiIJInNk
bGE3IgkiY3B1IgkiMTAwIgkic2VydmVyIHN0YWZmIGluZm9ybWVkIgkiYXV0byByZWNvdmVyeSIJ

infact it should have displayed in file attachement(as regular attacement) that can be downloaded form mail like a seprate file.
with this attachement i can't downlaod file.what wrong with my coding: below is code:-

PHP :code:
<?php
//require_once "Mail.php";
#--------From Html form
$to      = $_POST['to'];

$from    = $_POST['from'];

$subject = $_POST['subject'];

$message = $_POST['message'];

// Obtain file upload vars

$fileatt      = $_FILES['fileatt']['tmp_name'];

$fileatt_type = $_FILES['fileatt']['type'];

$fileatt_name = $_FILES['fileatt']['name'];



#$headers = "From: $from";
$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => false,
    'username' => $username,
    'password' => $password));




if (is_uploaded_file($fileatt)) {

  // Read the file to be attached ('rb' = read binary)

  $file = fopen($fileatt,'rb');

  $data = fread($file,filesize($fileatt));

  fclose($file);



  // Generate a boundary string

  $semi_rand = md5(time());

  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

 

  // Add the headers for a file attachment

  $headers .= "\nMIME-Version: 1.0\n" .

              "Content-Type: multipart/mixed;\n" .

              " boundary=\"{$mime_boundary}\"";



  // Add a multipart boundary above the plain message

  $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";



  // Base64 encode the file data

  $data = chunk_split(base64_encode($data));



  // Add file attachment to the message

  $message .= "--{$mime_boundary}\n" .

              "Content-Type: {$fileatt_type};\n" .

              " name=\"{$fileatt_name}\"\n" .

              "Content-Disposition: attachment;\n" .

              " filename=\"{$fileatt_name}\"\n" .

              "Content-Transfer-Encoding: base64\n\n" .

              $data . "\n\n" .

              "--{$mime_boundary}--\n";

}
#------------------
#$from = "sunil <sunil.phogat@sugaldamani.com>";
#$to = "sunil <afsa23@rediffmail.com>";
#$subject = "Hi!";
#$body = "Hi,\n\nHow are you?";

$host = "smtp.registeredsite.com";
$username = "sunil.phogat@registeredsite.com";
$password = "****";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => false,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $message);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
?>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.