doukuizuo1795 2019-01-06 15:45
浏览 48

将邮件移至收件箱文件夹

Used https://github.com/SSilence/php-imap-client

Imap connection and settings:

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();
}

have function for move messages to another folder

$imap->moveMessages($_POST['id'], 'INBOX.Archive');

Work fine! But, how to move this message back to Inbox folder? Im try:

$imap->moveMessages($_POST['id'], 'INBOX');

This is not work. Sorry my bad english.

Update:

View in front:

$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.'">&nbsp;</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 event for run move message:

$("#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 () {

            }
        }
    }
});

});

And php function:

if ($action == "moveTo"){
    if ($folder == 'INBOX'){
        $folder = 'INBOX';
    } else {
        $folder = 'INBOX.'.$folder;
    }
    $imap->moveMessages($_POST['id'], $folder);
}

Its work from Inbox, and do nothink from another folders. Dont give any errors, refresh mail list with 0 changes.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 WPF使用Canvas绘制矢量图问题
    • ¥15 用三极管设计一个单管共射放大电路
    • ¥15 孟德尔随机化r语言运行问题
    • ¥15 pyinstaller编译的时候出现No module named 'imp'
    • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
    • ¥15 怎么把多于硬盘空间放到根目录下
    • ¥15 Matlab问题解答有两个问题
    • ¥15 LCD12864中文显示
    • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
    • ¥15 gsoap生成onvif框架