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!


In reply to How do I fetch and send mail attachments? by Seeker100

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.