--- title: Email importer keywords: fastai sidebar: home_sidebar nb_path: "nbs/gmailimporter.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %}

This importers fetches your emails and accounts over IMAP, it uses the python built-in imap client and some convenience functions for easier usage, batching and importing to the pod. This importer requires you to login with your email address and an app password. It is tested on gmail, but should work for other IMAP-servers.

{% include note.html content='The recommended usage for Gmail is to enable two-factor authentication. In this case, make sure you allow SMTP-connections and set an application password (explained in the same link)' %}

ImapClient

The EmailImporter communicates with email providers over imap. We created a convenience class around pythons imaplib , called the ImapClient that lets you list your mailboxes, retriev your mails and get their content.

{% raw %}

class IMAPClient[source]

IMAPClient(username, app_pw, host='imap.gmail.com', port=993)

{% endraw %} {% raw %}

IMAPClient.list_mailboxes[source]

IMAPClient.list_mailboxes()

Lists all available mailboxes

{% endraw %} {% raw %}

IMAPClient.get_all_mail_ids[source]

IMAPClient.get_all_mail_ids()

retrieves all mail ids from the selected mailbox

{% endraw %} {% raw %}

IMAPClient.get_mail[source]

IMAPClient.get_mail(id)

Fetches a mail given a id, returns (raw_mail, thread_id)

{% endraw %}

EmailImporter

{% raw %}

class GmailImporter[source]

GmailImporter(client=None, pluginRun=None, *args, **kwargs) :: PluginBase

Base class for plugins

{% endraw %} {% raw %}
{% endraw %}

Methods

{% raw %}

GmailImporter.get_content[source]

GmailImporter.get_content(message)

Extracts content from a python email message

{% endraw %} {% raw %}

GmailImporter.create_item_from_mail[source]

GmailImporter.create_item_from_mail(mail, thread_id=None)

Creates a schema-item from an existing mail

{% endraw %} {% raw %}

GmailImporter.run[source]

GmailImporter.run(client=None)

This is the main function of the Gmail importer. It runs the importer given information provided in the plugin run.

{% endraw %}

Usage

Run from CLI

{% raw %}
pod_client = PodClient.from_local_keys()
reading database_key from /home/eelco/.pymemri/pod_keys/keys.json
reading owner_key from /home/eelco/.pymemri/pod_keys/keys.json
{% endraw %} {% raw %}
def get_gmail_creds():
    return read_file(HOME_DIR / '.memri' / 'credentials_gmail.txt').split("\n")[:2]
{% endraw %} {% raw %}
account = Account(service="gmail_imap")
run = PluginRun("", "gmail_importer.plugin", "GmailImporter")
run.add_edge("account", account)
run.status = RUN_STARTED

pod_client.create(run)
pod_client.create(account)
pod_client.create_edge(run.get_edges("account")[0])

print(pod_client.owner_key)
print(pod_client.database_key)
1210127632551467658081890196619587842208079869460745665680737018
3663389737548370717117517152016971000529921158054179307068371938
{% endraw %} {% raw %}
print("To simulate front-end, run:")
print(f"password_simulator --run_id {run.id}")
To simulate front-end, run:
password_simulator --run_id 83E2ed257Ab2BfB0462c35888E034bCc
{% endraw %} {% raw %}
pod_client.get(run.id).account[0].secret
{% endraw %} {% raw %}
!run_plugin --plugin_run_id $run.id --owner_key $pod_client.owner_key --database_key $pod_client.database_key
pod_full_address=http://localhost:3030
owner_key=0406847886098273501043678621314868213021589626108871420613276755

polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
polling for credentials...
eelcovdw@gmail.com lfpnjkhxzmpmvuvg
Using, HOST: imap.gmail.com, PORT: 993
Imap client logged in.
Importing emails...
Importing accounts...
Importing channels...
Finished running GmailImporter (#None)
{% endraw %}

Run from Docker