I’ve been pushing for this for a while - emails should be way smarter than they are. The technology was parked over a decade ago, and it’s time for a refresh. I said as much in:

Rewriting emails.

Here’s some Python that rewrites an email: update_weather_email.py

import imaplib
import email.message
import time
import urllib2
import json
from math import ceil
from email.parser import HeaderParser
import datetime


def temperature_and_weather_in(where):
    json_string = urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?q=%s' % where).read()
    parsed_json = json.loads(json_string)
    return parsed_json['main']['temp'], parsed_json['weather'][0]['description']


def to_temp_string(temp_k):
    return str(0.5 * ceil(2.0 * (temp_k - 273.15))) + " °C, " + str(
        0.5 * ceil(2.0 * ((temp_k * 9 / 5.0) - 459.67))) + " °F"

mail = imaplib.IMAP4_SSL('imap-mail.outlook.com', '993')
mail.login('YOUR_EMAIL@outlook.com', 'YOUR_PASSWORD')
mail.select('Inbox')

with open("weather_at_offices.html", "r") as myfile:
    weather_template = myfile.read().replace("\n", "")

degree_sign = u'\N{DEGREE SIGN}'

dublin_temp, dublin_weather = temperature_and_weather_in('Dublin,IE')
weather_template = weather_template.replace("DUBWEATHER", to_temp_string(dublin_temp) + ", " + dublin_weather)
manhattan_temp, manhattan_weather = temperature_and_weather_in('Manhattan')
weather_template = weather_template.replace("NYWEATHER", to_temp_string(manhattan_temp) + ", " + manhattan_weather)

new_message = email.message.Message()
new_message['Subject'] = 'Weather at each office'
new_message['From'] = 'YOUR_EMAIL@outlook.com'
new_message['To'] = 'YOUR_EMAIL@outlook.com'
new_message['Date'] = datetime.datetime.now().strftime("%d/%m/%Y %H:%M")
new_message['Content-Transfer-Encoding'] = '8bit'
new_message['Content-Type'] = 'multipart/alternative; boundary="---NOTIFICATION_BOUNDARY"'
new_message['MIME-Version'] = '1.0'
new_message.set_payload('This is a multi-part message in MIME format.\n'
                        '-----NOTIFICATION_BOUNDARY\nContent-Type: text/html; charset="utf-8"\n'
                        'Content-Transfer-Encoding: 8bit\n\n'
                        + str(weather_template))

mail.select("Inbox")
resp, data = mail.uid('search', None, "ALL")
uids = data[0].split()
mailparser = HeaderParser()
for uid in uids:
    resp, data = mail.uid('fetch', uid, "(BODY[HEADER])")
    msg = mailparser.parsestr(data[0][1])
    if msg['Subject'] is not None and msg['Subject'].startswith('Weather at each office'):
        mail.uid('STORE', uid, '+FLAGS', '(\\Deleted)')
mail.expunge()

mail.append('Inbox', '', imaplib.Time2Internaldate(time.time()), str(new_message))

mail.close()
mail.logout()

Here’s the resulting email from my iPhone (the Outlook mail app):

In this instance I’m testing using Outlook.com for a publically available IMAP server. I’m only using the resulting email account for this type of thing. I’m not yet brave enough to allow the same technology to use my main email account (Gmail).

What’s next?

I really want to take on Hipchat and Jenkins somehow. They spam me a lot. Hipchat when I’ve shut my laptop. Jenkins, just because it likes to. For HipChat I could benefit from a single roll-up of conversations I’ve missed. For Jenkins, maybe just current state of the pipelines I’m interested in, and a brief history up to that momnent. Both of these would require some state to be held for me, by some system that understands when I a) last looked at the system in question, and b) whether the update emails it had been sending me were read or unread. Its behavior would change based on those.

I lieu of a general upgrade of the IMAP standard, maybe I need a system that allows third-party login, and the read/write/rewrite of a subset of my emails. For example, say I log into Outlook like so:

server: imap-mail.outlook.com : 993
user: MY_USER_NAME@outlook.com
password: sdlfyweidf6867qre12ygevwjqhwbdjhqwg

It would be nice to configure HipChat to log in like so:

server: imap-mail.outlook.com : 993
user: hipchat---MY_USER_NAME@outlook.com
password: sdjkhfuwyt57615e1g2fehgveqwkje

Inside Outlook.com, I would configure that sub-account to be able to see/change emails from donotreply@hipchat.com. Inside the Hipchat service I would set an interval of 5 mins, so as to not DOS Microsoft.

There are plenty of things for this type of technology to do.

Footnote

I was at a LeanUX conference in Brooklyn. Jessica DeVita from Microsoft was there, lamenting (in a presentation) what slaves to email we have become. I chatted to her at the close out, and although she was non-commital, I sure hope she is able to influence tool/service directions inside their Redmond HQ.



Published

May 4th, 2015
Reads:

Categories