如何在 php 中使用 imap 获取的电子邮件中的 UID 回复特定电子邮件
How to reply to specific email using UID in imap fetched emails in php
我使用 SMTP gmail 设置发送电子邮件,工作正常,我使用 php 的 IMAP 功能和 return 电子邮件列表获取电子邮件。现在我有了带有 uid 的电子邮件列表。我想将回复发送到收到的电子邮件。下面是我的蛋糕php IMAP 代码:
public function listMessages($params=array()) {
$page = (isset($params['page'])) ? $params['page'] : 1;
$per_page = (isset($params['per_page'])) ? $params['per_page'] : 10;
$sort = (isset($params['sort'])) ? $params['sort'] : null;
$sorted = null;
$limit = ($per_page * $page);
$start = ($limit - $per_page) + 1;
$start = ($start < 1) ? 1 : $start;
$limit = (($limit - $start) != ($per_page-1)) ? ($start + ($per_page-1)) : $limit;
$info = imap_check($this->imapStream);
$limit = ($info->Nmsgs < $limit) ? $info->Nmsgs : $limit;
if(true === is_array($sort)) {
$sorting = array(
'direction' => array( 'asc' => 0,
'desc' => 1),
'by' => array( 'date' => SORTDATE,
'arrival' => SORTARRIVAL,
'from' => SORTFROM,
'subject' => SORTSUBJECT,
'size' => SORTSIZE));
$by = (true === is_int($by = $sorting['by'][$sort[0]]))
? $by
: $sorting['by']['date'];
$direction = (true === is_int($direction = $sorting['direction'][$sort[1]]))
? $direction
: $sorting['direction']['desc'];
$sorted = imap_sort($this->imapStream, $by, $direction);
$msgs = array_chunk($sorted, $per_page);
$msgs = $msgs[$page-1];
}
else
$msgs = range($start, $limit); //just to keep it consistent
$result = imap_fetch_overview($this->imapStream, implode($msgs, ','), 0);
if(false === is_array($result)) return false;
//sorting!
if(true === is_array($sorted)) {
$tmp_result = array();
foreach($result as $r)
$tmp_result[$r->msgno] = $r;
$result = array();
foreach($msgs as $msgno) {
$result[] = $tmp_result[$msgno];
}
}
$return = array('res' => $result,
'start' => $start,
'limit' => $limit,
'sorting' => array('by' => $sort[0], 'direction' => $sort[1]),
'total' => imap_num_msg($this->imapStream));
$return['pages'] = ceil($return['total'] / $per_page);
return $return;
}
我已经使这个功能起作用了。只需在发送电子邮件之前在主题前添加 "Re:" 即可。 Gmail 会自动将其视为回复并继续处理同一主题,而不是将其显示为新的单独电子邮件。
我使用 SMTP gmail 设置发送电子邮件,工作正常,我使用 php 的 IMAP 功能和 return 电子邮件列表获取电子邮件。现在我有了带有 uid 的电子邮件列表。我想将回复发送到收到的电子邮件。下面是我的蛋糕php IMAP 代码:
public function listMessages($params=array()) {
$page = (isset($params['page'])) ? $params['page'] : 1;
$per_page = (isset($params['per_page'])) ? $params['per_page'] : 10;
$sort = (isset($params['sort'])) ? $params['sort'] : null;
$sorted = null;
$limit = ($per_page * $page);
$start = ($limit - $per_page) + 1;
$start = ($start < 1) ? 1 : $start;
$limit = (($limit - $start) != ($per_page-1)) ? ($start + ($per_page-1)) : $limit;
$info = imap_check($this->imapStream);
$limit = ($info->Nmsgs < $limit) ? $info->Nmsgs : $limit;
if(true === is_array($sort)) {
$sorting = array(
'direction' => array( 'asc' => 0,
'desc' => 1),
'by' => array( 'date' => SORTDATE,
'arrival' => SORTARRIVAL,
'from' => SORTFROM,
'subject' => SORTSUBJECT,
'size' => SORTSIZE));
$by = (true === is_int($by = $sorting['by'][$sort[0]]))
? $by
: $sorting['by']['date'];
$direction = (true === is_int($direction = $sorting['direction'][$sort[1]]))
? $direction
: $sorting['direction']['desc'];
$sorted = imap_sort($this->imapStream, $by, $direction);
$msgs = array_chunk($sorted, $per_page);
$msgs = $msgs[$page-1];
}
else
$msgs = range($start, $limit); //just to keep it consistent
$result = imap_fetch_overview($this->imapStream, implode($msgs, ','), 0);
if(false === is_array($result)) return false;
//sorting!
if(true === is_array($sorted)) {
$tmp_result = array();
foreach($result as $r)
$tmp_result[$r->msgno] = $r;
$result = array();
foreach($msgs as $msgno) {
$result[] = $tmp_result[$msgno];
}
}
$return = array('res' => $result,
'start' => $start,
'limit' => $limit,
'sorting' => array('by' => $sort[0], 'direction' => $sort[1]),
'total' => imap_num_msg($this->imapStream));
$return['pages'] = ceil($return['total'] / $per_page);
return $return;
}
我已经使这个功能起作用了。只需在发送电子邮件之前在主题前添加 "Re:" 即可。 Gmail 会自动将其视为回复并继续处理同一主题,而不是将其显示为新的单独电子邮件。