i had to write a script for a company that led me towards MIME::Lite
The company wanted a form that would allow people to submit/upload resume
files; and have the script email those resumes to their
personnel department. sendmail was not a requirement, so i used
the company's SMTP server (so that it would work on both Win2k/linux)
on the form, there is a field named 'resume' that is of type 'FILE'.
when the user submits the form, i read in the data submitted in
the form field 'resume' and put it in the hash key 'resume_data'.
Then, i break out MIME::Lite and attach the data contained in that
hash key to the email message.
This enables me to send an email with the user's resume attached
without ever having to save the resume to a file. ;)
keep in mind that the following code chunks are ripped out of the
larger script... so it might not work as expected if you just
cut-n-paste the code
#$i is an object i declared previously
#read in all values (using CGI.pm)
my @names = ¶m();
foreach my $field (@names) {
$FORM{$field} = ¶m($field);
}
#read in the data uploaded, and save it into a hashkey
if ($FORM{'resume'}) {
#read in the resume's data
my ($data,$size);
while( $size = read( $FORM{'resume'}, $data, 1024) ) {
$FORM{'resume_data'} .= $data;
$FORM{'resume_size'} += $size;
}
}
#----
#resume is the form field of type 'FILE'
$FORM{'resume'} =~ m!([^/:\\]*)$!; #get file name/ext
my $shortname = $1; #file name (with no slashes or colons
+)
#----
#create a new message
my $msg = MIME::Lite->new(
From => "$FORM{'email'}",
To => "personnel\@company.com",
Subject => "Resume",
Type => 'TEXT',
Data => "Resume is <attached> to this email as <$shortname>",
) || return $i->error("Cannot create new email message object!");
#attach resume data
$msg->attach(
Type => 'application/octet-stream',
Encoding => 'base64',
Data => $FORM{'resume_data'},
Filename => $shortname,
) || return $i->error("Cannot attach your resume to the email!");
#tell MIME::Lite how to send this email
MIME::Lite->send('smtp', 'company.com', Timeout=>30)
|| return $i->error("Unable to set the email 'send' properties!");
#actually send it
$msg->send()
|| return $i->error("Unable to send email with your resume!");
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.