implementing crm with laravel mailbox module

Implementing CRM System With Laravel Part 10: Mailbox Module

In this part of this series i will go on by implementing an important module in CRM systems which is a Mailbox.



Series Topics:


Some CRM systems contain dedicated modules for emails and messaging to enable users to send messages to each other, In this part i will implement a simple module for sending emails. At first we need to create some migrations and models.


Let’s install special package used to migrate specific migration file:


Creating Migrations & Models

Run those commands in terminal

Those command will create migrations and models in one step. Here we have multiple tables that represent the mailbox:

  • MailboxFolder: Represent the mailbox folder like in Gmail Inbox, Send, Trash, etc.
  • Mailbox: The main table for storing message subject and body.
  • MailboxUserFolder: This table Links the mailbox message with the user_id and folder_id.
  • MailboxReceiver: Stores the message receiver ids.
  • MailboxAttachment: If there are an attachment for the message, this table stores them.
  • MailboxFlags: This table stores specific flags according to the current user for example is_important marks the message as important and shown with a star beside it and is_unread to mark the message with bold.
  • MailboxTmpReceiver: This table like the receiver table but used only in case we save the message as a “Draft”.


Modifying Migrations

Open XXXX_XX_XX_XXXXX_create_mailbox_folder_table.php and modify it as shown:








Now using the custom migration package to migrate:

As shown in the above code in the migration files every email message will have subject, body, sender, time sent. The parent id marks the message as if this is a reply or not. The mailbox user folder table holds the user_id, mailbox id or message id and the folder id. The other tables is self explanatory.

Default Mailbox Folders

Let’s insert the default mailbox folders, create a new seeder file using this command:

Open database/seeds/MailboxFolderSeeder.php

Now run this command to seed this file:

The folders are inserted successfully, next we will update the models.



Modifying Mailbox Models

Open app/Models/Mailbox.php and modify as below:







I have updated the models, and added some relations that will be used later when displaying the mailbox data.

update app/Helpers/General.php add this function

This function retrieves the unread messages for the current logged in user, here we make a join with mailbox related tables like the mailbox receiver and mailbox flags and fetch all the data where the receiver id is “Auth::user()->id” and is_unread flag equal to 1. I will use this function below.


Mailbox Controller

Run this command in terminal to create a new controller

Modify app/Http/Controllers/MailboxController.php add the below code

I have added method signatures without implementations that will represent the mailbox actions, those methods will be updated in the next sections.


Listing All Messages

The first step in the mailbox is to display tall the messages categorized by folder, this will be done in the index() method below:

The index() method lists the mailbox messages according to the folder, Don’t be shocked with the the query in the getData() method it just retrieves the messages according to the current folder and the current user. Retrieving the messages is a matter of joining the Mailbox table with the other related tables.

For the Inbox folder we need to retrieve the messages that was sent to me so in other words where receiver_id equal to “Auth::user()->id” and folder is Inbox like this:

For the Sent or Drafts folder i need to fetch the messages that i sent shown in this condition:

For the Trash folder i need to retrieve the messages that have been added to the trash folder, note that i can trash a message that sent to me or a message that i received.

create resources/views/pages/mailbox/index.blade.php and add the below code:





In the above files i have added the code for displaying messages, note that i have created other partials views for the mailbox controls and mailbox folders.

The mailbox listing screen divided into two parts the first is a sidebar which display the folders and the other display the messages for each folder.

The mailbox controls also located in a separated partial view. Note here the controls will be shown according to the Folder which mean that some controls will be shown in a folder and some are not.

I have added some javascript files the first will contain some helper functions that we will use later located in functions.js we will update it later. The other file index.js contain contain code for initializing the grid checkboxes, and calling the mailbox functions defined in functions.js


Customizing Pagination

If you checked the mailbox controls view you will see this code:

This code generates a pagination links using custom pagination file

To create custom pagination first run this command:

This command will create a vendor/pagination directory under views/ directory because we will need a custom pagination view for our mailbox listing page so create resources/views/vendor/pagination/mailbox.blade.php with the below code:



That’s it the custom pagination is working now.


Composing Email

Let’s see how to compose an email, the process is to show a form to enter the message subject, body, recipients, and attachments if exist, then the user can send the message or save it as a draft.

Update Mailbox controller by updating the create() and store() methods:

Add those private helper methods to the end of the controller:

The above code save and send the message to the recipients. First we show the compose form which we will show below. Next on submitting the form, the store() method triggered which simply do some simple validations then we save the message.

Then we save the receiver ids, folders, and flags. This is described in the save() private method which do a lot of steps. First from the sender perspective we save a record in the Mailbox user folder table with folder “Sent” or “Draft“. Also we save a record on the Mailbox Flags table.

Second From the receivers perspective i checked if i clicked the send button then we looped over the receivers, for each receiver we save a record in the Mailbox receiver table, and Mailbox user folder table with folder “Inbox” and Mailbox Flags with flag “is_unread=1”.

If we click the draft button on the form we save the data on the MailboxTmpReceiver table.

Finally we save the attachments if exist and we send the message.


Open app/Helpers/MailerFactory.php and add this method




Continue to Part 11: Mailbox Complete


Share this: