In PHP, IMAP Functions, what are the functions that download the attached files. I only want to download attached files if needed. I want to save bandwidth, but maybe some functions download them to get their information, or the structure of the email ? If I have an attachment of 2G, I don't want to download it, but want all the same to get the other part of the email (text parts, information about the email structure,...)
views:
25answers:
2
+2
A:
See imap_fetchbody
and imap_fetchstructure
. Messages with attachments are multi-part messages; you can tell imap_fetchbody
which part you want to fetch.
Artefacto
2010-07-07 11:15:36
I know that imap_fetchbody downloads the specified part of the email, so it downloads attachment if needed. But does imap_fetchstructure downloads the different parts in order to get the email structure (including the attachment files) ? (it's probably an obvious answer that I'll get, but I just want to be sure that I don't use twice as much bandwidth than needed)
Cedric
2010-07-07 11:35:41
@Ced No, the server determines the parts and send only the metadata.
Artefacto
2010-07-07 11:41:21
+1
A:
See this code, should help you understand better:
$structure = imap_fetchstructure($mailbox, $index);
$attachments = array();
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']) {
$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']);
}
}
} // for($i = 0; $i < count($structure->parts); $i++)
} // if(isset($structure->parts) && count($structure->parts))
MB34
2010-07-27 20:52:29