Skip to main content

Notifications

Dynamics 365 Community / Blogs / EasyD365 / Auto archive CRM emails in ...

Auto archive CRM emails in Gmail

Find more post on my blog.

We all know that Dynamics CRM offers a POP3/SMTP connection with Gmail. Some of you might be using it and others consider it as an option, either way, you must know that sometimes the connection between both servers might give you a serious headache.

It is always better to use Exchange connection for a couple of reasons, but the most important one is that it is a Microsoft product and it is supported in case of any problems.

My next scene shows a gap in Gmail, ignoring mailbox settings and CRM server-side synchronization commands.

If you are using Gmail as a Support mailbox you know how full can the mailbox become at some point due to the large number of emails received to the support mailbox in Gmail. For that reason, there are settings in the mailbox that you can configure to auto archive, read or delete the email after they've been accessed by another service, in our case this service is Dynamics CRM.

You can find these settings in your Gmail mailbox -> Settings -> Forward and POP/IMAP -> When messages are accessed with POP (select your option here)

4452.gmail-1.PNG

In my case, I've selected the option to delete the emails once they are being read by Dynamics CRM.

From the screenshot, we can see that a Gmail email has been synched successfully in CRM, but has remained in the Inbox folder of the mailbox.

2781.crm1.PNG

6237.gmail2.PNG

Here we can see that the Service-side has sent a POP command to the Gmail server to DELE the email, and the Gmail server responded with +OK marked for deletion, but in fact, the command was not honored and the email remains in the Inbox.

2664.unnamed.png

There is a solution for this that is quite simple and will save you the time to go and clear the mailbox yourself.

First, you need to create a new filter with a label called auto-archive which will be set to all incoming emails.

1460.label-1.PNG

0535.label-2.PNG

You can select to set the label to all already existing emails in the inbox, that way the historical email will also be auto-archived.

7128.label-3.PNG

3377.gmail-9.3.PNG

Next, we need to add a script that will check the label and will archive the emails based on certain criteria like date old. First, go to script.google.com and login with the credentials of the mailbox for which we want to enable the auto-archive actions.

Then you need to create a new App Script.

5700.gmail-3.PNG

The script will look like that. Let's take a look at it.

3051.gmail-4.PNG

We can specify how old the emails should be, in my case, this is 4 hours, but you can change it to let's say 2 days which is 2 * days. Then we get only the emails with the label "autoarchive", as we might have a couple of hundred emails, we want to get the in a batch of 100. The last step is where we want to send the email, in this case, we want them in the archive.

You can have a different label, where emails can be sent to the deleted items folder, but you will either need to create a new app script with the new conditions, along with the new filter created in Gmail, or you can add the new logic in the existing script.

Once ready, you can click to save the script, and then run the script.

0211.gmail-7.PNG

The first time you run some code you will get a list of warnings and popup like this.

0564.gmail-5.PNG

A Warning

Google has created this warning process for authorization of scripts for good reason. It does not want you to fall victim to nefarious practices like data theft or deletion.

If you are running code you created on your own, no worries. You know what you wrote and it will be fine. However, if you are copying someone else's code for a project ensure you trust the author or read through the code to check it does what it says it does.

6724.gmail-6.PNG

Once you've decided you trust the code you can click on the Go to (project name) (unsafe).

All emails that are with the label autoarchive will be moved automatically to your All Mail folder.

3527.gmail-8.PNG

To make the script run automatically you should create a trigger for it.

gmail-9.PNG

Once again go to script.google.com, click on the three dots at the left side of the script, and select triggers. Add a new trigger.

gmail9.1.PNG

From here, you can choose from time-based trigger options such as Hourly, Daily Weekly.

gmail-9.2.PNG

And that will trigger the script for the email in your inbox, from now the script basically will run automatically so you don't need to come here every time and pres Run manually.

You can check the run history and report about your runs as well.

3377.gmail-9.3.PNG

This might look more like a Gmail tutorial but actually will help you to organize your emails in your mailbox that is connected with CRM via POP3/SMTP.

Hope this will help and don't forget to share!

Comments

*This post is locked for comments