It is currently March 28th, 2024, 1:21 pm

Display new mails count of any mailbox

Tips and Tricks from the Rainmeter Community
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Display new mails count of any mailbox

Post by mistic100 »

Hello

Here is a simple trick to display the number of new mails in your Gmail, Outlook, IMAP, etc. mailboxes.
It is based on https://nylas.com cloud API (which can be hosted on your own server if you care about absolute privacy).
  1. Register on Nylas Developer Program https://developer.nylas.com/register
  2. On the "Accounts" tab of the developer console, add your mail account, any big provider is supported as well as any IMAP account
  3. You will be given an access token store it preciously, you won't be able to get it later (unless deleting the account)
  4. Now simply use the WebParser plugin with basic auth to get the mail count

Code: Select all

[mMailCount]
Measure=Plugin
Plugin=WebParser
UpdateRate=60
URL=https://YOUR_TOKEN@api.nylas.com/threads?in=inbox&view=count&unread=true
RegExp=(?siU)"count": ([0-9]+)
StringIndex=1
Have fun
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Display new mails count of any mailbox

Post by mistic100 »

Yesterday it was working well, but today I systematically get an error in WebParser dump :

Code: Select all

{
  "message": "Could not verify access credential.",
  "type": "invalid_request_error"
}
though it works when I access the API with Firefox.

Perhaps the Rainmeter User-Agent got blacklisted.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Display new mails count of any mailbox

Post by jsmorley »

mistic100 wrote:Yesterday it was working well, but today I systematically get an error in WebParser dump :

Code: Select all

{
  "message": "Could not verify access credential.",
  "type": "invalid_request_error"
}
though it works when I access the API with Firefox.

Perhaps the Rainmeter User-Agent got blacklisted.
Perhaps, or you might have overrun some hourly/daily/monthly limit on calls to the API during your testing.
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Display new mails count of any mailbox

Post by mistic100 »

I don't think so, it still works from the browser or curl
Last edited by mistic100 on May 29th, 2016, 1:19 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Display new mails count of any mailbox

Post by jsmorley »

You might be able to actually use cURL.exe with the RunCommand plugin and get the information that way. Either directly from STDOUT with RunCommand and RegExpSubstitute, or by downloading the XML to a file and using WebParser to parse the local file created. I believe in cURL you can set any User Agent string you want.
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Display new mails count of any mailbox

Post by mistic100 »

Interesting, I'll try, thank you.

I also contacted Nylas support to know if there was any changes in the BasicAuth process.
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Display new mails count of any mailbox

Post by mistic100 »

Working measure with curl :

Code: Select all

[mMailCountTick]
Measure=Calc
UpdateDivider=60
OnUpdateAction=[!CommandMeasure mMailCount "Run"]

[mMailCount]
Measure=Plugin
Plugin=RunCommand
Parameter=curl -s "https://#Token#@api.nylas.com/threads?in=inbox&view=count&unread=true"
OutputType=ANSI
RegExpSubstitute=1
Substitute='"count": ':"","{":"","}":""," ":"","#CRLF#":""
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Display new mails count of any mailbox

Post by jsmorley »

Cool beans...
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Display new mails count of any mailbox

Post by jsmorley »

FYI, we have added the capability to set a custom User Agent String to WebParser, so you won't need to us cURL for this purpose going forward.

https://forum.rainmeter.net/viewtopic.php?p=124153#p124153
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Display new mails count of any mailbox

Post by mistic100 »

I'm not 100% sure the problem is the user agent but a redirection with cookie auth.
Post Reply