PHP code example of bjc / roundcube-imap

1. Go to this page and download the library: Download bjc/roundcube-imap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bjc / roundcube-imap example snippets



// $ssl_mode is optional. It can take values "tls", "ssl" and "plain". Default value is "tls".
$server = new \bjc\roundcubeimap\server($host, $ssl_mode);

// $connection is an object of \bjc\roundcubeimap\connection.
$connection = $server->authenticate($username, $password);

$mailboxes = $connection->getMailboxes();

foreach ($mailboxes as $mailbox) {

  // $mailbox is an object of \bjc\roundcubeimap\mailbox

}


$mailbox = $connection->getMailbox('INBOX');

// true on success, throws \Exception on error
$result = $connection->createMailbox($mailboxname);

// true on success, throws \Exception on error
$result = $connection->renameMailbox($mailboxname, $new_mailboxname);

// true on success, throws \Exception on error
$result = $connection->clearMailbox($mailboxname);

// true on success, throws \Exception on error
$result = $connection->deleteMailbox($mailboxname);

$status = $mailbox->getStatus();
// $status is a stdClass object that contains the values uidnext, uidvalidity, recent and if available highestmodseq

$status = $mailbox->getStatus();
$uidvalidity = $status->uidvalidity;

$messagearray = $mailbox->getMessageshigherthan($lastfetcheduid);

foreach ($messagearray as $message) {
  // $message is an object of \bjc\roundcubeimap\message
}


$messagearray = $mailbox->getMessageshigherthan($lastfetcheduid, true);

$messagearray = $mailbox->getMessageshigherthan($lastfetcheduid, false, ['X-Spam-Status','X-Mailer']);


$message_set = '1:*';

$messagearray = $mailbox->getMessageSequence($message_set);

foreach ($messagearray as $message) {
  // $message is an object of \bjc\roundcubeimap\message
}


$messagearray = $mailbox->getMessageSequence($message_set, true);

$messagearray = $mailbox->getMessageSequence($message_set, false, ['X-Spam-Status','X-Mailer']);

$message = $mailbox->getMessage($uid);
// $message is an object of \bjc\roundcubeimap\message

$messagearray = $mailbox->getMessage($uid, true);

$message = $mailbox->getMessage($uid, false, ['X-Spam-Status','X-Mailer']);

$condstore = $mailbox->checkCondstore();
// true if condstore is available, false if not

$qresync = $mailbox->checkQresync();
// true if qresync is available, false if not

$status = $mailbox->getStatus();
$highestmodseq = $status->highestmodseq;

$synchronize_result = $mailbox->synchronize($stored_highestmodseq, $stored_uidvalidity);

// check if status == 1 (query worked), if status == 0 check statusmessage to see what went wrong:
$status = $synchronize_result["status"];
$statusmessage = $synchronize_result["statusmesage"];

// Get array of messages
$messagearray = $synchronize_result["messagearray"];

foreach ($messagearray as $message) {
  // $message is an object of \bjc\roundcubeimap\message
}

// get array of vanished (deleted) messages
$vanishedarray = $synchronize_result["vanishedarray"];

foreach ($vanishedarray as $uid) {
  // uid is the uid of the deleted message
}


$messagearray = $mailbox->synchronize($stored_highestmodseq, $stored_uidvalidity, true);

$synchronize_result = $mailbox->synchronize($stored_highestmodseq, $stored_uidvalidity, false, ['X-Spam-Status','X-Mailer']);


// retrieve only flags, uid and message-id of messages (to update their status).
// All messages with a uid lower than $lastfetcheduid that are known to your application but aren't in the result set of this function have been deleted from the mailbox

$messagearray = $mailbox->getMessageupdate($lastfetcheduid);

foreach ($messagearray as $message) {
  // $message is an object of \bjc\roundcubeimap\message
}

$messagearray = $mailbox->getMessageupdate($lastfetcheduid, false, ['subject', 'date']);

$messagearray = $mailbox->getMessageshigherthan($lastfetcheduid);

foreach ($messagearray as $message) {
  // $message is an object of \bjc\roundcubeimap\message
}

// message identification
$uid = $message->getUID(); // UID of message
$id = $message->getID(); // globally unique alphanumeric message ID

// Date and subject
$date = $message->getDate); // A datetime object
$timestamp = $message->getTimestamp(); // The timestamp of the date
$subject = $message->getSubject(); // string

// Addresses
$from = $message->getFrom(); // $from is an object of \bjc\roundcubeimap\emailaddress
$to = $message->getTo(); // $to is an array of objects of \bjc\roundcubeimap\emailaddress
$cc = $message->getCC(); // $cc is an array of objects of \bjc\roundcubeimap\emailaddress

// Flags
$isAnswered = $message->isAnswered; // true if ANSWERED flag is set, otherwise false
$isDeleted = $message->isDeleted; // true if DELETED flag is set, otherwise false
$isDraft = $message->isDraft; // true if DRAFT flag is set, otherwise false
$isSeen = $message->isAnswered; // true if SEEN flag is set, otherwise false
$isFlagged = $message->isFlagged; // true if FLAGGED flag is set, otherwise false

// get a specific header
$value = $message->getheader($headername);

// get all headers
$headerarray = $message->getHeaders();


// example: Guybrush Threepwood <[email protected]>
$address = $from->getAddress(); // [email protected]
$mailbox = $from->getMailbox(); // guy
$hostname = $from->getHostname(); // mightypirates.com
$name = $from->getName(); // Guybrush Threepwood

$fulladdress = $from->getFulladdress; // Guybrush Threepwood <[email protected]>

// get plain text of email if present
$plaintext = $message->getBodyText();

// get html text of message if present
$htmltext = $message->getBodyHtml();

// get attachments of message
$attachments = $message->getAttachments(); // $attachment is an array of objects of class \bjc\roundcubeimap\attachment

// get inline images of message
$inlineimages = $message->getInlineobjects(); // $inlineimages is an array of objects of class \bjx\roundcubeimap\attachment


foreach ($attachments as $attachment) {

   $filename = $attachment->getFilename();
   $charset = $attachment->getCharset(); // if available
   $mime_id = $attachment->getMimeId(); // get mime_id (also called part number) to retrieve the data of the attachment later
   $data = $attachment->getData(); // content of attachment

}

$attachment = $message->getAttachment($mime_id); // returns the attachment object
$data = $attachment->getData();

foreach ($inlineobjects as $inlineobject) {

   $filename = $inlineobject->getFilename();
   $charset = $inlineobject->getCharset(); // if available
   $data = $inlineobject->getData(); // content of inline image

}

$mimemessage = $message->getMimemessage();


// First parameter is an array of the flags you want to set, second parameter is the message set you want to set the flags for
// returns true if successful, throws exception if not

$message_set = '1:*';
$flags = array('FLAGGED', 'SEEN');

$result = $mailbox->setFlag($flags, $message_set);

// First parameter is an array of the flags you want to clear, second parameter is the message set you want to clear the flags for
// returns true if successful, throws exception if not
$message_set = '12322';
$flags = array('FLAGGED');

$result = $mailbox->clearFlag($flags, $message_set);


// Call from within the message object
$result = $message->setFlag($flags);
$result = $message->clearFlag($flags);



$message_set = '1:10';
$targetmailbox = 'newmailboxname';

// returns true if successful, throws exception if not
$result = $mailbox->copyMessages($message_set, $targetmailbox);
$result = $mailbox->moveMessages($message_set, $targetmailbox);

$message_set = array(11320,11330);
$result = $mailbox->deleteMessages($message_set);

// Call from within the message object
// returns true if successful, throws exception if not
$result = $message->copyMessage($targetmailbox);
$result = $message->moveMessage($targetmailbox);
$result = $message->deleteMessage();



// returns number of messages if successful, throws exception if not
$number_of_messages = $mailbox->countMessage();
$number_of_recent_messages = $mailbox->countRecent();
$number_of_unseen_messages = $mailbox->countUnseen();


$mail = new \PHPMailer();
// set all your mail credentials here

// Now send message
$mail->send();

// Get mime mailstring from PHPMailer
$mailstring = $mail->getSentMIMEMessage();

// Save it to an existing mailbox object

$flags = array('SEEN');

$uid_of_appended_message = $mailbox->appendMessage($mailstring, $flags);


// Get connection to source account and access the mailbox and message
$server = new \bjc\roundcubeimap\server('hostname_account1');
$connection = $server->authenticate('username_account1', 'password_account1');
$mailbox = $connection->getMailbox('mailboxname_account1');
$message = $mailbox->getMessage($uid);

// fetch mime message from server
$mimemessage = $message->getMimemessage();

// Get connection to destination account and access the mailbox
$server = new \bjc\roundcubeimap\server('hostname_account1');
$connection = $server->authenticate('username_account1', 'password_account2');
$mailbox = $connection->getMailbox('mailboxname_account2');

// Append the mime message to the mailbox
$mailbox->appendMessage($mimemessage);