This code demonstrates features that are not well documented at this time. The main feature is that the selected mailbox in imap_open (or reopen) and the specified mailbox in other imap functions are unrelated. It has been tested with Gmail and with a Dovecot IMAP server. The mailbox separator depends on the server. Gmail: "/" Dovecot: "." If you want to test with Gmail, you need to turn on "Access for less secure apps" in your account.
<?php
$server = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$email = "example@gmail.com";
$password = "password";
$selected = "{$server}Test/Sub1/Sub12";
$conn = imap_open($selected, $email , $password);
$specified = "{$server}Test/Sub1";
$boxes = imap_list($conn, $specified , '*');
print_r($boxes);
imap_append($conn, $specified
, "From: me@example.com\r\n"
. "To: you@example.com\r\n"
. "Subject: test\r\n"
. "\r\n"
. "this is a test message, please ignore\r\n");
$selected = "{$server}Test/Sub1";
imap_reopen($conn, $selected);
imap_mail_move ($conn , "1" , "Test");
imap_expunge($conn);
imap_close($conn);
?>