attachment and Generate csv file from form data on then email to address

0
236

$to=”findinall@gmail.com”;
$subject=”New enquiry received.”;
$from = stripslashes($_POST[‘ime’]).”<“.stripslashes($_POST[’email’]).”>”;

// generate a random string to be used as the boundary marker
$mime_boundary=”==Multipart_Boundary_x”.md5(mt_rand()).”x”;

// now we’ll build the message headers
$headers = “From: $from\r\n” .
“MIME-Version: 1.0\r\n” .
“Content-Type: multipart/mixed;\r\n” .
” boundary=\”{$mime_boundary}\””;

// here, we’ll start the message body.
// this is the text that will be displayed
// in the e-mail
$excelurl=SITE_WS_PATH.”/excel.php?id=”.$insid;
$message = “Dear Admin
\n A new enquiry has been posted by member.Which details are given below.
\n Name: ” .$_POST[‘fname’]. ”
\n Email: ” .$_POST[’email’]. ”
\n Phone: ” .$_POST[‘phone’]. ”
\n Mobile: ” .$_POST[‘mobile’]. ”
\n Company : ” .$_POST[‘company_name’]. ”
\n Company Type: ” .$_POST[‘company_type’]. ”
\n Address line 1: ” .$_POST[‘address’]. ”
\n Address line 2: ” .$_POST[‘address2’]. ”
\n State/Province: ” .$_POST[‘state’]. ”
\n City: ” .$_POST[‘city’]. ”
\n Post code: ” .$_POST[‘postal_code’]. ”
\n Country: ” .getcountryname($_POST[‘country’]). ”
\n Region : ” .$_POST[‘region’]. ”
\n Reason for contact : ” .$_POST[‘reason_for_contact’]. ”
\n Name of project : ” .$_POST[‘project_name’]. ”
\n Location of project: ” .$_POST[‘project_location’]. ”
\n How did you hear about us?: ” .$_POST[‘how_did_you’]. ”
\n Comments: ” .$_POST[‘comment’];

// next, we’ll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$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”;

// now we’ll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile[‘tmp_name’];
$type = $userfile[‘type’];
$name = $userfile[‘name’];
$size = $userfile[‘size’];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,’rb’);

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we’ll insert a boundary to indicate we’re starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don’t set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we’ll add the final one after the loop finishes.
$message .= “–{$mime_boundary}\n” .
“Content-Type: {$type};\n” .
” name=\”{$name}\”\n” .
“Content-Disposition: attachment;\n” .
” filename=\”{$fileatt_name}\”\n” .
“Content-Transfer-Encoding: base64\n\n” .
$data . “\n\n”;
}
}
// here’s our closing mime boundary that indicates the last of the message

//The Attachment
$fname=$_POST[‘fname’];$email=$_POST[’email’];$phone=$_POST[‘phone’];$mobile=$_POST[‘mobile’];$company_name=$_POST[‘company_name’];$company_type=$_POST[‘company_type’];$address=$_POST[‘address’];$address2=$_POST[‘address2’];$state=$_POST[‘state’];$region=$_POST[‘region’];$postal_code=$_POST[‘postal_code’];$city=$_POST[‘city’];$country=$_POST[‘country’];$reason_for_contact=$_POST[‘reason_for_contact’];$project_name=$_POST[‘project_name’];$project_location=$_POST[‘project_location’];$how_did_you=$_POST[‘how_did_you’];$comment=$_POST[‘comment’];
$cr = “\n”;
$data = “Name” . ‘,’ . “Email” . ‘,’ . “Phone” .’,’ . “Mobile” .’,’ . “Company” .’,’ . “Company Type”.’,’. “Address line 1” .’,’. “Address line 2” .’,’. “State Province” .’,’. “Post code” .’,’. “City” .’,’. “Country” .’,’. “Region” .’,’. “Reason for contact    ” .’,’. “Name of project” .’,’. “Location of project”.’,’. “Comments”. $cr;
$data .= “$fname” . ‘,’ . “$email” . ‘,’ . “$phone” .’,’ . “$mobile” .’,’ . “$company_name” .’,’ . “$company_type”.’,’ . “$address”.’,’ . “$address2”.’,’ . “$state”.’,’ . “$postal_code”.’,’ . “$city”.’,’ . “$country”.’,’ . “$region”.’,’ . “$reason_for_contact”.’,’ . “$project_name”.’,’ . “$project_location”.’,’ . “$comment”. $cr;
$fp = fopen(‘file.csv’,’a’);
fwrite($fp,$data);
fclose($fp);

$attachments[] = Array(
‘data’ => $data,
‘name’ => ‘file.csv’,
‘type’ => ‘application/vnd.ms-excel’
);

//Add sttachments

foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment[‘data’]));
$name = $attachment[‘name’];
$type = $attachment[‘type’];

$message .= “–{$mime_boundary}\n” .
“Content-Type: {$type};\n” .
” name=\”{$name}\”\n” .
“Content-Transfer-Encoding: base64\n\n” .
$data . “\n\n” ;
}

$message.=”–{$mime_boundary}–\n”;
// now we just send the message
mail($to, $subject, $message, $headers);
// User email send ……………….
$From2=”info@findinall.com”;
$To2=$_REQUEST[’email’];
$Subject2=”enquiry received”;
$Msg2=”Dear <b>”.$_REQUEST[‘fname’].”</b>,<br>Your information has been sent successfully.We will get back to you soon<br>”;
$Headers2 = “From: $From2\n”;
$Headers2 .= “X-Mailer: PHP/”. phpversion();
$Headers2 .= “X-Priority: 3 \n”;
$Headers2 .= “MIME-version: 1.0\n”;
$Headers2 .= “Content-Type: text/html; charset=iso-8859-1\n”;
@mail(“$To2”, “$Subject2”, “$Msg2″,”$Headers2″);
$_SESSION[‘msg’]=”Your information has been sent successfully.We will get back to you soon”;
echo “<script>document.location.href=’thanks.php'</script>”;
//header(“Location:”.SITE_WS_PATH.”/thanks.php”);