Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write data froma HTML form into a file and email the file

Status
Not open for further replies.

dvknn

IS-IT--Management
Mar 11, 2003
94
0
0
US
Hi guys,

I am totally new into PERL. I am C#/J2EE developer. I need your help.

I have a HTML form. When I hit submit button, I would like to invoke a PERL script, create a file and write the
name/value pair (pipe delimited), close the file and then email the file.

Due to the restriction on the Unix box, I have to write this in PERL/CGI.

Could anybody please show me the piece of code that would do this bit? (with some comments in it)

I REALLY appreciate it.

Thanks
 
Did u want it to send the file as an ATTACHMENT or just the text submitted by the form in the email?

Its very easy to send the form and put it into a file. But if you want to start making attachemnts then there will be more involved.


 
Here is some code that might help you.
It does not add an attachment, but simply send you the information and a link to the file created.

Code:
#!/usr/bin/perl


#- Call the CGI module.
use CGI qw/:standard/ ;
#- Prints errors on the browser
use CGI::Carp qw(fatalsToBrowser);

# Prints to HTML pages..
print header;

# Gets the form feilds out of the forms you made 
#- form_feild1 would be one of the form NAMES 
$form_feild1 = param('$form_feild1');
$form_feild2 = param('$form_feild2');
$form_feild3 = param('$form_feild3');
$form_feild4 = param('$form_feild4');
$form_feild5 = param('$form_feild5');




# You can do some checks here to see if the information Submitted is valid..... EXAMPLE
if ($form_feild1 eq "") { print "You must enter something in <b>form feild 1</b>"; exit; }


# Get the path to teh DIR you want to save the file in
$my_files_path = "/path/to/where/you/want/file";

# The URl to the file.
$url_to_file = "[URL unfurl="true"]http://www.MySite.com/temp_files";[/URL]

# Generate a Temporay file name
$temp_file_name = time;
	
	# Create the file and write to it
	open(FILE, ">$temp_file_path/$temp_file_name\.txt");
	
	# Write to the FILE
	print FILE "$form_feild1\n";
	print FILE "$form_feild2\n";
	print FILE "$form_feild3\n";
	print FILE "$form_feild4\n";
	print FILE "$form_feild5\n";
	print FILE "$form_feild6";
	
	# Close the FILE
	close(FILE);

	
#Mail variables
$to = "me\@me.com";
$reply_to = "who\@who.com";
$from = "My Web Site";
$subject = "THE FORM SUBMISSIONS";

# Teh path to sendmail on your UNIX box
$send_mail = '/usr/lib/sendmail';



# Open SENDMAIL and do some sending...
open (MAIL, "|$send_mail -t");
print MAIL "To: $to\n";
print MAIL "Reply-to: $reply_to <$reply_to>\n";
print MAIL "From: $from <$from>\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-type: text/html\n\n";

# Send the info and / or link
print MAIL "Here is the information submitted from the web site.\n";

print MAIL "Form Feild 1 = $form_feild1\n";
print MAIL "Form Feild 2 = $form_feild2\n";
print MAIL "Form Feild 3 = $form_feild3\n";
print MAIL "Form Feild 4 = $form_feild4 \n";
print MAIL "Form Feild 5 = $form_feild5";

# Sned the URL to the file.
print MAIL "Here is a link to the file made on my web server\n";
print MAIL "<a href=\"$url_to_file/$temp_file_name\.txt\"> THE FILE MADE </a>\n";

# Close The MAIL PROG
close(MAIL);

print "All Done<br>.\n";
print "The file has been created and an email has been sent with a link to the file.\n";

exit;

I never tested it.. but she should work.. nothing too hard unless i made some spelling errors.

Drew
 
Thanks Drew.

To ZimZangZoom's question: I would like to send the file as an attachment.

Could you that part too...

- Thanks
 
Take a look at the following FAQs, especially the third one. These four FAQs go over a few ways to send attachments.

faq219-364
faq219-1563
faq219-2111
faq219-2901

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top