Page 1 of 1

problem in attaching the file to mail

Posted: Thu Jun 26, 2008 8:15 am
by arun_php
i,
i want to send the email to the particular mail id and i want to attach the file in that,but everything is working ,even mail is also receiving to mail and path is also displaying .i can attach the file to that mail but not sending the attachment .. I tried number of codes but no use ,i think my code is right but it not working.I attached the file please help me:crazy: :crazy:
<?php
include('inc/config.inc.php');
include('inc/header.inc.php');
$notall = 0;
if ($_SERVER['REQUEST_METHOD']=='POST')
{
// Obtain file upload vars
$fileatt = $_FILES['file']['tmp_name'];
$fileatt_type = $_FILES['file']['type'];
$fileatt_name = $_FILES['file']['name'];
$fileatt_size =($_FILES["file"]["size"] / 1024) . " Kb<br />";
$mailto = "mail2arunprakash14@gmail.com";
$notall = 0;

$subject = "MyPerfectAds info request";
$mail=$_POST['mail'];
$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$fax=$_POST['fax'];
$country=$_POST['country'];
$account=$_POST['account'];
$url=$_POST['url'];
$path=$_POST['file'];
$content=$_POST['content'];
$message = "Mail: ".$mail."\n";
$message .= "First Name: ".$first."\n";
$message .= "Last Name: ".$last."\n";
$message .= "Phone: ".$phone."\n";
$message .= "Fax: ".$fax."\n";
$message .= "Country: ".$country."\n";
$message .= "Account Type:".$account."\n";
$message .= "URL:".$url."\n";
$message .= "Path:".$path."\n";
$message .= "Contents:".$content."\n";
if ($mail && $first && $last && $phone && $fax && $country && $account && $url && $content)

{
$result=mail($mailto,$subject,$message);
echo '<script language="javascript">alert("The form was successfully submitted");</script>';

}


else
{
$notall = 1;
}
if (file($fileatt))
{
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$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";
}
}


?>

Re: problem in attaching the file to mail

Posted: Thu Jun 26, 2008 12:14 pm
by Christopher
Have you read the manual page on the mail() function? It has an example and notes about attachments. Also, you might want to use SwiftMailer. It is very difficult to get complex emails correct.