How do I transfer a message from a Gmail inbox to a shortcut?

I am trying to move messages from Inbox to a processed label using this code:

$inbox = imap_open($host,$user,$pass) or die('Error: ' . imap_last_error()); if( $emails = imap_search($inbox,'ALL') ) { foreach($emails as $email_number) { imap_mail_move($inbox, $email_number, 'Processed') or die('Error'); } } imap_expunge($inbox); imap_close($inbox); 

Unfortunately, while messages receive the processed label, they still remain in the Inbox.

How do I get them to leave my inbox?

+6
source share
5 answers

Actually ... The reason that the emails remained in the Inbox was that when imap_mail_move did this, the identifiers of all the remaining messages were reduced by one, so when the foreach loop moved to the next message, one message remained behind. This message skipping is repeated for each iteration. This is why imap_mail_move seemed to not work.

The solution is to use unique message UIDs instead of potentially duplicate identifiers:

 $inbox = imap_open( $host, $user, $pass ); $emails = imap_search( $inbox, 'ALL', SE_UID ); if( $emails ) { foreach( $emails as $email_uid ) { imap_mail_move($inbox, $email_uid, 'processed', CP_UID); } } 
+8
source

You need to move the message to the [Gmail] / All Mail folder, after you move it to the tag folder, which is actually not a folder, see Gmail by simply telling Gmail to add this tag.

So, through IMAP:

1) When a message is moved to the [Gmail] / TAG folder, it tells Gmail to add the message "TAG" to the message, but does not move the message.

2) When a message has been moved to the [Gmail] / All Mail folder, it tells Gmail to remove it from the Inbox.

+3
source

@Henno, your diagnosis is correct, but you could just sort the emails in descending order.

 $inbox = imap_open($host,$user,$pass) or die('Error: ' . imap_last_error()); if( $emails = imap_search($inbox,'ALL') ) { arsort($emails); //JUST DO ARSORT foreach($emails as $email_number) { imap_mail_move($inbox, $email_number, 'Processed') or die('Error'); } } imap_expunge($inbox); imap_close($inbox); 
+1
source

Put this at the end of your file, after you have processed any emails, it will move all those found in the inbox and move them to the done folder.

 $mbox = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', ' emailaddress@gmail.com ', 'password'); $countnum = imap_num_msg($mbox); if($countnum > 0) { //move the email to our saved folder $imapresult=imap_mail_move($mbox,'1:'.$countnum,'done'); if($imapresult==false){die(imap_last_error());} imap_close($mbox,CL_EXPUNGE); } 
0
source

use imap_expunge() or imap_close (..., CL_EXPUNGE); but check the return value if true or false if using imap_close (..., CL_EXPUNGE);

-2
source

Source: https://habr.com/ru/post/902439/


All Articles