Source for file MailServer.php
Documentation is available at MailServer.php
* This module provides a simple wrapper around PHP's IMAP library while adding some
* functionality to make interacting with the mail server as easy as possible.
* This module can be used by other modules as follows:-
* - Copy module file to module directory
* - Adjust configuration file as needed (see further below on configuration file requirements)
* - $this->exec_module_action('MailServer', 'action')
* The MailServer module requires the following entries in the configuration file.
* hostname = imap.server.com : Name of the email host to connect to
* port = number : Port number to connect to for the above host
* root = Mail/ : Root folder on the server
* secure = true/false : Set to true to never send password as plain text
* ssl = true/false : Set to true to connect using SSL
* novalidate-cert = true/false : Set to true if no need to validate the server certificate
* tls = true/false : Set to true to force usage of tls
* notls = true/false : Set to true to not use tls even if available on server
* The defaults are as below:
* novalidate-cert = false
* @var array $server_config Connection information loaded from the configuration file
'hostname' =>
'localhost',
'novalidate-cert' =>
false,
* @var resource $connection Connection to the server
* @var string $current_user Name of the user currently logged in to the server
* @var string $current_mailbox Name of the mailbox currently opened
///////////////////////////////////////////////////////
* Constructor for the email server module
* The constructor registers all error messages used by this module.
// Register all error messages
* Get the configuration from config file
* Load all configuration items under section 'mailserver' in the
* main configuration file. Default values selected are described
* in the module description.
// Load values from config file if applicable
$keys =
explode(' ', 'hostname port root secure ssl novalidate-cert tls notls');
// Save connection information
///////////////////////////////////////////////////////
// Connection related code
* Connect to the server in a half-open mode (don't select any mailbox)
* Connect to the server using the username and password specified. This
* action should be invoked from another module before executing any other
* Function loads the configuration from config file.
* @param string $username Username to connect to the server with
* @param string $password Password to authenticate with
* @return resource Returns the server connection
function connect($username, $password) {
// Get configuration from file
// Figure out what flags to set
$flag_names =
explode(' ', 'secure ssl novalidate-cert tls notls');
foreach ($flag_names as $flag_name)
// Display error if failure
// Return connection or false
* Expunge currently selected mailbox
* Select a mailbox using $this->open_mailbox($mailbox_name);
// Display error if failure
* Disconnect from the current server if connected
// Disconnect if connected
// Reset the logged in user and current mailbox
///////////////////////////////////////////////////////
* Open the specified mailbox
* Call $this->connect($username, $password ) before calling this function
* @param string $mailbox_name Name of the mailbox to open, default INBOX
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_OPEN_MAILBOX_FAILED', $mailbox_name, imap_last_error());
* Create the specified mailbox
* @param string $mailbox_name Name of the mailbox to create
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_CREATE_MAILBOX_FAILED', $mailbox_name, imap_last_error());
* Rename the specified mailbox as requested
* @param string $mailbox_name Name of the mailbox to rename
* @param string $new_mailbox_name New name of the mailbox
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_RENAME_MAILBOX_FAILED',
* Delete the specified mailbox
* @param string $mailbox_name Name of the mailbox to delete
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_DELETE_MAILBOX_FAILED', $mailbox_name, imap_last_error());
* Get all mailboxes for current connection
* @return array Returns an array of mailbox name strings
// Get the list of mailboxes in folder root
// Remove full server path
for ($i =
0; $i <
sizeof($list); $i++
)
// Display error if failure
* Get only subscribed mailboxes for current connection
* @return array Returns an array of mailbox name strings
// Get the list of subscribed mailboxes
// Remove full server path
for ($i =
0; $i <
sizeof($list); $i++
)
// Display error if failure
* Get the status information for the specified mailbox
* @param string $mailbox_name Name of the mailbox, default INBOX
* @return array Returns an associative array with fields 'num_messages', 'num_recent', 'num_unseen' and 'uid_validity'
// Get the mailbox status
SA_MESSAGES |
SA_RECENT |
SA_UNSEEN |
SA_UIDVALIDITY
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_GET_MAILBOX_STATUS_FAILED',
// Return information as an array
'num_messages' =>
$status->messages,
'num_recent' =>
$status->recent,
'num_unread' =>
$status->unseen,
'uid_validity' =>
$status->uidvalidity,
* Subscribe to the specified mailbox
* @param string $mailbox_name Name of the mailbox to subscribe to
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_SUBSCRIBE_MAILBOX_FAILED',
* Unsubscribe from the specified mailbox
* @param string $mailbox_name Name of the mailbox to unsubscribe from
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_UNSUBSCRIBE_MAILBOX_FAILED',
///////////////////////////////////////////////////////
// Message information related code
* Get all the message headers for the current mailbox
* The object returned has several members which are documented here:
* http://www.php.net/manual/en/function.imap-headerinfo.php
* The Msgno member value is replaced with the message UID instead.
* @return array Returns an array of message header associative arrays with multiple fields
// Get all message numbers
// Display error if failure
// Store the headers for all the emails
// Headers for this message
// Get the headers for each message
// Convert msgno to uid since it is unique whereas msgno changes
// The other message properties
$header['message_id'] =
$header_obj->message_id;
$header['udate'] =
$header_obj->udate;
$header['subject'] =
$header_obj->subject;
$header['size'] =
$header_obj->Size;
if (isset
($header_obj->Unseen) &&
$header_obj->Unseen ==
'U' ||
$header_obj->Recent ==
'N')
if (isset
($header_obj->Answered) &&
$header_obj->Answered ==
'A')
if (isset
($header_obj->Draft) &&
$header_obj->Draft ==
'X')
$types =
explode(' ', 'from to cc bcc reply_to sender');
foreach ($types as $type) {
$header[$type] =
array();
if (isset
($header_obj->$type))
$header[$type] =
$header_obj->$type;
* Get the raw headers for the specified message uid
* @param int $message_uid The message uid to pull headers for
* @return string Returns a string containing all the header information
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_FETCH_HEADER_FAILED', $message_uid, imap_last_error());
// Get a list of message parts
// Display error if failure
if ($struct ===
false) break;
///////////////////////////////////////////////////////
// Message action related code
* Copy the specified messages to the specified mailbox
* @param string $message_uids One or more message uids, comma separated
* @param string $mailbox_name Mailbox to copy the messages to
// Don't copy anything if source and destination mailbox are the same
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_MESSAGE_COPY_FAILED',
* Move the specified messages to the specified mailbox
* @param string $message_uids One or more message uids, comma separated
* @param string $mailbox_name Mailbox to move the messages to
// Don't move anything if source and destination mailbox are the same
// Display error if failure
$this->error->display_error('ERROR_MAILSERVER_MESSAGE_MOVE_FAILED',
* Mark specified messages as read
* @param string $message_uids One or more message uids, comma separated, to mark as read