2015-06-06 pymmails for teachingsΒΆ

Over the past year, I needed this module in two cases. I set up a remote notebook server for my students this year and they needed to retrieve data they produced on this remote machine. IPython 3 solved that issue by exposing links to any new file but IPython could not do that. So, I suggested to send emails with attachments and I implemented a module to make it easier:

from pymmails import create_smtp_server, send_email
server = create_smtp_server("gmail", "somebody", "pwd")
send_email(server, "somebody@gmail.com", "somebody@else.com",
                "subject", attachements = [ os.path.abspath(__file__) ])
server.quit()

With more than 80 students to follow, it is difficult to download reports, files for every student project I need to evaluate. To avoid that, I just move every email I receive in a separate folder in my inbox and I dump this folder on my laptop:

def grab_mails(mailbox, emails, subfolder, date, no_domain=False):
    """
    look for some emails in a mail box
    from specific emails or sent to specific emails

    @param      mailbox         MailBoxImap object (we assume you are logged in)
    @param      emails          list of emails
    @param      date            date (grab emails since ..., example ``1-Oct-2014``)
    @param      subfolder       folder of the mailbox to look into
    @param      no_domain       remove domain when searching for emails
    @param      fLOG            logging function
    @return                     list of emails
    """
    mid = {}
    res = []
    for m in emails:
        ms = m.split('@')[0] if no_domain else m
        ms = ms.strip()
        iter = mailbox.enumerate_search_person(ms, subfolder, date=date)
        mails = []
        for m in iter:
            if m["Message-ID"] not in mid:
                mails.append(m)
                mid[m["Message-ID"]] = m
        res.extend(mails)
    return res

imap = pymmails.MailBoxImap("firstname.lastname", "pwd", "imap.gmail.com", True)
imap.login()

listmails = grab_mails(emails=["student@school.country", mailbox=imap,
                       subfolder="inbox_folder_teachings", date="1-Oct-2014",
                       no_domain=no_domain)

mailbox.dump_html(listmails, "local_folder")
imap.logout()