Skip to content

Instantly share code, notes, and snippets.

@kostasdizas
Last active September 12, 2015 22:47
Show Gist options
  • Save kostasdizas/e12fbe1078e7ff83ef98 to your computer and use it in GitHub Desktop.
Save kostasdizas/e12fbe1078e7ff83ef98 to your computer and use it in GitHub Desktop.
IMAP mailbox count dashing.io widget

Description

Dashing widget to display the number of unread emails in an IMAP mailbox.

##Usage

To use this widget, copy mailchecker.coffee, mailchecker.html, and mailchecker.scss into the /widgets/mailchecker directory. Put the mailchecker.rb file in your /jobs folder.

To include the widget in a dashboard, add the following to your dashboard layout file:

	<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
		<div data-id="mailchecker" data-view="Mailchecker" data-title="Unread Emails"></div>
		<i class="icon-envelope icon-background"></i> <!-- optional: displays an envelope icon in the background -->
	</li>

##Settings

  • Assuming that you have Dotenv set up, add the following to a .env file in your project folder

    MAILCHECKER_HOSTNAME: _imap_hostname*_
    MAILCHECKER_PORT: _imap_port_
    MAILCHECKER_SSL: _imap_ssl_bool_
    MAILCHECKER_USERNAME: _imap_username*_
    MAILCHECKER_PASSWORD: _imap_password*_
    MAILCHECKER_MAILBOX: _imap_mailbox_
    
  • The settings marked with * are required, you can optionally set MAILCHECKER_PORT which defaults to 143, and MAILCHECKER_MAILBOX which defaults to INBOX

  • Use MAILCHECKER_SSL to set a boolean value to enable secure IMAP connections (disabled by default)

  • Default schedule set to check for unread emails every 10 minutes but can be changed from within mailchecker.rb.

class Dashing.Mailchecker extends Dashing.Widget
ready: ->
# This is fired when the widget is done being rendered
onData: (data) ->
# Handle incoming data
# You can access the html node of this widget with `@node`
# Example: $(@node).fadeOut().fadeIn() will make the node flash each time data comes in.
<h1 class="title" data-bind="title"></h1>
<h2 class="unread" data-bind="unread | raw"></h2>
<br>
<p class="account" data-bind="account"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
require 'net/imap'
imap_hostname = ENV['MAILCHECKER_HOSTNAME']
imap_port = ENV['MAILCHECKER_PORT'] || 143
imap_ssl = ENV['MAILCHECKER_SSL'] || false
imap_username = ENV['MAILCHECKER_USERNAME']
imap_password = ENV['MAILCHECKER_PASSWORD']
imap_mailbox = ENV['MAILCHECKER_MAILBOX'] || "INBOX"
SCHEDULER.every '10m', :first_in => 0 do |job|
begin
imap = Net::IMAP.new(imap_hostname, imap_port, imap_ssl)
imap.authenticate('LOGIN', imap_username, imap_password)
imap.examine(imap_mailbox)
imap_mailbox_unread = imap.search(["NOT", "SEEN"]).count
send_event('mailchecker', { account: "#{imap_username}", unread: "#{imap_mailbox_unread}" })
imap.logout()
imap.disconnect()
rescue => e
puts "\e[33mMailCheckerError: #{e}.\e[0m"
end
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #51D2FC;
$full-color: rgba(255, 255, 255, 1);
$light-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-mailchecker styles
// ----------------------------------------------------------------------------
.widget-mailchecker {
.title,
.account {
color: $light-color;
}
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment