By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

file attachment in php mail

pradeepjain
100+
P: 563
Hii Guys,
i need to make a php and html form and in tht form it should attach a file and send it to the recepient . how to do this function of attaching a file (any kind) ..



Thanks,
pradeep
Feb 13 '08 #1
Share this Question
Share on Google+
5 Replies


Markus
Expert 5K+
P: 6,050
have a look at this.

Dont forget: google is your friend! ;)
Feb 13 '08 #2

pradeepjain
100+
P: 563
have a look at this.

Dont forget: google is your friend! ;)


Hey markusn00b,
I looked at that exapmle...it works...but i have more problems...i have a preview button tht previews all the entries and then in preview there is a subbmit button and also i need to attach 2 files..how to do this.

Thanks,
Pradeep
Feb 15 '08 #3

pradeepjain
100+
P: 563
This is the code ....please tell me how to add 2nd file in here..

[PHP]<?php
// Read POST request params into global vars
$to = $_POST['to'];
$from = "webmaster@iiap.res.in";
$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'];

$fileatt1 = $_FILES['fileatt1']['tmp_name'];
$fileatt1_type = $_FILES['fileatt1']['type'];
$fileatt1_name = $_FILES['fileatt1']['name'];

$headers = "From: $from";

// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$file1 = fopen($fileatt1,'rb');
$data1 = fread($file1,filesize($fileatt1));
fclose($file1);
// 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";

/*how to add 2nd file details here*/

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
[/PHP]
Feb 15 '08 #4

pradeepjain
100+
P: 563
another problem is ....as u cab see i am using smarty engine..

and the $_FILES['variable']['type'] is not working here...is there any solution ....is this a problem with smarty engine or wht..


Thanks,
Pradeep
Feb 15 '08 #5

pradeepjain
100+
P: 563
Hii,
I have solved all the problems i have given . i have tried the code to send 2 attachments also it worked as a singel file...but when i used the code in smarty engine its not recognizing the headers.its printing as it is in the mail.


the code for it is

[PHP]<?

require_once('libs/Smarty.class.php');
$smarty = new Smarty;
//require_once('db.inc');
$smarty->compile_dir = 'templates_c/';

$smarty->assign("TPL_Name",$_POST['Name']);
$smarty->assign("TPL_DOB",$_POST['DOB']);
$smarty->assign("TPL_Gender",$_POST['Gender']);
$smarty->assign("TPL_Nationality",$_POST['Nationality']);
$smarty->assign("TPL_Affiliation",$_POST['Affiliation']);
$smarty->assign("TPL_Email",$_POST['Email']);
$smarty->assign("TPL_Phone",$_POST['Phone']);
$smarty->assign("TPL_Fax",$_POST['Fax']);
$smarty->assign("TPL_PostalAddress",$_POST['PostalAddress']);
$smarty->assign("TPL_PermanentAddress",$_POST['PermanentAddress']);

$smarty->assign("TPL_Degree1",$_POST['Degree1']);
$smarty->assign("TPL_Institute1",$_POST['Institute1']);
$smarty->assign("TPL_University1",$_POST['University1']);
$smarty->assign("TPL_Year1",$_POST['Year1']);
$smarty->assign("TPL_Class1",$_POST['Class1']);
$smarty->assign("TPL_Rank1",$_POST['Rank1']);
$smarty->assign("TPL_Major1",$_POST['Major1']);

$smarty->assign("TPL_Degree2",$_POST['Degree2']);
$smarty->assign("TPL_Institute2",$_POST['Institute2']);
$smarty->assign("TPL_University2",$_POST['University2']);
$smarty->assign("TPL_Year2",$_POST['Year2']);
$smarty->assign("TPL_Class2",$_POST['Class2']);
$smarty->assign("TPL_Rank2",$_POST['Rank2']);
$smarty->assign("TPL_Major2",$_POST['Major2']);

$smarty->assign("TPL_Degree3",$_POST['Degree3']);
$smarty->assign("TPL_Institute3",$_POST['Institute3']);
$smarty->assign("TPL_University3",$_POST['University3']);
$smarty->assign("TPL_Year3",$_POST['Year3']);
$smarty->assign("TPL_Class3",$_POST['Class3']);
$smarty->assign("TPL_Rank3",$_POST['Rank3']);
$smarty->assign("TPL_Major3",$_POST['Major3']);

$smarty->assign("TPL_Degree4",$_POST['Degree4']);
$smarty->assign("TPL_Institute4",$_POST['Institute4']);
$smarty->assign("TPL_University4",$_POST['University4']);
$smarty->assign("TPL_Year4",$_POST['Year4']);
$smarty->assign("TPL_Class4",$_POST['Class4']);
$smarty->assign("TPL_Rank4",$_POST['Rank4']);
$smarty->assign("TPL_Major4",$_POST['Major4']);

$smarty->assign("TPL_PhDTopic",$_POST['PhDTopic']);
$smarty->assign("TPL_Referee1",$_POST['Referee1']);
$smarty->assign("TPL_Referee2",$_POST['Referee2']);
$smarty->assign("TPL_Justify",$_POST['Justify']);
$smarty->assign("TPL_TravelSupport",$_POST['TravelSupport']);
$smarty->assign("TPL_TravelSupportNeed",$_POST['TravelSupportNeed']);


$smarty->assign("TPL_city",$_POST['city']);
$smarty->assign("TPL_post",$_POST['post']);
$smarty->assign("TPL_country",$_POST['country']);

$smarty->assign("TPL_pcity",$_POST['pcity']);
$smarty->assign("TPL_ppost",$_POST['ppost']);
$smarty->assign("TPL_pcountry",$_POST['pcountry']);

$smarty->assign("TPL_sample",$_POST['sample']);
$smarty->assign("TPL_TSD",$_POST['TSD']);
$smarty->assign("TPL_cv",$_FILES['cv']['name']);
$smarty->assign("TPL_publication",$_FILES['publication']['name']);


$fileatt = $_FILES['cv']['tmp_name'];
$fileatt_type = $_FILES['cv']['type'];
$fileatt_name = $_FILES['cv']['name'];
print_r($_FILES['cv']);
print_r($_FILES['publication']);

$fileatt1 = $_FILES['publication']['tmp_name'];
$fileatt1_type = $_FILES['publication']['type'];
$fileatt1_name = $_FILES['publication']['name'];

// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$file1 = fopen($fileatt1,'rb');
$data1 = fread($file1,filesize($fileatt1));
fclose($file1);



if($_POST['TPL_Flag']!="Mail"){
$smarty->assign("TPL_Flag",'Preview');
$smarty->display('appnew.tpl');
}else{
$smarty->assign("TPL_Flag",'Mail');
$msg=$smarty->fetch('appnew.tpl');

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\r\nMIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;" .
" boundary=\"{$mime_boundary}\"\r\n";
// Add a multipart boundary above the plain message
$message1 .= "This is a multi-part message in MIME format" . $mime_boundary . "\r\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
"Content-Transfer-Encoding: 7bit\r\n" .
$msg . "\r\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
$data1 = chunk_split(base64_encode($data1));
// Add file attachment to the message
$message1 .= "--{$mime_boundary}\r\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
$data . "\r\n";
$message1 .= "--{$mime_boundary}\r\n" .
"Content-Type: {$fileatt1_type};\r\n" .
" name=\"{$fileatt1_name}\"\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
$data1 . "\r\n" .
"--{$mime_boundary}--\r\n";




$sub = "Astrostat School Application from " . $_POST['Name'] . "," .$_POST['country'];

//$headers = "From: ";
$headers = "From: \r\nContent-Type: text/html; charset=\"iso-8859-1\"\r\n";

$to = "";
$mail_ret =@mail($to, $sub, $message1, $headers);

if($mail_ret){
$message ="Your application for the 2nd IIA-PennState Astrostatistics school has been successfully submitted.";
}else{
$message ="Registration failed. Please try after some time.";
}


//Display message in a template
$smarty->assign("TPL_Message",$message);
$smarty->Display("newmessage.tpl");

}

?>[/PHP]


and in the mail at begging its printing as This is a multi-part message in MIME format==Multipart_Boundary_x96a252f89aecb7b6374843 7f2ce20e62x Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit

and at the end
--==Multipart_Boundary_x96a252f89aecb7b63748437f2ce2 0e62x Content-Type: ; name="" Content-Transfer-Encoding: base64 --==Multipart_Boundary_x96a252f89aecb7b63748437f2ce2 0e62x Content-Type: ; name="" Content-Transfer-Encoding: base64 --==Multipart_Boundary_x96a252f89aecb7b63748437f2ce2 0e62x--


Thanks,
Pradeep
Feb 16 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.