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

Hello,

Would you be so kind as to help me out?

I'm trying to set up a web page through which visitors can send me emails with attachments, and then use Perl behind the scenes to process the incoming content and send everything to me. With the code below (simplified for asking this question), I've succeeded in attaching and sending files already existing on the web server.

use Net::SMTP; use strict; use warnings; use MIME::Base64 qw( encode_base64 ); print "Content-type: text/html\n\n"; print "<html>\n<head>\n</head>\n<body>\n"; my $from = 'tester@mydomain.com'; my $to = 'myalias@mydomain.com'; my $attachBinaryFileName = 'test.jpg'; my $attachTextFileName = 'testattach.txt'; my $boundary = 'frontier'; my $smtp = Net::SMTP->new('mail.mydomain.com', Timeout => 10) || die(" +Could not create SMTP object."); $smtp->mail($from); $smtp->recipient($to, { SkipBad => 1 }); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: Multi part test\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundar +y\"\n"); #$smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-type: text/plain\n"); $smtp->datasend("Content-Disposition: quoted-printable\n"); $smtp->datasend("\nToday\'s files are attached:\n"); $smtp->datasend("\nHave a nice day! :)\n"); $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: application/text; name=\"$attachTextFil +eName\"\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$attachTe +xtFileName\"\n"); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFileNam +e\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$attachBi +naryFileName\"\n"); $smtp->datasend("\n"); my $buf; open(DAT, "$attachBinaryFileName") || die("Could not open binary file! +"); binmode(DAT); local $/=undef; while (read(DAT, my $binaryfile, 114)) { $buf = &encode_base64( $binaryfile ); $smtp->datasend($buf); } close(DAT); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->dataend(); $smtp->quit;

What I'm having trouble with, is understanding how to fetch and send files that a visitor selects from their local computer. I don't know if I'm using the right Perl modules or right html form elements and what to put in the Perl script to fetch and attach the data.

On the html form, I've got:

<form action="mailscript.pl" method="post" enctype="multipart/form-dat +a"> <p>Name: <input type="text" name="sender_name" /></p> <p>Upload file 1: <input type="file" name="file1" /></p> <p>Upload file 2: <input type="file" name="file2" /></p> <p>Your Email Address: <input type="text" name="email_address" /></p> <p><input type="submit" name="Submit" value="Submit Form" /></p> </form>

What are your suggestions? Thanks in advance!

Replies are listed 'Best First'.
Re: How do I fetch and send mail attachments?
by hippo (Archbishop) on Jun 11, 2019 at 14:49 UTC
      Hmmm... are you saying I need to first upload the file to the server and then send it as a mail attachment? That would make sense. :-) Cheers!

        Maybe you could send an email without attachments but with links to download the actual files directly from the server.

        poj
Re: How do I fetch and send mail attachments?
by FreeBeerReekingMonk (Deacon) on Jun 14, 2019 at 00:23 UTC
    Just to say that you can also convert the files into base64 text strings on the client side using Javascript and send them in your form as just a text field, and you can email yourself those base64 strings, which you will need to convert back later, or if your mailclient is intelligent, it will autodetect attachments. (or mark the email as spam/virus).

    But uploading the files to your server first, virus scan them, and only then email it to you when clean is safer...

Re: How do I fetch and send mail attachments?
by Anonymous Monk on Jun 10, 2019 at 21:22 UTC
      Hi there, I checked the package thoroughly but couldn't find anything with sending attachments. Did I miss something? Cheers!

        I think the curt reply by Anonymous Monk is more about using a "proper" mail send form, for which the linked Tfmail isn't all bad.

        About sending mail, I'm quite fond of MIME::Lite, which works well for creating mails with attachments.