0

I have a rather large form that when filled out and submitted, I want the data to be formed into a CSV file and emailed to a specific email address. Does anyone know if it is all at all possible to add a file upload function to the bottom of this form? This is for a client and have just encountered limitations in my knowledge of PHP.

This is the code I am using:

<?php

if(isset($_POST['formSubmit'])) {

$email=$_REQUEST['email']; 
$firstName=$_REQUEST['firstName']; 
$lastName=$_REQUEST['lastName']; 
$to = "***@***.co.uk"; 
$subject = "New application submission"; 


$message = "". 
"Email: $email" . "\n" . 
"First Name: $firstName" . "\n" . 
"Last Name: $lastName";   

//The Attachment    
$varTitle = $_POST['formTitle'];    
$varForname = $_POST['formForname'];
$varMiddlename = $_POST['formMiddlename'];
$varSurname = $_POST['formSurname'];
$varKnownas = $_POST['formKnownas'];
$varAdressline1 = $_POST['formAdressline1'];
$varAdressline2 = $_POST['formAdressline2'];
$varAdressline3 = $_POST['formAdressline3'];
$varAdressline4 = $_POST['formAdressline4'];
$varAdressline5 = $_POST['formAdressline5'];
$varPostcode = $_POST['formPostcode'];
$varTelephone = $_POST['formTelephone'];
$varMobile = $_POST['formMobile'];
$varEmail = $_POST['formEmail'];
$varApproval = $_POST['formApproval'];
$varothersurname = $_POST['formothersurname'];
$varsex = $_POST['formsex'];
$varninumber = $_POST['formninumber'];
$varjobtitle = $_POST['formjobtitle'];
$vardates = $_POST['formdates'];
$varresponsibilities = $_POST['formresponsibilities'];
$varjobtitle2 = $_POST['formjobtitle2'];
$vardates2 = $_POST['formdates2'];
$varresponsibilities2 = $_POST['formresponsibilities2'];
$varjobtitle3 = $_POST['formjobtitle3'];
$vardates3 = $_POST['formdates3'];
$varresponsibilities3 = $_POST['formresponsibilities3'];
$varwebsite = $_POST['formwebsite'];
$vartshirt = $_POST['formtshirt'];
$vardietary = $_POST['formdietary'];
$varpc = $_POST['formpc'];
$varmac = $_POST['formmac'];
$varlaptop = $_POST['formlaptop'];
$vardongle = $_POST['formdongle'];
$varediting = $_POST['formediting'];
$varsocial = $_POST['formsocial'];
$varphotography = $_POST['formphotography'];
$varfilming = $_POST['formfilming'];
$vartraining = $_POST['formtraining'];
$varexhibition = $_POST['formexhibition'];
$varspecial = $_POST['formspecial'];
$varhobbies = $_POST['formhobbies'];
$varphotography = $_POST['formphotography'];
$varfilming = $_POST['formfilming'];
$vartraining = $_POST['formtraining'];
$varexcel = $_POST['formexcel'];
$varbigpicture = $_POST['formbigpicture'];
$varcriminal = $_POST['formcriminal'];

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


 //Generate a boundary string 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

//Add the headers for a file attachment 


$headers = "MIME-Version: 1.0\n" . 
       "From: {$from}\n" . 
         "Cc: [email protected]\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/html; charset=\"iso-8859-1\"\n" . 
      "Content-Transfer-Encoding: 7bit\n\n" . 
      $text . "\n\n"; 

//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"; 
mail($to, $subject, $message, $headers);
header('Location: http://bigpictest.co.uk/thanks.php');
exit();
}


}


?>
1
  • what's the exact problem with the code you have? Commented Jun 14, 2013 at 0:15

2 Answers 2

1

There is no need to go to so much trouble. The following will probably help:

$filename="directory/csvfile.csv"; //specify filename and path
$keys=array_keys($_POST);     //get list of post keys
$file=fopen($filename,"w");   //open a csv file to write to;
fputcsv($file,$keys);         //write post keys as first line of CSV file.
fputcsv($file,$_POST);        //write post data as second line of csv file.
fclose($file);                //close the file.

The csv file you need to attach is created thus. No need for all those variables.

I haven't checked your mail code. If you are having problems with attaching the file, please ask a different question about that (though there are doubtless answers here already)

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

I've done this in asp and asp.net but never had to do it with php (that is sending mail with attachments from the server).

The thing to understand is that when you submit the form, the data is being sent to be processed somewhere, lets say: FormProcessor which may be in a separate file or in the php page itself.

So when the FormProcessor receives the data, you can combine it or format it in anyway you like. You would need access to the filesystem to create the file on the server. Let's say that the function for sending email is in the FormProcessor scope. You can use email headers to add the attachment when you define the email object.

This is one of my send email funcitons in php which I've modified to use as a sample here:

function FormProcessor_SendEmail($v){ // $v is an array containing the data to use for sending the email
           // parameters = $v[0] = user email, $v[1] = business name,
           // $v[2] = address, $v[3] = city, $v[4] = state, $v[5] = zip code,
           // $v[6] = phone, $v[7] = message, $v[8] = service area
           $eol = "\r\n";
           $tb = "\t";
           $stb = "\t\t";

           // notify new registrant
           $to = $v[0];
           $subject = "This is the subject text";
           $message = "Date : " . $this->prepare_new_timestamp() . ".<br /><br />" . $eol;
           $message .= "Thank you for requesting more information about " . $this->get_sitename() . "<br /><br />" . $eol;
           $message .= "We will be contacting you as soon as possible<br />" . $eol;
           $message .= "with information that is tailored to your needs and interests.<br /><br />" . $eol;
           $message .= "We have received the following information from you:<br /><br />" . $eol;
           $message .= "Your Business Name: " . $v[1] . "<br />" . $eol;
           $message .= "Business Address: " . $v[2] . "<br />" . $eol;
           $message .= "City: " . $v[3] . "<br />" . $eol;
           $message .= "State: " . $v[4] . "<br />" . $eol;
           $message .= "Zip Code: " . $v[5] . "<br />" . $eol;
           $message .= "Phone: " . $v[6] . "<br />" . $eol;
           $message .= "Service Area: " . $v[8] . "<br />" . $eol;
           $message .= "Message: <br />" . $v[7] . "<br /><br />" . $eol;
           $message .= "If you have additional questions, please address them to: " . $this->get_support_email() . "<br />" . $eol;
           $message .= "or by phone at: " . $this->get_support_phone() . "<br /><br />" . $eol;
           $message .= "If you did not submit this request, please contact us and let us know<br />" . $eol;
           $message .= "so that we may take the proper actions necessary.<br /><br />" . $eol;
           $message .= "Sincerely,<br />" . $eol;
           $message .= "The Team at " . $this->get_sitename() . "<br />" . $eol;
           $headers = "MIME-Version: 1.0" . "\r\n";
           $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
           $headers .= 'From: sitename <' . $this->get_webmaster_email() . '>' . "\r\n";
           $headers .= 'Cc: webmaster <' . $this->get_webmaster_email() . '>' . "\r\n";
           mail($to,$subject,$message,$headers);
           return 1;
  }

It doesn't send an attachment, but I'm sure someone else on this site can help you find a better way.

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.