using IMAP and POP3 with laravel

Using Imap/POP3 With Laravel To Fetch Mailbox

Have you ever looking for away to read your personal mailbox such as Gmail or Yahoo, in this article i will show how to do this in php and laravel.

 

 

Many enterprise based systems such as Customer Relationship Managements (CRM) contain components for managing mailboxes and emails from inside the system itself providing the system admins to monitor their mailbox, compose email messages without going to email clients such as outlook.

PHP supports connecting and reading mailboxes using the IMAP or POP3 protocols, and in this post i will demonstrate this using laravel framework. We will use a laravel package Webklex/laravel-imap it’s based on PHP imap extension.

 

Installation

First you need to be sure that php imap extension library is installed otherwise install it using:

After installing add it to your php.ini restart your apache server. Also check your phpinfo() if it contains imap then it was installed successfully.

Next install Webklex/laravel-imap using composer in your laravel project using this command:

If you’re running Laravel >= 5.5, package discovery will configure the service provider and Client alias out of the box.

Otherwise, for Laravel <= 5.4, edit your config/app.php as follows:

  • add the following to the providers array: 
  • add the following to the aliases array:

    After that run this command to generate the config file:

 

Configuration

1- Single Account

If you intend to use the package for only a single account then the process is pretty simple, add those configuration items into the .env file:

1- Multi Accounts

If you intend to use it for multi accounts such as every user in the system can control their mailbox it’s preferred to store the above variables into the database in a separate table connected with user_id.

 

If you open config/imap.php their will a lot of options:

 

  • default: This is the default account
  • accounts: This array contains all available accounts such as default and gmail. You can add as many accounts.
  • delimiter — you can use any supported char such as “.”, “/”, etc
  • fetchIMAP::FT_UID (message marked as read by fetching the message) or IMAP::FT_PEEK (fetch the message without setting the “read” flag)
  • fetch_body — If set to false all messages will be fetched without the body and any potential attachments
  • fetch_attachment — If set to false all messages will be fetched without any attachments
  • fetch_flags — If set to false all messages will be fetched without any flags
  • message_key — Message key identifier option useful when you want to save the messages into the database.
  • fetch_order — Message fetch order by ascending or descending
  • open — special configuration for imap_open()
    • DISABLE_AUTHENTICATOR — Disable authentication properties.
  • decoder — Currently only the message subject and attachment name decoder can be set
  • masks — Default masking config
    • message — Default message mask
    • attachment — Default attachment mask

 

Example Fetching all messages

Sometimes when you run this example it takes a long time to execute and this is because this code fetch all messages in your inbox and this can be a huge number so the best way is to limit the results as demonstrated below in the pagination and limit sections.

 

Using the Facade

Get specific folder by name:

Searching For Messages:

Refer to Webklex/laravel-imap for more options on searching.

 

Result limiting

To limit the results you can use function limit(number, page):

 

Pagination

Use function paginate() to paginate:

Paginate a message collection:

By default the pagination displays 15 results per page but you can customize it like this:

 

Messages

Get specific message

Get a specific message by uid (Please note that the uid is not unique and can change):

Set message flags:

Attachments

Get message attachments

 

Fetch messages without body fetching (decrease load) useful in listing pages:

This example will disable body fetching to speed the retrieval process while fetching.

 

Fetch messages without body, flag and attachment fetching (decrease load) useful in listing pages:

This example will disable body and attachment fetching to speed the retrieval process while fetching.

 

Best practices

  • When you intend to read a mailbox in your own system try to save the messages into the database.
  • Use a cronjob to read and save the messages in the background.
  • When saving the data check if the message not exist in the database otherwise save it.
  • Always save the important data only such as subject, body, from, to, cc, bcc, and attachments.
  • Create a sync button which syncs your local messages with the remote messages.

Share this: