×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

MIME::Lite attachments

MIME::Lite attachments

MIME::Lite attachments

(OP)
the following code works for a single attachment as defined in the script (Type => 'application/pdf').

CODE

my $msg = MIME::Lite->new(From    => email@email.com',
                       To      => email@email.com',
                       Subject => 'My File Tester',
                       Type    => 'multipart/mixed');

$msg->attach(Type        => 'application/pdf',
             Path        => '../file.pdf',
             Filename    => file.pdf',
             Disposition => 'attachment');

$msg->send( );




however, i would like to allow users to upload an attachement so it will not always be a pdf file.

how can i modify the code to allow multiple attachment types (i can get the path and filename from the upload parameter)?



i haven't tried it, but i believe it would work with a dropdown box for the user to select the file type (pdf, doc, xls, gif, jpg, etc.).  the value associated with the selection could be passed to the script.


CODE

<option value="image/jpg">jpg


two issues -
1. it seems cheesy to make the user upload the file and tell me what type it is
2. in the case of jpg - if i use type => image/jpg, would it make a difference if the extension is actually jpeg?

thanks.

 

RE: MIME::Lite attachments

If the user uploads a file that is then attached to an email, first the file uplaod is done. Your script should know the path to that file and the name of the file, you would then use that information to attach the file to the email instead of hard coding it like the example you posted.

If you want them to attach multiple files you just use a loop to keep adding attachments to the email.

The 'Type' header is not essential to the attachment process but you can probably determine the Type dynamically by the extension of the uploaded file. Or use a module that can try and determine a files MIME type. MIME::Lite might also have a builtin function/method that can do that, check the documentation.

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

RE: MIME::Lite attachments

(OP)


thanks for the info.

in following your advice i came up with the code below, but get a 500 error and not the thank you page.  if i remove the attachment portion of the code, the code works fine - that is it sends the email with correct info (no attachment of course) and goes to the thank you page.

also, even with the 500 error, the email is sent, but the attachment isn't really there.  the filename shows in the email attachment line, but won't open.

by the way, i have tried with and without the quotes around the path and filename variables.

CODE

 
$subject = $cgi->param('subject');
$emailmsg = $cgi->param('emailmsg');
$upload1 = $cgi->param('upload1');

### Isolate the attachment name from the path
if ($upload1) {
    chomp($upload1);
    $filename = $upload1;
    $fn = reverse $filename;
    $dot = 0;
    while($dot eq 0) {
        chop($fn);
        if(not($fn =~ /\\/i)) { $dot = 1; }
        if($fn eq "") { $dot = 1; }
    }
    $filename = reverse $fn;
}
my $msg = MIME::Lite->new(From    => 'mymail@mymail.com',
                       To      => $type,
                       Subject => $subject,
                       Type    => 'multipart/mixed');

### Add the text message part
$msg->attach (
  Type => 'TEXT',
  Data => $emailmsg
);

### Add the attachment
$msg->attach(Path        => "$upload1",
             Filename    => "$filename",
             Disposition => 'attachment');

$msg->send();
 

thanks.

 

RE: MIME::Lite attachments

Hard to say, that is only part of your code and it looks like you're not using "strict" to eliminate typos and other hard to find errors that could be tripping your script up.

Add this line for now:

CODE

use CGI::Carp qw/fatalsToBrowser/;

and see if the 500 error resolves to something you can pin point.

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

RE: MIME::Lite attachments

(OP)
i received this error:

CODE

 
Software Error:
filename.gif: not readable
 

notice that the filename doesn't include the entire path.  this is the problem.  after investigation i found that from the initial upload form, the value of the filename is being passed to the script as only the filename - the full path isn't included.  so the script is looking for the file in the directory of the script - on the web server.

my form looks like this:

CODE

 
<form method=post action=mailattach ENCTYPE="multipart/form-data"><b>Send To:</b><br>
    <select name=email class=input>
<option selected value="">Select
<option value="email@email.com">Email Option</select><br><br>
<b>Subject:</b><br>
    <input type=text name=subject size=80 class=input><br><br>
<b>Message:</b><br>
    <textarea name=emailmsg cols=80 rows=25 class=input></textarea><br><br>
<b>Attachment:</b><br>
<INPUT TYPE="file" NAME="upload1" size=50 class=input><br><br>

    <input type=submit value="Send E-Mail" class=input>
    </form>
 

any suggestions on the cause??

thanks.



 

RE: MIME::Lite attachments

Nothing to do with the form. Find where the server uploads the file to and use the path to the file on the server after it is uploaded.

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

RE: MIME::Lite attachments

(OP)

i couldn't find where the file was being uploaded to so i used the following code to save the file where i want it - i remove the file after the email.

CODE

 
$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
my $name = $2;
$path = "../../ads/$name";
open(LOCAL, ">$path") or fatalError("$path $!");
binmode LOCAL;
while(<$file>) {
print LOCAL $_;
}
close(LOCAL);
 

This works with respect to saving the file with the correct filename and when i download it from the server it opens correctly.  

also the email is sent, i receive the success message and the email contains the attachment (with correct name).  however, the file size is much smaller than the actual file and when i try to open (or save and open) i get an error that the file is damaged and can't be repaired.

after many trials and much hair pulling, i entered the Type header as 'application/pdf'.  Problem solved.

the file is saved, sent with the email, opens from the email, etc.  i have emailed txt, image and pdf files so far all with Type header pdf.

obviously i don't understand everything about this, but it works so it stays as is.

FYI -
during my fights with this program i found something interesting.  if i execute the script from IE it works per the script above, but if i use FireFox, it sends only the filename to the script - not the full path from the file location on my PC.  this mucks up the script so i had to add a line to the script above

CODE

 
$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
my $name = $2;  #this var isn't populated in firefox

######################
$name = "$file" if(!$name);
######################

$path = "../../ads/$name";
open(LOCAL, ">$path") or fatalError("$path - $file - $name $!");
binmode LOCAL;
while(<$file>) {
print LOCAL $_;
}
close(LOCAL);
 

so far this works for both IE and FireFox.  luckily the form is in the site admin so i will be in control of its use.

thanks for the help.

 

RE: MIME::Lite attachments

Well done :)

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close