Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Attaching Files to E-mail

by She-wolf (Sexton)
on Sep 01, 2000 at 19:18 UTC ( [id://30733]=perlquestion: print w/replies, xml ) Need Help??

She-wolf has asked for the wisdom of the Perl Monks concerning the following question:

I need a way to have a program take an attachment specified by the user, upload it from their computer/workstation and send it to a set email address.
I've looked at MIME:Lite but it looks to me like it sends a preset attachment.
Is there any module or code chunk that could do this?
Thanks

She-wolf
"Wha? I don't get it."

Replies are listed 'Best First'.
Re: Attaching Files to E-mail
by agoth (Chaplain) on Sep 01, 2000 at 19:30 UTC
    This will do the sending, ie v. easy, as is the file upload bit, as KM, use CGI.

    sub send_with_attach { my $msg = new MIME::Lite From =>'', To =>'', Subject =>'Website File download', Type =>'multipart/mixed'; attach $msg Type =>'TEXT', Data =>'Downloaded files from website'; attach $msg Type =>'TEXT', Path =>'../conf/WWWCHI.TXT', Encoding =>'8bit', Filename =>'WWWCHI.TXT'; attach $msg Type =>'TEXT', Path =>'../conf/WWWCUS.TXT', Encoding =>'8bit', Filename =>'WWWCUS.TXT'; $msg->send; }
Re: Attaching Files to E-mail
by b (Beadle) on Sep 01, 2000 at 19:42 UTC
Re: Attaching Files to E-mail
by KM (Priest) on Sep 01, 2000 at 19:22 UTC
    Look again at MIME::Lite. You specify what the file is to send as an attachment. I would suggest giving it a try and reporting back the results :) But, you found the right tool to do the job. As for the uploading part, it looks like a job for CGI.pm. I believe there are examples for file uploading in the CGI.pm docs.

    Cheers,
    KM

Re: Attaching Files to E-mail
by merlyn (Sage) on Sep 01, 2000 at 19:58 UTC
Re: Attaching Files to E-mail
by She-wolf (Sexton) on Sep 01, 2000 at 19:52 UTC
    Hmm, let me be a little more specific.

    1. User fills out a form which includes their e-mail address and the path on their workstation/computer where the file is
    2. File(s) specified are sent as an attachment and NOT stored on the website server

    As for using the file upload section of CGI.pm, the author put warnings regarding several things I need to do:

    1. Large files tend to fail for netscape servers(We use netscape and the file(s) are going to be very large)
    2. For UNIX netscape the browser will hang, guess what we're on

    Thanks again

    She-wolf
    "Wha? I don't get it."

      First, be sure that you define the FORM tag in your HTML like this:
      <form method=post enctype="multipart/form-data"> # multipart/form-data + is necessary for uploads ... <input type="file" name="file"> # This defines an upload field
      Processing the query then should be easy with MIME:Lite:
      # Create new Mail object. my $mail = MIME::LITE->new( From => $from, To => $to, Subject => $subject, Type => 'multipart/mixed' ); # 'Attach' text to it. $mail->attach( TYPE => 'TEXT', DATA => $messagebody ); # If we have an attachment, attach it. if ($q->param("file")) { my $upload = $q->upload; my $fh = $upload->fh; my @data; binmode $fh; local $_ = ''; while (read($fh, $_, 1024)) { push @data, $_; } close $fh; $mail->attach( Data => \@data, Filename => $upload->filename, Type => $upload->type ); } # Finally, send the mail. open (MAIL, "| /usr/sbin/sendmail -t -i") or die $!; $mail->print(\*MAIL); close MAIL;
      We have pointed you to a variety of options. What exactly have you tried and failed? Did you try CGI.pm's upload and have it fail? IMO, a good thing to do it try what is readily available, suggested, and tested first. Then, if something doesn't work (browser hangs, files are too large, etc..) let us know and we can try to think up other ways. But, generally, nothing you are wanting to do can not be done with CGI.pm and MIME::Lite.

      Cheers,
      KM

      I don't really understand the purpose of this script. The user enters their own email address and the path to a file. The script then sends a copy of that file to the user's email address. Why?

      At any rate, it seems like the easiest solution would be to allow the user to open up a mail client (like messenger that comes with communicator in Linux) and attach files that way.

      Either you are going to have to get into the nitty gritty of how multipart forms send stuff and read it and fix some problems that even (bows head in reverence)Lincoln Stein has not solved yet, or you are going to have to allow the user to upload the file to the server.

      It'd be easier to write a web page with pretty pictures that would explain how to do this than to write a script that acts as a mediator.

        Sorry, I voted you --. Seems you never heard of Web based eMail-clients.

        Of course, the attachments get stored on the server, but only temporarily.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://30733]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found