knudde has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody,
I have a form that works with a cgi-handler in perl
It emails the data that people fill in. Now I want to receive that data directly in a excel-sheet and emailed to me.
Is this possible? And how do you do it?

cheers & happy holidays!
Knudde Günther
this is my code in the cgi handler

#!/usr/bin/perl # Copyright 2004 Boutell.Com, Inc. Compatible with our earlier C progr +am. use CGI; my $sendmail = "/usr/sbin/sendmail"; my $emailConfPath = "nieuwsbrief.conf"; my $query = new CGI; my $recipient = &clean($query->param('recipient')); my $subject = &clean($query->param('subject')); my $nieuwsbriefnaam = &clean($query->param('nieuwsbriefnaam')); my $nieuwsbriefemail = &clean($query->param('nieuwsbriefemail')); if (($nieuwsbriefnaam eq "") || ($nieuwsbriefemail eq "")) { &error("Email Rejected", "Please fill out all fields provided. Back up to the " . "previous page to try again."); } if (!open(IN, "$emailConfPath")) { &error("Configuration Error", "The file $emailConfPath does not exist or cannot be " . "opened. Please read the documentation before installing " . "email.cgi."); } my $returnpage; my $ok = 0; while (1) { my $recipientc = <IN>; $recipientc =~ s/\s+$//; if ($recipientc eq "") { last; } my $returnpagec = <IN>; $returnpagec =~ s/\s+$//; if ($returnpagec eq "") { last; } if ($recipientc eq $recipient) { $ok = 1; $returnpage = $returnpagec; last; } } close(IN); if (!$ok) { &error("Email Rejected", "The requested destination address is not one of " . "the permitted email recipients. Please read the " . "documentation before installing email.cgi."); } # Open a pipe to the sendmail program open(OUT, "|$sendmail -t"); # Use the highly convenient <<EOM notation to include the message # in this script more or less as it will actually appear print OUT <<EOM To: $recipient Subject: $nieuwsbriefnaam $subject Reply-To: $email Supposedly-From: $name $nieuwsbriefnaam $subject Naam: $nieuwsbriefnaam Emailadres: $nieuwsbriefemail EOM ; close(OUT); # Now redirect to the appropriate "landing" page for this recipient. print $query->redirect($returnpage); exit 0; sub clean { # Clean up any leading and trailing whitespace # using regular expressions. my $s = shift @_; $s =~ s/^\s+//; $s =~ s/\s+$//; return $s; } sub error { # Output a valid HTML page as an error message my($title, $content) = @_; print $query->header; print <<EOM <html> <head> <title>$title</title> </head> <body> <h1 align="center">$title</h1> <p> $content </p> EOM ; exit 0; }

Replies are listed 'Best First'.
Re: excel sheet as attachment
by Corion (Patriarch) on Dec 21, 2009 at 15:38 UTC
Re: excel sheet as attachment
by Sinistral (Monsignor) on Dec 21, 2009 at 15:43 UTC

    Although it isn't incorrect to hand craft e-mail (even with attachments), it's much easier if you let someone else worry about the niggling details. I recommend you use Email::MIME to do the construction, print it as a string, then send that to your invocation to Sendmail.

    You specifically want to send an attachment that's an Excel compatible file. The very simplest is to make a CSV file and set the MIME type to text/csv. Excel will handle that gracefully when the recipient gets the e-mail using their favorite e-mail client. If you want to go more advanced, then Spreadsheet::WriteExcel will give you the ability to create an actual binary Excel file that can be attached as the MIME type application/vnd.ms-excel.

Re: excel sheet as attachment
by knudde (Initiate) on Dec 22, 2009 at 12:42 UTC

    Hello Corion and Sinistral,

    I don't see a start to change the source code.
    I have more experience with c and java but to perl I'm a newbie...
    Can you give a little example...
    Thank you!

    Happy holidays!
    Knudde Günther

        Thanks John, i'll give it a try! Knudde