Gossamer Forum
Home : General : Perl Programming :

Getting an attachement from form data and emailing it

Quote Reply
Getting an attachement from form data and emailing it
Ok guys, last time I posted a question here I got the answer I needed within a day Smile. Lets see if we can do this again Wink.

Here goes:

I need to figure out how to have my script accept form content and an image (or another type of document, in various formats) from an HTML form and then build and send that message along with the attachement to the desired sender.

I already have a script that gets the from data (no attachement) and sends it along but it accepts data using the 'GET' method.

I figure I need to use method 'POST' and enctype 'multipart/form-data' to send the attachement to my script, but how does the script handle the data once it has it?

Note that I do not want to save the file anywhere, I just need get it from the form and email it.

Any help would be great.

Thanks.
Quote Reply
Re: [Crolguvar] Getting an attachement from form data and emailing it In reply to
Quote:
I figure I need to use method 'POST' and enctype 'multipart/form-data' to send the attachement to my script,

Correct.

Quote:
but how does the script handle the data once it has it?

Code:
my $file_field = $IN->param('file_field_name');
my $buffer;
while (<$file_field>) {
$buffer .= $_;
}

That gets the file content into $buffer. That is a very basic example. I've left all the error checking etc, to you.

To email the content I'd probably write to a temporary file and then send it with MIME::Lite (see cpan.org) then delete the temp file.
Quote Reply
Re: [Paul] Getting an attachement from form data and emailing it In reply to
Thanks Paul, I'll give that a try.

Ideally however I would need a solution where I am not required to save the file (if that is possible) since the production server where this script resides is not hosted by my organization and they might not take too kindly to me creating and removing files this way.

I will test this on my dev server however since that is all mine Cool...

Again, thanks for the prompt reply.
Quote Reply
Re: [Crolguvar] Getting an attachement from form data and emailing it In reply to
MIME::Lite may allow you to pass in a buffer, I just looked quickly and saw an example where it wanted a file name.

You may have some luck here:

http://search.cpan.org/...ite/lib/MIME/Lite.pm