使用了https://github.com/SSilence/php-imap-client
Imap连接和设置:
try {
$imap = new Imap([
'flags' => [
'service' => ImapConnect::SERVICE_IMAP,
'encrypt' => ImapConnect::ENCRYPT_SSL,
'validateCertificates' => ImapConnect::NOVALIDATE_CERT,
],
'mailbox' => [
'remote_system_name' => 'localhost',
],
'connect' => [
'username' => $username,
'password' => $password
]
]);
} catch (ImapClientException $error) {
echo $error->getMessage().PHP_EOL;
die();
}
具有将邮件移动到另一个文件夹的功能
$imap->moveMessages($_POST['id'], 'INBOX.Archive');
工作正常!但是,如何将此消息移回“收件箱”文件夹? 我尝试:
$imap->moveMessages($_POST['id'], 'INBOX');
这不起作用。 对不起,我的英语不好。
更新:
前面查看:
$imap->selectFolder("INBOX");
$emails = $imap->getMessages(20,0);
foreach($emails as $email){
if($email->header->seen == 0) {
$addUnreadClass = 'unseen';
} else {
$addUnreadClass = '';
}
if($email->header->flagged == 0) {
$addFlaggedIcon = '<i class="far fa-star flag"></i>';
} else {
$addFlaggedIcon = '<i class="fas fa-star flag flagged"></i>';
}
if(!empty($email->attachments)) {
$addAttachmentIcon = '<i class="icon-link"></i>';
} else {
$addAttachmentIcon = '';
}
$unixTimestamp=strtotime($email->header->date);
$uid = $imap->getUid($email->header->msgno);
echo '
<div class="d-flex pb-1 px-1 message-item '.$addUnreadClass.'" data-uid="'.$uid.'" data-id="'.$email->header->msgno.'">
<div class="col-1 p-0">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck'.$email->header->msgno.'">
<label class="custom-control-label" for="customCheck'.$email->header->msgno.'"> </label>
</div>
'.$addFlaggedIcon.'
</div>
<div class="col-8 pl-1">
<span class="d-block mail-title text-truncate">'.$email->header->from.'</span>
<span class="d-block mail-subject text-truncate">'.$email->header->subject.'</span>
</div>
<div class="col-3 px-0 text-right">
<div class="mail-date">'.date("M d", $unixTimestamp).'</div>
'.$addAttachmentIcon.'
</div>
</div>
';
}
运行移动消息的jQuery / ajax事件:
$("#selected-controls .moveTo").click(function() {
$('#selected-controls').hide();
var selected = [];
$('.message-item input:checked').each(function() {
selected.push($(this).parent().parent().parent().data('uid'));
});
$.confirm({
title: 'Choose folder',
content: $('#moveFolders').html(),
theme: 'modern',
buttons: {
confirm: {
text: 'OK',
btnClass: 'btn-green',
action: function () {
var selectedFolder = this.$content.find('select').val();
$.ajax({
type: "POST",
url: 'ajax.php',
data: {action: "moveTo", id: selected, folder: selectedFolder},
success: function(data){
$.notify({
icon: 'far fa-check-circle',
title: 'Success!',
message: 'Selected messages moved to another folder.'
},{
type: 'success',
animate: {
enter: 'animated fadeInDown',
exit: 'animated fadeOutUp'
},
showProgressbar: true
});
}
});
$('.inbox').addClass('loading');
var folder = $('.folder.active').ignore("span").text();
$.ajax({
type: "POST",
url: 'ajax.php',
data: {action: "changeFolder", folder: folder},
success: function(data){
$('.inbox .messages-container .mCSB_container').html(data);
$('.messages-container .scrollbar').mCustomScrollbar("update");
$('.inbox').removeClass('loading');
}
});
}
},
cancel: {
text: 'Cancel',
btnClass: 'btn-red',
action: function () {
}
}
}
});
});
和php函数:
if ($action == "moveTo"){
if ($folder == 'INBOX'){
$folder = 'INBOX';
} else {
$folder = 'INBOX.'.$folder;
}
$imap->moveMessages($_POST['id'], $folder);
}
它可以在“收件箱”中运行,并且不会从其他文件夹中隐匿。不要给出任何错误,请使用0项更改刷新邮件列表。