[RESOLVED] Modifying osTicket to use Zend\Mail instead of IMAP functions

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

#1
if $struct is null or empty AND $mid (message ID) is NOT null) get the structure of the message

#2
if $struct is NOT null
AND the structure did not return a predefined $mimeType
AND (there is not an array of objects corresponding to the parameters of the Content-disposition
OR can't locate a filename)

#3
if there's a part number use it, otherwise use 1

#4
if can retrieve the section (by $partNumber) of the specified message (by $mid)
then if the encoding is either base64 or quoted-printable decode it as an 8 bit string

#5
if an encoding type has been defined then
if an array of object parameters exists and is not null then
loop through the parameters to determine the charset

#6
mime encode the string $text (the decoded content of $partnumber) using
the $charset and $encoding method found in the structure

Are you familiar with Zend\Mail at all? Because I am trying to keep everything as close to its original form as possible so that functions, variables and such can still be referred to by other scripts in the ticket system, I am trying to work out the best way to replace the imap c-client functions with appropriate Zend\Mail functions.

I guess I am a little confused by how the function getMimeType works. The variable $mimeType afaik is passed into function getPart from only one other function (getBody shown at the end of this post) and thus is always 'text/html' or 'text/plain' (searching the entire script, class.mailfetch.php, for "->getPart" only returns instances in function getBody and the one line of getPart in the recursive search section). Can you shed some light on what's going on in getMimeType? If it is just returning the MIME type of that particular part of the header/message I could replace the existing code:

I would appreciate a review. I think I have worked through the function and have converted it. I haven't tested it yet because I have a number of other functions yet to finish going through and converting, but on its own does this look like it's going to work okay?

Here's the function:

PHP Code:

//search for specific mime type parts....encoding is the desired encoding.
function getPart($mid, $mimeType, $encoding=false, $struct=null, $partNumber=false) {
try {
$mailmsg = $this->zmail->getMessage($mid);
$text = null;
if (!$part && $mid) {
if ($mailmsg->isMultipart()) {
$partNumber = null; //(re)sets $partNumber to null
foreach (new RecursiveIteratorIterator($this->zmail->getMessage($mid)) as $part) {
if(strcasecmp($mimeType, getMimeType($part))==0 //the part returned the predefined $mimeType
&& (!isset($part->contentdisposition) //AND no contentdisposition is set
|| !$part->getHeaderField('Content-Disposition', 'filename'))) { //OR there is no attached filename
$partNumber = $partNumber?$partNumber:1; //if there's a part number use it, otherwise use 1
if ($text = $mailmsg->getPart($partNumber)->getContent()) {
$encoding = null; //(re)set encoding to null
$charset=null; //(re)set $charset as null
if (isset($part->contenttransferencoding)) { //does the encoding field exist
$encoding = $part->contenttransferencoding; //get the encoding type
if ($encoding //if $encoding is NOT null
&& ($encoding == 'base64' || $encoding == 'quoted-printable')) { //AND if $encoding is either base64 or qp
$text = $this->decode($text, $encoding); //decode the content accordingly
$charset = $part->getHeaderField('Content-Type', 'charset'); //determine the $charset to be used
$text = $this->mime_encode($text, $charset, $encoding); //mime encode the string $text (the decoded content of $partnumber) using the $charset and $encoding method found in the message $part
}
} //end if encoding field exists
} //end if the content of the message can be retrieved (by $partNumber) of the specified message (by $mid)
} //end if the MIME Type is returned and there is no attachment
$partNumber++; //increase $partNumber by 1
return $text;
} // end foreach going through each mail part
} // end if multipart message
else {
if(strcasecmp($mimeType, getMimeType($mailmsg))==0 //the part returned the predefined $mimeType
&& (!isset($mailmsg->contentdisposition) //AND no contentdisposition is set
|| !$mailmsg->getHeaderField('Content-Disposition', 'filename'))) { //OR there is no attached filename
if ($text = $mailmsg->getContent()) {
$encoding = null; //(re)set encoding to null
$charset=null; //(re)set $charset as null
if (isset($mailmsg->contenttransferencoding)) { //does the encoding field exist
$encoding = $mailmsg->contenttransferencoding; //get the encoding type
if ($encoding //if $encoding is NOT null
&& ($encoding == 'base64' || $encoding == 'quoted-printable')) { //AND if $encoding is either base64 or qp
$text = $this->decode($text, $encoding); //decode the content accordingly
$charset = $mailmsg->getHeaderField('Content-Type', 'charset'); //determine the $charset to be used
$text = $this->mime_encode($text, $charset, $encoding); //mime encode the string $text (the decoded content of $partnumber) using the $charset and $encoding method found in the message $part
}
} //end if encoding field exists
} //end if the content of the message can be retrieved (by $partNumber) of the specified message (by $mid)
}//end if the MIME Type is returned and there is no attachment
return $text;
} //end if NOT multipart message
$part=null; //(re)set $text to null
} // end if $part is null and $mid is not null
} //end try
catch (\Exception $ex) {
$this->getLastError($ex);
}
/* This segment is from the original class.mailfetch.php and has not yet been translated to Zend\Mail
//Do recursive search
$text='';
if($struct && $struct->parts) {
while(list($i, $substruct) = each($struct->parts)) {
if($partNumber)
$prefix = $partNumber . '.';
if(($result=$this->getPart($mid, $mimeType, $encoding, $substruct, $prefix.($i+1))))
$text.=$result;
} //end while
}// end if
return $text;
//*/
}

I used this to cycle through the parts of the message since there isn't an equivalent to imap_fetchstructure in Zend\Mail, but to return the $text it breaks out of the foreach loop.... How would I send $text back to the calling function without breaking the loop? I thought about using "continue" but don't fully understand it and am not sure it will work.

Thanks.

(Oh... and if you hadn't noticed, I have modified not only how I ask questions, but my coding to match some of the suggested formats from your signature links. I'm trying to be a better coder and poster.)

I'm going to go ahead and mark this one as Resolved. I have finished going through the script and replacing the imap c-client functions with Zend\Mail functions. It's not causing the system to crash and did generate a ticket, so for the moment, although a few of my questions in this thread went unanswered, the overall issue is resolved (sort of for the time-being).

(Oh... and if you hadn't noticed, I have modified not only how I ask questions, but my coding to match some of the suggested formats from your signature links. I'm trying to be a better coder and poster.)

Originally Posted by mittra

I'm going to go ahead and mark this one as Resolved. I have finished going through the script and replacing the imap c-client functions with Zend\Mail functions. It's not causing the system to crash and did generate a ticket, so for the moment, although a few of my questions in this thread went unanswered, the overall issue is resolved (sort of for the time-being).

Kudos! (for both items )

/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!