|
Posted by Rik on 01/13/07 13:51
Manfred Kooistra wrote:
> Thank you so far.
>
> To make this more concrete:
> I am trying to send a variable number of attachments with PEAR::Mail.
>
> With a fixed number of attachments (example with 1 file), the relevant
> part of the code was this:
>
> $mime->addAttachment($file, "image/$type_of_file",
> $_FILES['upfile_0']['name']);
>
> Now, with an unknown number of files, I changed that part of the code
> to (exactly as in my script):
>
> <SNIP>
> $_POST_DATA is stuff processed through a Perl script. Files are named
> upfile_0, upfile_1 etc. The data is all there, I checked it. I can
> even output $file to the screen, but $mime->addAttachment seems not
> do do anything, because the emails arrive without attachment (all
> other content is okay, since the rest of the [rather very long, so
> not posted here] script is unchanged).
>
> It seems to me that somehow the foreach and the -> don't work well
> together, because I have the same foreach loop for the email content,
> where $text .= $sometextfragment; works fine.
There is absolutely no problem in using objects within a loop. It's the
logic that is faulty. I do not use the PEAR::Mail class, but I suspect this
will solve your problem (untested, might be one or two typos):
function return_mime($filename,$fallback){
if(function_exists('finfo_file')){
$finfo = finfo_open(FILEINFO_MIME);
return finfo_file($finfo, $filename);
}
$mime_magic = mime_content_type($filename);
return ($mime_magic==false) ? $fallback : $mime_magic;
}
foreach($_FILES as $file){
if($file['error'] == UPLOAD_ERR_OK){
$mime->addAttachment($file['tmp_name'],
return_mime($file['name'],$file['type']),$file['tmp_name']);
} else {
trigger_error("{$file['name'] is not uploaded correctly.");
}
}
I do not know how your PERL script works, or what it does, so I have not
used that. You should be able te get it to work using this example though.
--
Rik Wasmus
[Back to original message]
|