International PHP Conference Munich 2025

Voting

: three minus three?
(Example: nine)

The Note You're Voting On

dominic_mayers at yahoo dot com
8 years ago
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
// Change these.
$server = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$email = "[email protected]";
$password = "password";

// The code assumes that the folders Test/Sub1/Sub11, etc. exist.
$selected = "{$server}Test/Sub1/Sub12";
$conn = imap_open($selected, $email , $password);

// This returns the $specified mailbox and its sub mailboxes,
// even if the $specified mailbox is outside the $selected mailbox.
$specified = "{$server}Test/Sub1";
$boxes = imap_list($conn, $specified , '*');
print_r($boxes);

// This appends the message in the $specified mailbox.
// It ignores the $selected mailbox.
imap_append($conn, $specified
, "From: [email protected]\r\n"
. "To: [email protected]\r\n"
. "Subject: test\r\n"
. "\r\n"
. "this is a test message, please ignore\r\n");

// This changes the $selected mailbox
$selected = "{$server}Test/Sub1";
imap_reopen($conn, $selected);

// This moves a message from the $selected to the $specified mailbox
// In this case, the specified mailbox does not include the server.
imap_mail_move ($conn , "1" , "Test");
imap_expunge($conn);
imap_close($conn);
// If you executed this code with a real IMAP server,
// the message is now in the Test mailbox !
?>

<< Back to user notes page

To Top