
Alpha1980
Novice
Feb 25, 2007, 7:59 AM
Post #8 of 9
(614 views)
|
|
Re: [KevinR] Create and amend tab delimited .txt from Perl
[In reply to]
|
Can't Post
|
|
Thanks once again Kevin. :) I've got it working with the following code:
#!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><h1>Hello!</h1></html>\n"; use strict; use warnings; use CGI; my $query = new CGI; my $title = $query->param('title'); my $surname = $query->param('surname'); my $firstname = $query->param('firstname'); open(TEXT,">>$surname".time. '.txt') or die "$!"; print TEXT "Title\tSurname\tFirst Name\n"; print TEXT "$title\t$surname\t$firstname\n"; close (TEXT); use MIME::Lite; # set up email my $to = "standard\@example.co.uk"; my $from = "admin\@example.co.uk"; my $subject = "$surname".time; my $message = "This email was sent from Perl"; my $file = "$surname".time.".txt"; # send email email($to, $from, $subject, $message, $file); # email function sub email { # create a new message my $msg = MIME::Lite->new( From => $from, To => $to, Subject => $subject, Data => $message ); # add the attachment $msg->attach( Type => "text/plain", Path => $file, Filename => $file, Disposition => "attachment" ); # send the email MIME::Lite->send('smtp', 'mail.example.co.uk', Timeout => 60); $msg->send(); } I've taken out this piece of code:
{ # get incoming parameters local ($to, $from, $subject, $message, $file) = @_; because it was returning the error:
Can't localize lexical variable $to at test.pl and I wasn't sure what that mean't or what this code even does. Anyway, there is one final thing I need to do. I have the following script which generates a .pdf containing $title, $surname and $firstname.
#!/usr/bin/perl use CGI; $query = new CGI; @names = $query->param; $title = $query->param(title); $surname = $query->param(surname); $firstname = $query->param(firstname); $message =~ s/\r//g; $message =~ s/\n\n/<br\/><br\/>/g; print "Content-Disposition: attachment;filename=\"letter.pdf\"\n"; print "Content-type: application/pdf\n\n"; if (! open(PRINCE, "| /usr/bin/prince -")) { print "Content-type: text/plain\n\n"; print "Error starting Prince\n"; exit; } print "Content-Disposition: attachment;filename=\"letter.pdf\"\n"; print "Content-type: application/pdf\n\n"; $margin="0"; print PRINCE <<"EOF"; <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> \@page { size: letter; margin: 10% 15%; } \@media screen { body { margin: 3em } } div.letter { text-align: center; font: bold 17pt "Lega", "Georgia", serif; margin-bottom: 3em; } body { font: 11pt "Gill Sans", "Chantilly", "Georgia", sans-serif } /* IE6 and older does not support the 'white-space' property. In other browsers, the line below can be uncommented and those pesky <br/> tags can be removed */ /* div.letter { white-space: pre } */ </style> </head> <body> <div class="letter"> $title<br/> $surname<br/> $firstname<br/> </div> </body> </html> EOF close PRINCE; I want to add three radio inputs to the webform that produces $title, $surname and $firstname. The first radio should create the .txt file and send the email. The second radio should create the .pdf. And the third radio should do both. I'd really appreciate a push in the right direction because I don't know where to start with this one. Many Thanks :)
|