Click to See Complete Forum and Search --> : Formmail to CSV?
klloyd
08-11-2004, 03:34 PM
I have built a form for one of our major clients to submit their monthly meter reads to us online. We have used NMS Formmail for most of our feedback needs before but now the client is asking that the results also be emailed to them as a comma separated values file (CSV). I am hoping there is a simpler way than learning php or asp. I have never used either before. Any suggestions?
Kevin
silent11
08-11-2004, 05:51 PM
why would you want to use asp or php??? csv is easy to make using web forms.
use strict;
use CGI;
use Text::CSV;
my $c = new CGI;
my $csv = new Text::CSV;
for ( sort $c->param() ){
$_ = $_ || " ";
push @rows, $c->param($_);
}
if ( $csv->combine(@rows) ){
# this prints every paramater
# passed to your script in
# csv format. Escaped an everything...
# If you can print to screen you can print to email
# maybe something like...
# my $message = $csv->string;
# send_email_func($to,$from,$message)
# just as an example
print $csv->string . "\n" || die $!;
}
else {
print $csv->error_input;
print "boooo.. Something didn't work right!";
}
don't attempt parsing and creating csv by hand. use the above module.
klloyd
08-12-2004, 09:21 AM
Forgive me for my limited knowledge of perl but will this create a csv file on the server or attach it to the formmail email? Should I just copy & paste it into the end of my NMS Formmail perl script? Thank you for your response by the way!
Kevin
silent11
08-12-2004, 09:32 AM
I don't use form mail.
I can't tell you how it works. I'm not sure what you'll have to adapt to get my code working with yours.
This script puts all of the paramaters passed to your cgi script via a form, or URL into csv format in the object calling the method "$csv->string".
knocknock.biz
08-12-2004, 10:13 AM
I guest if you try to send an attachment with formmail nms without perl knowledge, your changes of succeed are not much.
A workaround if the info is submitted less or equal to once a day:
You can create in a special folder a file with the name as the date of the post dot csv, and send in the e-mail a link to this file. By this way you will not need to learn about e-mail attachments, multipart data, charsets and who know what else.
If the workaround isn't ok, Good luck!
Try to found a mail script which handle attachments and then add the code by Silent11 and print the CSV where the attach goes instead of a file.
Good luck
Once you get all the results ina csv file, you can use Net::SMTP and MIME::Lite to send that csv file as email attachment automatically using perl script.
$msg = MIME::Lite->new(
From =>"from_emailaddress",
To =>"to_address",
Subject =>"subject",
Type =>'TEXT',
Data =>"testmessage"
);
$msg->attach(Type =>'text/csv',
Path =>"pathtofile",
Filename =>"filename",
Disposition => 'attachment'
);
Let me know if you need furher details.
klloyd
08-17-2004, 04:39 PM
Unfortunately I have no idea how to get it into a csv file in the first place. And I was hoping to at least have some kind of email notification (i.e. the use of formmail) so that I know when the form has been filled out.
Kevin
CSV stands for comma separated value file.
You can write to a csv file like below:
open(CSV, ">/file/path/to/store") || die "cannot write to csv file";
print CSV, @data;
close(CSV);
where @data can be having results with "," as separator.
Let me know if you still need more help.
klloyd
08-17-2004, 05:44 PM
Are you saying that I could use a perl script as simple as this, after of course changing parameters to match my needs?
#!/usr/bin/perl
open(CSV, ">/file/path/to/store") || die "cannot write to csv file";
print CSV, @data;
close(CSV);
$msg = MIME::Lite->new(
From =>"from_emailaddress",
To =>"to_address",
Subject =>"subject",
Type =>'TEXT',
Data =>"testmessage"
);
$msg->attach(Type =>'text/csv',
Path =>"pathtofile",
Filename =>"filename",
Disposition => 'attachment'
);
Yes, provided you have MIME::Lite module installed.
you need to use "use MIME::Lite;" statement in the beginning of the script.
You can even write all the variable values to csv file as below too.
open(csv,">pathtofile");
print csv, join(",",$var1,$var2...);
close(csv);
Let me know if you still need more details.
Hope this helps.
sorry forgot to add one more line at the end.
$msg->send();
klloyd
08-18-2004, 10:35 AM
I've contacted our hosting company and instructed them to install the MIME::Lite module, but in the meantime a simple question...once this is up & running my old html redirect tag should still work correctly right?
<input type="Hidden" name="redirect" value="http://www.myserver.com/thankyou.htm">
Kevin
silent11
08-18-2004, 10:53 AM
Originally posted by svb
Yes, provided you have MIME::Lite module installed.
you need to use "use MIME::Lite;" statement in the beginning of the script.
You can even write all the variable values to csv file as below too.
open(csv,">pathtofile");
print csv, join(",",$var1,$var2...);
close(csv);
Let me know if you still need more details.
Hope this helps.
ummmm... and what if $var1, $var2, or any of the other variables you are join()ing have commas (,) or new lines in them (\n) that will break your format. Imagine if George Bush submitted his name as "Bush, George W." you would suddenly have "Bush" in the name field and "Georege W." in what ever was next. By using the module I suggested above all of those *special* CSV characters will be properly escaped.
You can even use other separator like ":" or "*". It all depends on the types of your input.