Have you ever had the need to retrieve e-mail information and utilize the data for display on a website, GUI, or for some other web-based purpose? With PHP you can also parse emails in your inbox. You can easily turn email into a means of collecting data from your users and trigger different actions. For instance :
1) Create a support email account that parses all recieved emails and creates help tickets automatically
2) Create a text message distribution list. If there is ever a problem shoot an email to the specific account, upon receipt it texts that message to your team.
3) Update a blog or webpage. Capture all emails in an inbox and create blog posts or webpages out of them.
4) Setup an email parsing script that looks for keywords and sends back auto-generated responses.
For this tutorial, we will use ddeboer/imap library. Here’s how to install using composer:
composer require ddeboer/imap
You can follow the detail on how to use composer here.
Once composer completed, you can load and start playing around:
require_once(‘vendor/autoload.php’);
Open a connection to Gmail
To open a connection, we will use Server class:
use Ddeboer\Imap\Server; $username = 'your.email.address@gmail.com'; $password = 'your-password'; $server = new Server('imap.gmail.com'); $connection = $server->authenticate($username, $password);
Here is how we list user mailbox:
$mailboxes = $connection->getMailboxes(); foreach ($mailboxes as $mailbox) { // Skip container-only mailboxes (we can't open this mailboxes) if ($mailbox->getAttributes() & \LATT_NOSELECT) { continue; } printf("Mailbox '%s' has %s messages\n", $mailbox->getName(), $mailbox->count()); }
To list emails in a mailbox, we execute getMessages() method of Mailbox instance:
$messages = $mailbox->getMessages(); echo "Retrieving " . sizeof($messages) . " emails:\n"; foreach ($messages as $message) { echo "Subject: " . $message->getSubject() . "\n"; echo "Message: " . $message->getBodyHTML() . "\n"; }
The library we’re using in this tutorial provide loads of ways to search emails.
We can search emails based on date:
• Before certain date
• On certain date
• After certain date
Search based on email address, like: bcc, from, to, or cc. Search criteria can also be combined with or. There’s also search criteria based on text data, like: search within body, subject, etc.
$search = new SearchExpression(); $search->addCondition(new To('email.to@address.com')); $search->addCondition(new From('email.from@address.com')); $search->addCondition(new Subject('search query')); $search->addCondition(new Unseen()); $search->addCondition(new Deleted()); $search->addCondition(new From('appleid@id.apple.com')); $arrayOrCondition = array(); $arrayOrCondition = new Subject('php tutorial'); $arrayOrCondition = new Subject('imap tutorial'); $search->addCondition(new OrConditions($arrayOrCondition)); $messages = $mailbox->getMessages($search);
You can check more search criteria from the library’s repository.
The $mailbox->getMessages() will return arrays of Message. There are a lot of method you can use:
$message->getSubject(); $message->getFrom(); $message->getTo(); $message->getDate(); $message->isAnswered(); $message->isDeleted(); $message->isDraft(); $message->isSeen(); $message->getBodyHTML(); $message->getAttachments(); if($message->hasAttachments()){ $attachs = $message->getAttachments(); foreach($attachs as $attach){ $attach->isEmbeddedMessage(); $attach->getFileName(); $attach->getSize(); } }
Within IMAP protocol, you can also update the email like moving to another inbox, set flag, and delete the email. Here is an example of what you can do:
$message->delete(); $anotherMailbox = $connection->getMailbox('target-mailbox'); $message->move($anotherMailbox); $message->maskAsSeen(); $message->setFlag('\\Seen \\Flagged');
For list of flags, you can consult the php documentation on imap library.