Integrating Imap Features Into Web App For A Gmail Mailbox

Hope you already have basic understanding of what Imap is. if not go here

Basically what i am explaining here will be a way to access mails from a gmail email mailbox into your application, save mail information to a batabase table, saving the replays, send reply from application, move certain mails to certain folders in gmail from application itself etc.

i will be doing it with the help of an extension (EIMAp) developed by the Mr. Antonio Ramirez (Big Thanks to him) and the core php imap functions. i customized the code to suit my needs and explaining it in simple language. You can download EIMAp extension form here and can refer php imap functions from here

1st things 1st.. if you are using wamp as local server you will need to enable php_imap extension. for this click on wamp server green icon in system tray and select php->PHP extensions->php_imap.

Extract the contents of EIMap extension into protected/extensions in your app directory.

Connecting to a gmail mailbox and fetching mails into app page and saving in database.

In my project the requirment was to manage a complaint mailbox in gmail. that is a mail id into which users will send their complaints. a url is run in cronjob in a certain interval which should fetch the mails into our application and save certain info from the mail to database.

the final view in application is somthing like this:


For that i am writing an action “fetch”(which is run as a cronjob url/action). if you have the intention of saving the mails to a database table then create a table. Here i am creating a table named “fetch_email” with 7 columns ‘id’(primary key, int), ‘uid’(unique id of an email, int), ‘subject’, (subject of the mail, varchar 50), ‘from_address’(varchar 50), ‘body’(text), ‘first_mail_time’(for saving the mail arriving time, int or varchar 50), ‘status’(suppose if we want to save the status of mail. varchar 50. Eg: in my app mails were complaints. so i had to set the status of the mail as ‘complaint’ and ‘resolved’).

in this table we will only be saving the first mail. all the remaining mails which comes under the same mail thread will be saving in another table.

now generate its model, crud and controller.

in model add this line to ‘rules’


array(‘uid’, ‘unique’), //for not saving same mail twice.

in gmail all the replys we sent and the responses we get under the same subject will be saved as a conversation thread. we should display them as thread in application too.

For that create another table for saving the remaining conversation thread of the main mail.

table name “reply_thread” id(PK), ‘parent_id’(for saving unique id of parent mail, ‘to’, ‘subject’, ‘body’, ‘mail_time’.

generate model, crud and controller..

in model add this line to ‘rules’

array(‘body’, ‘unique’), //for not saving same mail twice.

code of the fetch action is here

code of the reply action is here

here i used 2 objects to connect to 2 folders. instead of that we can use imap_open function to reopen another folder and fetch mails from that folder. for easyness i wrote a function to switch between folders

//function for reconecting to another mailbox. here sent mail
public function reopen($identi)
//for switching between inbox and sent mail folder
if(imap_reopen($this->stream, ‘{}[Gmail]/Sent Mail’))
return true;
if(imap_reopen($this->stream, ‘{}INBOX’))
return true; }

call this function like $this->reopen(2); or $this->reopen(1); for switching between INBOX and sent mail.

Hope this works for you.

Conversation view in application.

for displaying mails as a conversation view i edited the view file of “fetchemail” for getting the view as in the picture given above. code of that edited view is here.

Replaying from application

For replying from application we are using a function called imap_mail(),

and inorder to save our reply in gmail sent mails we are appending that mail to mailbox using a function called imap_append().

code of that action is here

Hope you understood the explanation


Leave a Reply

Your email address will not be published. Required fields are marked *