Warning: fopen(graphic_design/files/thread-997-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421
file NOT opened form results as attachments? -
PDA

View Full Version : form results as attachments?


dubtastic
06-06-2004, 02:50 PM
is there a way to collect what a user inputs in a standard form, create a file attachment with that information, save it as a csv file, and send that as an attachment?

a friend of mine has an access database that they are updating with information that is sent via an html form. the host is a unix box or else i would look for a way to use asp to automatically update the database realtime. so what i would like to do is have the user fill out the form and that information be sent in csv format so it could easily be imported into access.

any ideas?

Octane
06-06-2004, 05:15 PM
you will need some kind of program to do it, like Perl or PHP ... if u want, I can probably whip something up for you in PHP??

dubtastic
06-09-2004, 08:35 AM
that would be cool. :) how difficult is this?

Octane
06-09-2004, 10:49 AM
not very long at all ... 10 minutes ... I just needed to pull together some of my functions that I've built in the last few years to make it work:

<?php

$message_body = 'Whatever floats your boat'; // message that goes in the body of the message
$subject = 'CSV Attach'; // subject of email
$to_email = 'john.doe@hotmail.com'; // to
$redirect_to = 'thankyou.php'; // page to be redirected to after sending mail

function verify_email ($email)
{
$email = trim($email);
if (preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', $email)) return 1;
else return 0;
}


function send_mail ($to, $from, $subject, $body, $attach_content='')
{
$headers = "";

// name of file to attach
$file_name = 'form_data.csv';
$boundary = md5(uniqid(time(),1))."_xmail";

// attach CSV
$mail_attached = "--".$boundary."\r\n"
. "Content-Type: text/plain; name=\"$file_name\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: inline; filename=\"$file_name\"\r\n\r\n"
.chunk_split(base64_encode($attach_content))."\r\n"
."--".$boundary." \r\n";

// form mail
$add_header ="MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$boundary\"";
$mail_content = "--".$boundary."\r\n"
. "Content-Type: text/plain; charset=iso-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 8bit\r\n\r\n"
. $body."\r\n\r\n".$mail_attached;

mail ($to, $subject, $mail_content,"From: ".$from."\r\n".$add_header);
} // end send_mail


// send mail
if ($_POST['c'] == 'contact')
{
$content = '';

// loop through all values posted
foreach ($_POST AS $key => $value)
{
// but ignore command and submit values
if ($key != 'c' && $key != 'submission')
{
echo "$key - $value<br>";
$content .= str_replace(',','',$value).', ';
} // end if
} // end foreach

if (verify_email($_POST['email']))
{
$email = trim(strtolower($_POST['email']));
$from_email = "\"{$_POST['first_name']} {$_POST['last_name']}\" <$email>";
send_mail($to_email,$from_email,$subject,$message_body,$content);
header("Location: $redirect_to");
exit;
}
else $message = 'invalid email address';
} // end if c = contact

echo <<< TEOL
$message

<form method=post>
<input type=hidden name=c value=contact>
<table width=400 border=0 cellspacing=0 cellpadding=0 bgcolor=#cccccc>
<tr>
<td><table width=400 border=0 cellspacing=1 cellpadding=2>
<tr bgcolor="#ffffff">
<td><b>First Name:&nbsp;</b></td>
<td><input type=text name="first_name" size=20 value="$first_name" ></td>
</tr>
<tr bgcolor="#ffffff">
<td><b>Last Name:&nbsp;</b></td>
<td><input type=text name="last_name" size=20 value="$last_name" ></td>
</tr>
<tr bgcolor="#ffffff">
<td><b>Email:&nbsp;</b></td>
<td><input type=text name="email" size=20 value="$email"></td>
</tr>
<tr bgcolor="#ffffff">
<td valign=top><b>Comments/Questions:&nbsp;</b></td>
<td><textarea name="comments" cols="45" rows="4" wrap="hard">$comments</textarea></td>
</tr>
<tr bgcolor="#ffffff">
<td>&nbsp;</td>
<td><input type="submit" value="Submit" name="submission"></td>
</tr>
</table></td>
</tr>
</table>
<br><br>
</form>
TEOL;
?>

... I didn't do any error verification in the way of spitting out a message to the end user besides checking for a valid email address. Enjoy!

dubtastic
06-10-2004, 07:53 AM
10 minutes? well, you've impressed me then. :) despite the fact that chris says you dance around the apartment in women's underwear, i think you are pretty cool. ;)

i will give this code a whirl and see what happens. many thanks for putting this together for me.

dubtastic
06-10-2004, 10:21 AM
did some quick testing and it works like a charm so far. only trouble is that spam assassin caught the message. i had to disable spam assassin in order to get the message. not a biggie, though. :)

thanks again!

Octane
06-10-2004, 05:32 PM
i think he's only seen me wear the pink ones ... you should see me in my yellow thong.

it only took me 10 minutes because of the countless hours i've spent into refining some of these functions ... you should see the sendmail function that i have now ... all classed out ... it'll do everything from sending mail to sending someone to the moon .. yet to be tested, but the method is there.

yea, i figured that might happen with a SPAM tool of some sort, but i figured, since you were sending it to yourself, then it wouldn't matter ...
to avoid that you add -f infront of the email address and add it to the header (i can show u if u want) ... however, this will set off red-flags to your server admin that someone is forcing email addresses ... mostly used when someone is SPAMing.

dubtastic
06-11-2004, 04:59 AM
i think he's only seen me wear the pink ones ... you should see me in my yellow thong.

it only took me 10 minutes because of the countless hours i've spent into refining some of these functions ... you should see the sendmail function that i have now ... all classed out ... it'll do everything from sending mail to sending someone to the moon .. yet to be tested, but the method is there.

yea, i figured that might happen with a SPAM tool of some sort, but i figured, since you were sending it to yourself, then it wouldn't matter ...
to avoid that you add -f infront of the email address and add it to the header (i can show u if u want) ... however, this will set off red-flags to your server admin that someone is forcing email addresses ... mostly used when someone is SPAMing.

yellow ones? pink ones? chris said they were plaid with a tail hanging off the back. :eek:

i think for now the form will be fine. if i need to do something different with sending it i might look you up.

very much appreciate the help! :grinning: