note 90131 added to function.imap-fetchstructure

From: Date: Tue, 07 Apr 2009 17:47:46 +0000
Subject: note 90131 added to function.imap-fetchstructure
Groups: php.notes 
Request: Send a blank email to php-notes+get-152914@lists.php.net to get a copy of this message
I had minor, but frustrating problems with David's IMAP e-mail parsing script example (david at hundsness dot com, 12-Sep-2008 08:00, post #85685). Here are the edits that made it work for me: 1) Eliminated PHP runtime errors caused by references to nonexistent variables. These errors were revealed when using error_reporting(E_ALL). 2) Eliminated global variables in favor of arguments passed by reference 3) Changed argument named "mid" to "mno", since it should be a message number NOT a message ID for this script to work <? // decode_email.php // Original code by david at hundsness dot com // Updated by Hobson Lane at http://totalgood.com // Updates 2009-04-07: // Globals made local and returned in an array // Used isset() to avoid PHP STRICT runtime errors // Deleted header processing as it's not required for body decoding function decode_email($mbox,$mno) { $htmlmsg = $plainmsg = $charset = ''; $attachments = array(); $s = imap_fetchstructure($mbox,$mno); if ( (!isset($s->parts)) || (!$s->parts) ) // not multipart getpart($mbox, $mno, $s, 0, $htmlmsg, $plainmsg, $charset, $attachments); else // multipart: iterate through each part foreach ($s->parts as $partno0=>$p) getpart($mbox, $mno, $p, $partno0+1, $htmlmsg, $plainmsg, $charset, $attachments); return array($htmlmsg, $plainmsg, $charset, $attachments); } function getpart($mbox,$mno,$p,$partno, &$htmlmsg, &$plainmsg, &$charset, &$attachments) { // $partno = '1', '2', '2.1', '2.1.3', etc if multipart, 0 if not multipart // DECODE DATA $data = ($partno)? imap_fetchbody($mbox,$mno,$partno): // multipart imap_body($mbox,$mno); // not multipart // Any part may be encoded, even plain text messages, so check everything. if ($p->encoding==4) $data = quoted_printable_decode($data); elseif ($p->encoding==3) $data = base64_decode($data); // no need to decode 7-bit, 8-bit, or binary // PARAMETERS $params = array(); if (isset($p->parameters) && ($p->parameters)) foreach ($p->parameters as $x) $params[ strtolower( $x->attribute ) ] = $x->value; if (isset($p->dparameters) && ($p->dparameters)) foreach ($p->dparameters as $x) $params[ strtolower( $x->attribute ) ] = $x->value; // ATTACHMENT if ((isset($params['filename'])) || (isset($params['name']))) { // filename may be given as 'Filename' or 'Name' or both $filename = (isset($params['filename']))? $params['filename'] : $params['name']; // filename may be encoded, so see imap_mime_header_decode() $attachments["{$filename}"] = $data; } // problem if 2 files have same name // TEXT elseif (($p->type==0) && ($data)) { if (strtolower($p->subtype)=='plain') $plainmsg .= trim($data) ."\n\n"; else $htmlmsg .= $data ."<br><br>\n"; if (isset($params['charset'])) $charset = $params['charset']; // assume all parts are same charset } // EMBEDDED MESSAGE elseif ($p->type==2 && $data) $plainmsg .= trim($data) ."\n\n"; // SUBPART RECURSION if ((isset($p->parts)) && (count($p->parts)>0)) foreach ($p->parts as $partno0=>$p2) getpart($mbox, $mno, $p2, $partno.'.'.($partno0+1), $htmlmsg, $plainmsg, $charset, $attachments); } // partno = 1.2, 1.2.1, etc. ?> ---- Server IP: 69.147.83.197 Probable Submitter: 189.164.85.171 ---- Manual Page -- http://www.php.net/manual/en/function.imap-fetchstructure.php Edit -- https://master.php.net/note/edit/90131 Del: integrated -- https://master.php.net/note/delete/90131/integrated Del: useless -- https://master.php.net/note/delete/90131/useless Del: bad code -- https://master.php.net/note/delete/90131/bad+code Del: spam -- https://master.php.net/note/delete/90131/spam Del: non-english -- https://master.php.net/note/delete/90131/non-english Del: in docs -- https://master.php.net/note/delete/90131/in+docs Del: other reasons-- https://master.php.net/note/delete/90131 Reject -- https://master.php.net/note/reject/90131 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152914) next »