HTML Form using PHP to create/Email/Delete CSV

Hi it has been a while since I have had a reason to post a thread, but as most of you will know someone asks for something to be done and look at you for a solution.

OK, I have been asked to create a HTML form which will then create a CSV file, then using PHP I have been asked to Create the File, Email as an Attachment, then Delete the file. Within a few days I have been able to do this with a text file, but not successfully with a CSV file as it doesn’t seem to save the data correctly.

I would like to ask if anyone would look at the code I have created and edit it and inform me where I have gone wrong (I can take the critisium as I need to correct my learning style).

I have placed the code into 3 parts, first the simple html code which creates the form and calls the php file, then the create text file and send and finally the delete file.

I look forward to your comments and guidance.

html form name = user.html

<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=iso-8859-1” />
<title>Web Form Sample</title>
</head>

<body>
<form method=“post” action=writefile.php>

<p>Enter your email address
<input type=“text” name=“email” size=“50” />
</p>
<p>Please add your name
<input type=“text” name=“name” size=“20” />
</p>
<p>What comments do you have
<textarea rows=“2” name=“comments” cols=“20”></textarea>
</p>
<p> </p>
<p> </p>
<p>
<input type=“submit” value=“Submit” name=“B1” />
<input type=“reset” value=“Reset” name=“B2” />
</p>
</form>
</body>
</html>

php file name = writefile.php

<?php
$ourFileName = “textsample.txt”;
$ourFileHandle = fopen($ourFileName, ‘w’) or die(“can’t open file”);
fclose($ourFileHandle);

$email = $_POST[‘email’];
$name = $_POST[‘name’];
$comments = $_POST[‘comments’];

//the data
$data = "$email | $name | $comments
";

//open the file and choose the mode
$fh = fopen(“textsample.txt”, “a”);
fwrite($fh, $data);

//close the file
fclose($fh);

print “User Submitted”;

//define the receiver of the email
$to = web@somewebsite.co.uk’;
//define the subject of the email
$subject = ‘Email with attachment’;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date(‘r’, time()));
//define the headers we want passed. Note that they are separated with \r\

$headers = “From: web@somewebsite.co.uk\r
Reply-To: web@somewebsite.co.uk”;
//add boundary string and mime type specification
$headers .= “\r
Content-Type: multipart/mixed; boundary=\“PHP-mixed-”.$random_hash.”\“”;
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents(‘textsample.txt’)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
–PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary=“PHP-alt-<?php echo $random_hash; ?>”

–PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset=“iso-8859-1”
Content-Transfer-Encoding: 7bit

Hello sending test!!!
This is a trial text email message.

–PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset=“iso-8859-1”
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

–PHP-alt-<?php echo $random_hash; ?>–

–PHP-mixed-<?php echo $random_hash; ?>
Content-Type: textsample/txt; name=“textsample.txt”
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
–PHP-mixed-<?php echo $random_hash; ?>–

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print “Mail sent”. Otherwise print “Mail failed”
echo $mail_sent ? “Mail sent” : “Mail failed”;

Final snip of code

$myFile = “textsample.txt”;
unlink($myFile);

?>

not successfully with a CSV file as it doesn’t seem to save the data correctly

What is wrong with the data?
I looked at the code, and you’re using a pipe sign (|) as separator. For CSV you would generally use commas or semi-colons (especially if you want to open it in MS Excel). Also, you might need to encapsulate (at least) the comment field. If someone types an enter into the comment, the csv output will be invalid. Put quotes around it, and escape any quotes already in the comment.