imap_mail_move

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_mail_moveDéplace des messages dans une boîte aux lettres

Description

imap_mail_move(
    IMAP\Connection $imap,
    string $message_nums,
    string $mailbox,
    int $flags = 0
): bool

imap_mail_move() déplace les messages spécifiés par message_nums dans la boîte aux lettres mailbox. À noter que les messages sont en réalité copiées à la boîte au lettres mailbox, et les messages originaux sont marquées pour être supprimés. Ceci implique que les messages dans mailbox sont assignées des nouveaux UIDs.

Liste de paramètres

imap

Une instance de IMAP\Connection.

message_nums

message_nums est un intervalle, et pas seulement une liste de messages (comme décrit dans la » RFC2060).

mailbox

Le nom de la boîte aux lettres, voir la documentation de la fonction imap_open() pour plus de détails

Avertissement

Passer des données qui ne sont pas digne de confiance à ce paramètre est dangereux, sauf si, imap.enable_insecure_rsh est désactivé.

flags

flags est un champ de bits et peut contenir une seule valeur :

  • CP_UID - La séquence de nombres contient des UID

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.

Historique

Version Description
8.1.0 La paramètre imap attend désormais une instance de IMAP\Connection ; auparavant, une ressource imap était attendue.

Notes

Note:

imap_mail_move() va marquer l'email original avec un marqueur de suppression, pour effectivement le supprimer, un appel à imap_expunge() est nécessaire.

Voir aussi

add a note add a note

User Contributed Notes 13 notes

up
12
juuuugroid at yahoo dot com
21 years ago
I had the most trouble with figureing out what the message list was supposed to be.  There was one comment by jan@showstar.com but i was still terribly confused.  So I searched and searched and searched.  I read rfc2060 over 10 times.  Then BAM!  My brother found it here:

http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc2060.html#sec-6.4.7

Here is the importand stuff.

Another important field is the SEQUENCE, which identifies a set of messages by consecutive numbers from 1 to n where n is the number of messages in the mailbox.  A sequence may consist of a single number, a pair of numbers delimited by colon (equivalent to all numbers between those two numbers), or a list of single numbers or number pairs.  For example, the sequence 2,4:7,9,12:15 is equivalent to
   2,4,5,6,7,9,12,13,14,15 and identifies all those messages.

There is what I know about it.  BAM!
up
10
alex at bestgames dot ro
18 years ago
After using imap_mail_move, imap_mail_copy or imap_delete it is necesary to call imap_expunge() function.
up
5
ThEDocTor
13 years ago
To copy/move a mail in Gmail to a particular Folder like Starred/Spam/Drafts/Trash

use the following statement and don't forget to call CL_EXPUNGE or imap_expunge($mbox) after it.

<?php
imap_mail_copy
($mbox,'16','[Gmail]/Starred'); // 16 is the message number, which can also be a range.(ex: '1:15')

imap_close($mbox,CL_EXPUNGE);
?>

Incase u want to send it to a personally created Label/folder(ex: Test) use..

<?php
imap_mail_copy
($mbox,'16','Test');
imap_expunge($mbox);
imap_close($mbox);
?>
up
6
laurent dot penou at gadz dot org
22 years ago
to complete the previous example, if the mbox/folders names are
{imap.free.fr}INBOX
{imap.free.fr}INBOX/draft
{imap.free.fr}INBOX/test

and you want to copy/move from INBOX to INBOX/test this is the syntax:

$mbox = imap_open("{imap.free.fr}INBOX",$mailuser,$mailpass)
      or die("can't connect: ".imap_last_error());
...
imap_mail_move($mbox,$messageset,"INBOX/test");

Hope this could help !
up
1
php at guardn dot de
20 years ago
To move mails via IMAP on an Exchange Server into "Gelöschte Objekte" use:

imap_mail_move($mbox, $delmsg, "Gel&APY-schte Objekte");

It took me some tcpdumping to get this out, since both
imap_utf7_encode and
imap_utf8

did not translate it right.

Guardn
up
1
FredN
3 years ago
to get right the folders names for imap_mail_move/imap_mail_copy, do not guess, instead use imap_list
up
0
phpmanual at NOSPAMPLEASE dot headbank dot co dot uk
3 years ago
Be aware that this function returning TRUE doesn't necessarily mean anything actually happened.

It counts as "success" if all messages matching the range you supply were moved; however that includes if there were _no_ matching messages to move.

So suppose you supply a sequence-number or UID that's invalid, e.g. because  it's no longer present in the source folder:

<?php
$res
= imap_mail_move($stream, '99999', 'DestFolder', CP_UID);

var_dump($res); // bool(true)
?>

It's up to you to validate the desired message(s) before moving, and/or afterwards.
up
0
jim dot bodine at comcast dot net
16 years ago
I offer the following example because it took me HOURS to figure this out

<?php
$this
->mailInBox = imap_open($this->mailConnectString."INBOX", $this->accountLogin, $this->accountPassword);

           
$this->messageCount = imap_num_msg($this->mailInBox);
            echo
"Processing " . $this->messageCount . " messages:<Br>";
            for (
$i = 1; $i <= $this->messageCount; ++$i) {
               
$header = imap_header($this->mailInBox, $i);
                               
               
$prettydate = date("jS F Y", $header->udate);
               
$fromHost = $header->from[0]->host;
               
                if (isset(
$header->from[0]->personal)) {
                   
$personal = $header->from[0]->personal;
                } else {
                   
$personal = $header->from[0]->mailbox;
                }
               
               
$body = imap_fetchbody($this->mailInBox, $i, "1.1");
                if (
$body == "") {
                  
$body = imap_fetchbody($this->mailInBox, $i, "1");
                }

                   
$move = "INBOX.processed" . date("Ymd");
                    echo
"trying to move:" . $i . "<br>";
                    @
imap_mail_move($this->mailInBox, $i, $move);
                                       
                }

?>
up
0
ian at showstar dot com
24 years ago
The syntax for the message list is defined in RFC2060
It is a string, containing a list of message numbers and ranges, separated by commas (no spaces anywhere.) For example, "1,2,5,11:15,19" would be accepted by imap_mail_copy or imap_mail_move.
A range of messages is defined as two message numbers separated by a colon (Ex. "1:10".) Also, a "*" may be used to refer to the last message in a mailbox. (Ex. "1:*" refers to all messages)
Be careful not to use the same mailbox for source and destination, especially if you expunge the mailbox immediately afterwards; the message will be copied (back over itself), flagged as deleted (by the imap_mail_move function), and then expunged.

The following code will move the messages in the $msg_no[] array from the folder in $mbox_name to the folder in $newmbox_name: ($mbox is an already-opened imap stream)

<?php
if ($mbox_name != $newmbox_name) {
 
reset($msg_no);
 
$messageset = implode (",",$msg_no);
 
imap_mail_move($mbox,$messageset,$newmbox_name);
 
imap_expunge($mbox);
}
?>
up
0
portico dot nospam at neverwas dot net
24 years ago
This function works, just not like everything else IMAP in PHP3... rather than feeding the server {server.ext/type:port}folder just feed it the folder's name.
up
-1
matt at bonneau dot net
24 years ago
This function copies the mail and then marks the source as deleted.  In order to see the changes, you must imap_expunge the source box.
up
-2
dinter at gmx dot de
21 years ago
I've used a dot in
imap_mail_move($mbox,$movmsgid,'INBOX.send');
instead of
INBOX/test
and it work's fine.
up
-1
vlad (php.net)
21 years ago
This keeps biting me time after time. A lot of IMAP servers with quotas don't implement 'move' right - they do a 'copy&delete' instead and don't recognize that this conflicts with their quota implementetions. So, if you try to move a large message, you'll exceed your quota even though moving it does not increase the total size of your mailbox. This is not PHP-specific, but I bet it'll bite someone else besides me, so here you go.
To Top