473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

file attachment in php mail

pradeepjain
563 512MB
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
5 2973
Markus
6,050 Expert 4TB
have a look at this.

Dont forget: google is your friend! ;)
Feb 13 '08 #2
pradeepjain
563 512MB
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
563 512MB
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
563 512MB
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
563 512MB
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

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

Similar topics

1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
4
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
2
by: Cimento Cola | last post by:
Hello all. Hope anyone can help me! I have this form, with: Please choose your file: (browse) The main purpose is the user to select a local file and then when he submits a mail should be...
14
by: Manish | last post by:
The project I am developing doesn't involves database. I want to parse the mailbox file (.mbx) and store the summary in the text file for fast retrieval and display of information in the Inbox...
9
by: deepaks85 | last post by:
Dear Sir, I have created a simple request form which will be mailed to me. Now I want to attach files and send it through that request form. For this I am using the following script: ...
7
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and...
1
by: krishan123456 | last post by:
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given...
0
by: prasenjit2007 | last post by:
I have a main form for inputing the (to/from/mesg/file) with the following code:- <html> <body> <table> <tr> <td>To:</td> <td><input type="text" name="to" size="50" ...
1
by: nukephp | last post by:
Hello Guys would you know about php email with his attachment Working on linux platform................................................... The code as below: mail.php <?php //define the...
2
by: Erik Witkop | last post by:
So I have been trying to get this to work all day. I can't get a local file on my web server to attach to an email. Right now I have it printing out in the body of the email. Please help me with...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.