Mailbox

Read emails from remote mailbox

Opens connection to imap post box.

Arguments

$name
Name of the mailbox in account preferences.
Default: First preference that is found.
$preferences:
Connection parameters in array

Example

Find emails sent by orderer:

$Mails = $this->get('mailbox','mybox')->getEmails('SENDER',$Order->email);

Connection

Set connection

Set account preferences for mailbox:

mailbox:
  - Name:      my_mailbox
    Host:      mail.domain.com
    Username:  demouser@domain.com
    Password:  password

Connect to mailbox using account preferences:

$Mailbox = $this->get('mailbox', 'my_mailbox');

Use custom parameters

$Mailbox = $this->get('mailbox', Array(
  'Host'     => 'mail.domain.com',
  'Username' => 'demouser@domain.com',
  'Password' => '1234'
));

Read emails

Read all messages and print subjects:

foreach ( $Mailbox->getEmails() as $Email ){
    $txt .= "{$Email->getSubject()} from {$Email->getFrom()}";
}

Save new email as a new order

Read all emails that are not yet marked as seen, set information to new order and save it:

foreach ( $Mailbox->getEmails('UNSEEN') as $Email ){
    $Order = $this->get('page','order');
    $Order->name         = $Email->getSubject();
    $Order->email        = $Email->getFrom();
    $Order->description  = $Email->getBody(); // Marks Email to SEEN!!
    $Order->save();

    // $Email->delete(); // Delete Email from Mailbox after reading
}

Search

Read all unseen messages and mark them as seen:

foreach ( $Mailbox->getEmails('UNSEEN') as $Email ){
    $Email->markSeen();
}

Find all messages from certain email:

foreach ( $Mailbox->getEmails('SENDER', 'sender.name@domain.com')) as $Email ){
    $txt .= "{$Email->getSubject()}<br/>";
}

Find messages with multiple terms:

foreach ( $Mailbox->getEmails(Array(
   'UNSEEN' => true,
   'SENDER' => 'sender.name@domain.com',
   'LIMIT'  => 50 // Only first 50 emails
)) as $Email ){
   $txt .= "{$Email->getSubject()}<br/>";
}

Query terms

String
'FROM', 'TO', 'CC', 'BCC', 'SUBJECT', 'BODY', 'TEXT', 'KEYWORD', 'UNKEYWORD'
Boolean
'ANSWERED', 'UNANSWERED', 'DELETED', 'UNDELETED', 'FLAGGED', 'UNFLAGGED', 'NEW', 'OLD', 'RECENT', 'SEEN', 'UNSEEN'
Date
'ON', 'SINCE', 'BEFORE'

Full Method List

Emails Return type Description
getMailboxes() array List of remote mailboxes (folders).
getEmails(flag) Email[] Find emails with given flag.
getEmails(term,value) Email[] Find emails that has value in given term.
getEmails(array) Email[] Find emails with multiple search terms.
Connection Return type Description
setFolder(path) this Set remote path.
getFolder(path) string Get remote path.
getStream() imap_stream Open connection and return imap connection stream.
getRef() string Get connection reference: Host, port, folder.
connect() - Connect to remote server. Connection is made automatically when needed.
close() - Close connection to remote server.
Preferences Return type Description
setHost(string) this Set connection host.
setUsername(string) this Set connection username.
setPassword(string) this Set connection password.
setPort(int) this Set connection port. Default: 993.
getHost(string) string Get current host.
getUsername(string) string Get current username.
getPassword(string) string Get current password.
getPort(int) string Get current port.

Email

Read email Return type Description
getFrom() string Sender email
getTo() string Sender email
getSubject() string Email subject
getBody() string Email body (html or txt?)
getBodyPlain() string Email body (txt)
getBodyHtml() string Email body (html)
getTimestamp() int Timestamp of send time.
getDay() string Y-m-d of send time.
getTime() string H:i of send time.
getDate() eDate eDate of send time.
getAttachments() array Email attachments.
getAttachment(id) file Attachment stream.
Edit email Return type Description
setFlag(name,bool) this Set given flag to email.
markAnswered() this Set flag: Answered=TRUE
markUnanswered() this Set flag: Answered=FALSE
markFlagged() this Set flag: Flagged=TRUE
markUnflagged() this Set flag: Flagged=FALSE
markSeen() this Set flag: Seen=TRUE
markUnseen() this Set flag: Seen=FALSE
delete() this Delete Email from server