Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MIME::Lite attachments

Status
Not open for further replies.

ibjdt

Programmer
Nov 25, 2002
63
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.

 
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]
 


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.

 
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]
 
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.



 
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]
 

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.

 
Well done :)

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top