PHP Imap Client
Copyright (c) 2016 Tobias Zeising, tobias.zeising@aditu.de
http://www.aditu.de
Licensed under the MIT license
Features
This PHP IMAP Client is a simple class for IMAP Email access. It base on the PHP IMAP extension and offers a simple interface for handling emails. In my opinion the PHP IMAP functions are not very intuitive.
- simple interface
- get emails and folders
- move, delete, count emails
- rename, delete and add folders
- get attachments
How to use
Instantiating the class will open the imap connection.
$mailbox = 'my.imapserver.com';
$username = 'myuser';
$password = 'secret';
$encryption = 'tls'; // or ssl or '';
$imap = new Imap($mailbox, $username, $password, $encryption);
if($imap->isConnected()===false) {
die($imap->getError());
}
Now you can fetch all available folders:
$folders = $imap->getFolders(); // returns array of strings
foreach($folders as $folder) {
echo $folder;
}
Now you can select a folder:
$imap->selectFolder("Inbox");
Once you selected a folder you can count the messages in this folder:
$overallMessages = $imap->countMessages();
$unreadMessages = $imap->countUnreadMessages();
Okay, now lets fetch all emails in the currently selected folder (in our example the "Inbox"):
$emails = $imap->getMessages();
var_dump($emails);
getMessages() will not mark emails as read! It will return the following structure without changing the emails. In this example two emails are in the Inbox.
array(2) {
[0]=>
array(8) {
["to"]=>
array(1) {
[0]=>
string(30) "Tobias Zeising <tobias.zeising@aditu.de>"
}
["from"]=>
string(30) "Karl Mustermann <karl.mustermann@aditu.de>"
["date"]=>
string(31) "Fri, 27 Dec 2013 18:44:52 +0100"
["subject"]=>
string(12) "Test Subject"
["id"]=>
int(15)
["unread"]=>
bool(true)
["answered"]=>
bool(false)
["body"]=>
string(240) "<p>This is a test body.</p>
<p>With a bit <em><u>html</u></em>.</p>
<p>and without <span style="color:#008000"><span style="font-size:14px"><span style="font-family:arial,helvetica,sans-serif">attachment</span></span></span></p>
"
}
[1]=>
array(9) {
["to"]=>
array(1) {
[0]=>
string(29) "tobias.zeising@aditu.de <tobias.zeising@aditu.de>"
}
["from"]=>
string(40) "Karl Ruediger <karl.ruediger@aditu.de>"
["date"]=>
string(31) "Thu, 19 Dec 2013 17:45:37 +0100"
["subject"]=>
string(19) "Test mit Attachment"
["id"]=>
int(14)
["unread"]=>
bool(false)
["answered"]=>
bool(false)
["body"]=>
string(18) "Anbei eine Datei"
["attachments"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(24) "640 x 960 (iPhone 4).jpg"
["size"]=>
int(571284)
}
}
}
}
You can also add/rename/delete folders. Lets add a new folder:
$imap->addFolder('archive');
Now we move the first email into this folder
$imap->moveMessage($emails[0]['id'], 'archive');
And we delete the second email from inbox
$imap->deleteMessage($emails[1]['id']);
Methods
Following methods are available.
__construct($mailbox, $username, $password, $encryption)
open new imap connectionisConnected()
check whether the imap connection could be opened successfullygetError()
returns the last error messageselectFolder($folder)
select current foldergetFolders()
get all available folderssetEmbed($val)
If true, embed all 'inline' images into body HTML, accesible in 'body_embed'countMessages()
count messages in current foldercountUnreadMessages()
count unread messages in current foldergetMessages($withbody = true)
get emails in current foldergetMessage($id, $withbody = true)
get email by given iddeleteMessage($id)
delete message with given iddeleteMessages($ids)
delete messages with given ids (as array)moveMessage($id, $target)
move message with given id in new foldermoveMessages($ids, $target)
move messages with given ids (as array) in new foldersetUnseenMessage($id, $seen = true)
set unseen state of the message with given idgetAttachment($id, $index = 0)
get attachment of the message with given id (getMessages returns all available attachments)addFolder($name)
add new folder with given nameremoveFolder($name)
delete folder with fiven namerenameFolder($name, $newname)
rename folder with given namepurge()
move all emails in the current folder into trash. emails in trash and spam will be deletedgetAllEmailAddresses()
returns all email addresses of all emails (for auto suggestion list)saveMessageInSent($header, $body)
save a sent message in sent foldergetMailboxStatistics()
returns statistics, see imap_mailboxmsginfo
Feedback
Feel free to improve this class. You can use the pull request function of github for contributing improvments. The inner structure of this class is simple and easy. Don't hesitate and check it out ;)