OK, did a little digging and it seems that it is not (necessarily) PHP that
doesn't support the x.HEADER/x.TEXT syntax... The c-client function called
by imap_fetchbody function calls mail_fetchbody_full (alias for
mail_fetch_body) which _does not_ support the above syntax since it only
expects integers in the section specification. The x.0 syntax for
retrieving the header is supported through a special check (read 'hack') in
mail_fetch_body, and appears as though it only exists for BC. In fact, that
special case calls mail_fetch_header the same as the funtion I posted does.
However, if you wanted c-client/IMAP to filter the header fields returned to
you (ie. you only want From and To headers), you cannot do that with
imap_fetchbody, even though mail_fetch_header supports it. To do that with
the new function you could do the following:
$headers = imap_fetch_header($mbox, 1, '2', Array('To', 'From'), FT_PEEK);
Anyway, I have attached an updated diff with a couple of minor modifications
like c-style comments, tab indenting. If anyone thinks this is work
committing, feel free.
-Brad
Markus Fischer wrote:
> On Wed, Feb 27, 2002 at 12:16:47PM +0900, Yasuo Ohgaki wrote :
> > Brad Fisher wrote:
> > >This is a function I wrote a while back so I could access more of the
> > >headers available in a mail message. The imap_headers and
> > >imap_fetchheaders functions work fine for retrieving headers for the
> > >main body of a given message, but if you receive an email with an
> > >attachment which is itself an email message (ie. mime type
> > >'message/rfc822'), you can use this function to retrieve the (raw)
> >
> >
> > Could you post unified diff against latest CVS? (HEAD branch)
>
> "I don't think it is the c-Client lib that is broken here,
> but the PHP implementation."
>
> If that's true and someone can actually verify this I wold
> not hesitate and commit this but first fix the extension if
> the c-client library really supports it already.
>
> --
> Please always Cc to me when replying to me on the lists.
> GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Index: ext/imap/php_imap.c
===================================================================
RCS file: /repository/php4/ext/imap/php_imap.c,v
retrieving revision 1.111
diff -u -r1.111 php_imap.c
--- ext/imap/php_imap.c 12 Jan 2002 05:39:07 -0000 1.111
+++ ext/imap/php_imap.c 28 Feb 2002 00:40:44 -0000
@@ -91,6 +91,7 @@
PHP_FE(imap_bodystruct, NULL)
PHP_FE(imap_fetchbody, NULL)
PHP_FE(imap_fetchheader, NULL)
+ PHP_FE(imap_fetch_header, NULL)
PHP_FE(imap_fetchstructure, NULL)
PHP_FE(imap_expunge, NULL)
PHP_FE(imap_delete,
NULL)
@@ -2582,6 +2583,92 @@
RETVAL_STRING(mail_fetchheader_full(imap_le_struct->imap_stream,
Z_LVAL_PP(msgno), NIL, NIL, (myargc == 3 ? Z_LVAL_PP(flags) : NIL)), 1);
}
/* }}} */
+
+
+
+/* {{{ proto string imap_fetch_header(int stream_id, int msg_no [, string section [,
+array lines [, int options]]])
+ Get the (optionally filtered) header for a given section of a message */
+PHP_FUNCTION(imap_fetch_header)
+{
+/*
+ NOTE: You need to explicitly set FT_PEEK to not mark message as READ when calling
+this function.
+ Empty strings are ignored in the lines array.
+*/
+ zval **streamind, **msgno, **sec = NIL, **lines = NIL, **flags, **tmp;
+ int i, msgindex, myargc = ZEND_NUM_ARGS();
+ pils *imap_le_struct;
+ HashPosition pos;
+ STRINGLIST *cur_lines = NIL, *cur;
+
+ if (myargc < 2 ||
+ myargc > 5 ||
+ zend_get_parameters_ex(myargc, &streamind, &msgno, &sec, &lines,
+&flags) == FAILURE)
+ {
+ ZEND_WRONG_PARAM_COUNT();
+ } /*if*/
+
+ convert_to_long_ex(msgno);
+
+ if (myargc > 2) {
+ convert_to_string_ex(sec);
+ } /*if*/
+
+ /* Convert the <lines> array to a STRINGLIST */
+ if (myargc > 3) {
+ convert_to_array_ex(lines);
+
+ zend_hash_internal_pointer_reset_ex((*lines)->value.ht, &pos);
+ while (zend_hash_get_current_data_ex((*lines)->value.ht, (void **)
+&tmp, &pos) == SUCCESS) {
+ convert_to_string_ex(tmp);
+
+ /* Only add to the filter list if not an empty string */
+ if (*Z_STRVAL_PP(tmp)) {
+ if (cur_lines) {
+ cur->next = mail_newstringlist();
+ cur = cur->next;
+ } else {
+ cur_lines = mail_newstringlist();
+ cur = cur_lines;
+ } /*if*/
+
+ cur->LSIZE = (*tmp)->value.str.len;
+ cur->LTEXT = (char*) cpystr((*tmp)->value.str.val);
+ } /*if*/
+
+ zend_hash_move_forward_ex((*lines)->value.ht, &pos);
+ } /*while*/
+ } /*if*/
+
+ if (myargc == 5) {
+ convert_to_long_ex(flags);
+ } /*if*/
+
+ ZEND_FETCH_RESOURCE(imap_le_struct, pils *, streamind, -1, "imap", le_imap);
+
+ if ((myargc == 5) && (Z_LVAL_PP(flags) & FT_UID)) {
+ /* This should be cached; if it causes an extra RTT to the
+ IMAP server, then that's the price we pay for making sure
+ we don't crash. */
+ msgindex = mail_msgno(imap_le_struct->imap_stream, Z_LVAL_PP(msgno));
+ } else {
+ msgindex = Z_LVAL_PP(msgno);
+ } /*if*/
+
+ if ((msgindex < 1) || ((unsigned) msgindex >
+imap_le_struct->imap_stream->nmsgs)) {
+ php_error(E_WARNING, "Bad message number");
+ RETURN_FALSE;
+ } /*if*/
+
+ /* Perform the header fetch */
+ RETVAL_STRING(mail_fetch_header(imap_le_struct->imap_stream, Z_LVAL_PP(msgno),
+Z_STRVAL_PP(sec),
+ cur_lines,
+NIL, myargc==5 ? Z_LVAL_PP(flags) : NIL), 1);
+
+ /* Free the temporary string list */
+ mail_free_stringlist(&cur_lines);
+} /* PHP_FUNCTION(imap_fetch_header) */
+/* }}} */
+
+
/* {{{ proto int imap_uid(int stream_id, int msg_no)
Get the unique message id associated with a standard sequential message number */
Index: ext/imap/php_imap.h
===================================================================
RCS file: /repository/php4/ext/imap/php_imap.h,v
retrieving revision 1.16
diff -u -r1.16 php_imap.h
--- ext/imap/php_imap.h 11 Dec 2001 15:29:38 -0000 1.16
+++ ext/imap/php_imap.h 28 Feb 2002 00:40:44 -0000
@@ -149,6 +149,7 @@
PHP_FUNCTION(imap_setflag_full);
PHP_FUNCTION(imap_clearflag_full);
PHP_FUNCTION(imap_sort);
+PHP_FUNCTION(imap_fetch_header);
PHP_FUNCTION(imap_fetchheader);
PHP_FUNCTION(imap_fetchtext);
PHP_FUNCTION(imap_uid);
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php