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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

imap_savebody problem 1

Status
Not open for further replies.

MrBelfry

IS-IT--Management
May 21, 2003
289
hi there

I clearly out of my depth so need some really simple advice. I have a little script that logs into an email server, looks through some emails and then should download some attachments onto a webserver.

I can do all of the above but when I try and open the files I get errors to do with decoding etc - i.e the attachments won't open. I cobbled the script together from various internet stuff so i'm fairly sure I'm doing something obviously wrong that I'm too stupid to decipher

Code:
function extract_attachments($connection, $message_number) {
   
    $attachments = array();
    $structure = imap_fetchstructure($connection, $message_number);
   
    if(isset($structure->parts) && count($structure->parts)) {
   
        for($i = 0; $i < count($structure->parts); $i++) {
   
            $attachments[$i] = array(
                'is_attachment' => false,
                'filename' => '',
                'name' => '',
                'attachment' => ''
            );
           
            if($structure->parts[$i]->ifdparameters) {
                foreach($structure->parts[$i]->dparameters as $object) {
                    if(strtolower($object->attribute) == 'filename') {
                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i]['filename'] = $object->value;
                    }
                }
            }
           
            if($structure->parts[$i]->ifparameters) {
                foreach($structure->parts[$i]->parameters as $object) {
                    if(strtolower($object->attribute) == 'name') {
                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i]['name'] = $object->value;
                    }
                }
            }
           
            if($attachments[$i]['is_attachment']) {
                $file = ATTACHMENT_PATH . $attachments[$i]['filename'];
				$attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i+1);
				
                /*if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                    $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                }
                elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                    $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                }*/
                imap_savebody($connection, $file, $message_number, $i+1);
            }
            
           
        }
       
    }
   
    return $attachments;
   
}

if I don't include the imap_fetchbody then apache crashes

Any help is greatly appreciate

MrBelfry
 
how interesting. i wrote a mail class just yesterday.

why are using imap_savebody if you already have the body from imap_fetchbody?

instead of imap_savebody i would just use

Code:
file_put_contents($file, $attachments[$i]['attachment']);

in this way they data is already decoded before saving so all should be well.

imap_save_body, i think, i used for keeping local copies of complete emails. for web app emails, I certainly have never used it.

make sure, of course, that the filename is unique before using file_put_contents on it. you might want to test with file_exists($file) and then, if the file does exist, add some uniqueness to the filename.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top