我正在尝试创建一个小型票务脚本,但我被困在需要显示用户和支持组之间的电子邮件对话的这一部分。
该脚本的工作方式如下:
表格由用户填写,在提交数据后,将创建一个工单,并将包含工单一些基本信息的电子邮件发送到支持组。
支持小组回复邮件并附上一些说明
- 用户和支持组之间的对话应该显示在工单页面上,当前代码我遇到以下问题:
如果线程中的邮件数为奇数,则邮件显示正确(奇数是指奇数,1/3/5/7 等)
如果邮件数量为偶数,则线程中缺少一些回复。
这是代码:
<?php
$imap = imap_open('{mail.mail.eu:993/imap/ssl}INBOX', 'mail@mail.com', 'Mailpass');
//Testing value
$value2 = '6';
$subject = 'Escalation Ticket No.: '.$value2.' has been created and assigned to you.';
$threads = array();
//remove re: and fwd:
$subject = trim(preg_replace("/Re\:|re\:|RE\:|Fwd\:|fwd\:|FWD\:/i", '', $subject));
//search for subject in current mailbox
$results = imap_search($imap, 'SUBJECT "'.$subject.'"', SE_UID);
//because results can be false
if(is_array($results)) {
//now get all the emails details that were found
$emails = imap_fetch_overview($imap, implode(',', $results), FT_UID);
//foreach email
foreach ($emails as $email) {
//add to threads
//we date date as the key because later we will sort it
$threads[strtotime($email->date)] = $email;
}
}
//now reopen sent messages
imap_reopen($imap, '{mail.mail.com:993/imap/ssl}INBOX.Sent');
//and do the same thing
//search for subject in current mailbox
$results = imap_search($imap, 'SUBJECT "'.$subject.'"', SE_UID);
//because results can be false
if(is_array($results)) {
//now get all the emails details that were found
$emails = imap_fetch_overview($imap, implode(',', $results), FT_UID);
//foreach email
foreach ($emails as $email) {
//add to threads
//we date date as the key because later we will sort it
$threads[strtotime($email->date)] = $email;
}
}
//sort keys so we get threads in chronological order
ksort($threads);
foreach ($threads as $key => $object) {
$kee = $object->msgno;
$qprint = imap_fetchbody($imap, $kee, 2);
}
echo quoted_printable_decode($qprint);
?>