问题
The Problem was, that I was not able to get the UID of a message I moved ( imap_mail_move() ) to another Folder. As I have limited Possibilities on a Shared Webspace, I tried to figure out a Workaround that works for my Needs.
回答1:
Time to give something back! Maybe its not the nicest Solution, but hey - it works!
So what I did was that I defined a random E-Mail-Adress that cannot be confused with a real one.
$sDummyEmail = '912ec803b2ce49e4a541068d495ab570@system.local';
My Script reads the Message Data first (not displayed here). Then the Message gets moved to the Targetfolder. After that, it generates another Message that will also be stored in the Targetfolder. The latter contains 1:1 the Sender-E-Mail-Adress, the Subject and in the Body the UID and the Unix_Timestamp of the Original Message.
Note: {MAILSETTINGS} must be replaced with valid Data.
imap_mail_move($oMailbox, $iMessageUniqueId, 'INBOX/TARGETFOLDER', CP_UID);
imap_append($oMailbox, '{MAILSETTINGS}INBOX/TARGETFOLDER'
, "From: ".$oMessage->fromaddress."\r\n"
. "To: ".$sDummyEmail."\r\n"
. "Subject: ".$oMessage->subject."\r\n"
. "\r\n"
. $oMessage->udate.":".$iMessageUniqueId.":DO NOT DELETE!");
So at this point after an E-Mail has arrived and it is read out with the Script, it is already moved to the Targetfolder and there is an additional generated Message in the Targetfolder.
Now the Mailbox is changed, so it reads the Messages from the Targetfolder, looking up Messages from the random E-Mail-Adress (the one above) and returning the UIDS of these Messages. After that it cycles trough each of it and reads out the Data stored into it.
In each Cycle it now looks up for a message from the Sender with the same E-Mail-Adress and the same Subject giving back their UIDs. Now it cycles trough each of these UIDs.
As the Message is generated right after the original Message, higher UIDs can be ignored and the order of the UIDs is reversed, so it cycles down and not up.
If there is a Match with the Unix Timestamp of the Original-Message and the Timestamp (which was stored in the Body) of the generated Message, it finally gives back the UID. The generated Message is marked to be deleted.
$aMessageUIds = imap_search($oMailbox, 'TO "'.$sDummyEmail.'"', SE_UID);
foreach($aMessageUIds as $iCycles => $iMessageUId){
$aMessage = imap_fetch_overview($oMailbox, $iMessageUId, FT_UID);
$aData = array_map('trim', explode(':', imap_fetchbody($oMailbox, $iMessageUId, '1', FT_UID)));
$aResult = imap_search($oMailbox, 'FROM "'.$aMessage[0]->from.'" SUBJECT "'.$aMessage[0]->subject.'"', SE_UID);
natsort($aResult);
rsort($aResult);
foreach($aResult as $iKey => $iUId){
if($iUId < $iMessageUId){
$aMessage = imap_fetch_overview($oMailbox, $iUId, FT_UID);
if($aMessage[0]->udate == $aData[0]){
echo intval($iUId);
imap_delete($oMailbox, $iMessageUId, FT_UID);
}
}
}
}
I recommend to limit the Cycles just to be on the safe Side. That's how I got that working.
Dont forget to use
imap_expunge();
来源:https://stackoverflow.com/questions/65494096/get-the-imap-uid-after-moving-the-message-to-another-folder