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
if I don't include the imap_fetchbody then apache crashes
Any help is greatly appreciate
MrBelfry
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