I am a complete novice with php, so forgive me if this is something very obvious! I have tried to create an html form on my website which, when submitted, generates an email with a csv attachement which is sent to me with the relevant information.
I have been using code found online (some from this forum) to try to create a solution and seem to be vey close - however there is a problem somewhere. I am receiving 2 emails when submitted - the first is incorrect in that it is missing the senders' email address and the csv has no information - the second email is exactly right however.
This is the code I am using:
<?php
$email=$_POST['email'];
$customer=$_POST['customer'];
$quantity=$_POST['quantity'];
$prefix=$_POST['prefix'];
$itemno=$_POST['itemno'];
$format=$_POST['format'];
$to = "me@myemail.com";
$subject = "Form 01";
//Message Body
$text = "Form 01 attached";
//The Attachment
$cr = "\n";
$data .= "$email" . $cr;
$data .= "$customer" . $cr;
$data .= "$quantity" . $cr;
$data .= "$prefix" . $cr;
$data .= "$itemno" . $cr;
$data .= "$format" . $cr;
$fp = fopen('form01.csv','a');
fwrite($fp,$data);
fclose($fp);
$attachments[] = Array(
'data' => $data,
'name' => 'form_01.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" .
"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 attachments
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);
?>
I am also trying to add in reCaptcha, this is going in fine except it only seems to be allowing the first email to get through - which is the incorrect one. So I hope if the problem is in the code above and it can be fixed, then this should be solved too.
Thanks in advance!