limap/Notes.md
2023-01-10 00:39:11 +01:00

4.7 KiB

How to identify partno string

imap-fetchbody() will decode attached email messages inline with the rest of the email parts, however the way it works when handling attached email messages is inconsistent with the main email message.

With an email message that only has a text body and does not have any mime attachments, imap-fetchbody() will return the following for each requested part number:

(empty) - Entire message 0 - Message header 1 - Body text

With an email message that is a multi-part message in MIME format, and contains the message text in plain text and HTML, and has a file.ext attachment, imap-fetchbody() will return something like the following for each requested part number:

(empty) - Entire message 0 - Message header 1 - MULTIPART/ALTERNATIVE 1.1 - TEXT/PLAIN 1.2 - TEXT/HTML 2 - file.ext

Now if you attach the above email to an email with the message text in plain text and HTML, imap_fetchbody() will use this type of part number system:

(empty) - Entire message 0 - Message header 1 - MULTIPART/ALTERNATIVE 1.1 - TEXT/PLAIN 1.2 - TEXT/HTML 2 - MESSAGE/RFC822 (entire attached message) 2.0 - Attached message header 2.1 - TEXT/PLAIN 2.2 - TEXT/HTML 2.3 - file.ext

Note that the file.ext is on the same level now as the plain text and HTML, and that there is no way to access the MULTIPART/ALTERNATIVE in the attached message.

Here is a modified version of some of the code from previous posts that will build an easily accessible array that includes accessible attached message parts and the message body if there aren't multipart mimes. The $structure variable is the output of the imap_fetchstructure() function. The returned $part_array has the field 'part_number' which contains the part number to be fed directly into the imap_fetchbody() function.

parts) > 0) { // There some sub parts foreach ($structure->parts as $count => $part) { add_part_to_array($part, $prefix.($count+1), $part_array); } }else{ // Email does not have a seperate mime attachment for text $part_array[] = array('part_number' => $prefix.'1', 'part_object' => $obj); } return $part_array; } // Sub function for create_part_array(). Only called by create_part_array() and itself. function add_part_to_array($obj, $partno, & $part_array) { $part_array[] = array('part_number' => $partno, 'part_object' => $obj); if ($obj->type == 2) { // Check to see if the part is an attached email message, as in the RFC-822 type //print_r($obj); if (sizeof($obj->parts) > 0) { // Check to see if the email has parts foreach ($obj->parts as $count => $part) { // Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments if (sizeof($part->parts) > 0) { foreach ($part->parts as $count2 => $part2) { add_part_to_array($part2, $partno.".".($count2+1), $part_array); } }else{ // Attached email does not have a seperate mime attachment for text $part_array[] = array('part_number' => $partno.'.'.($count+1), 'part_object' => $obj); } } }else{ // Not sure if this is possible $part_array[] = array('part_number' => $prefix.'.1', 'part_object' => $obj); } }else{ // If there are more sub-parts, expand them out. if (sizeof($obj->parts) > 0) { foreach ($obj->parts as $count => $p) { add_part_to_array($p, $partno.".".($count+1), $part_array); } } } } ?>

body type

#define TYPETEXT 0        /* unformatted text */
#define TYPEMULTIPART 1        /* multiple part */
#define TYPEMESSAGE 2        /* encapsulated message */
#define TYPEAPPLICATION 3    /* application data */
#define TYPEAUDIO 4        /* audio */
#define TYPEIMAGE 5        /* static image */
#define TYPEVIDEO 6        /* video */
#define TYPEMODEL 7        /* model */
#define TYPEOTHER 8        /* unknown */
#define TYPEMAX 15        /* maximum type code */

stream flags

local OP_DEBUG = 0x1
local OP_READONLY = 0x2
local OP_ANONYMOUS = 0x4    --/* anonymous open of newsgroup */
local OP_SHORTCACHE = 0x8 --/* short (elt-only) caching */
local OP_SILENT = 0x10    --/* don't pass up events (internal use) */
local OP_PROTOTYPE = 0x20 --/* return driver prototype */
local OP_HALFOPEN = 0x40    --/* half-open (IMAP connect but no select) */
local OP_EXPUNGE= 0x80    --/* silently expunge recycle stream */