--- title: Email importer keywords: fastai sidebar: home_sidebar nb_path: "nbs/gmail.ipynb" ---
This is an email importer, it imports your emails from an email server using SMTP. It was tested on gmail, but should work for most SMTP-servers. The recommended usage for Gmail is to enable two-factor authentication. In this case, make sure you allow SMTP-connections and have enabled an application password (explained in the same link):
The email importer has the following parameters
# This cell is meant to be able to test the importer locally
# Store your credentials in this file:
file = open('tmp/credentials_gmail.txt','r')
imap_host = DEFAULT_GMAIL_HOST
imap_user = file.readline().strip('\n')
imap_pw = file.readline().strip('\n')
pod_client = PodClient()
pod_client.delete_all()
importer_run = ImporterRun.from_data(progress=0, username=imap_user, password=imap_pw)
importer = Importer.from_data()
host_item = GenericAttribute(name='host', stringValue=DEFAULT_GMAIL_HOST)
port_item = GenericAttribute(name='port', intValue=993)
max_number_item = GenericAttribute(name='max_number', intValue=10)
importer_run.add_edge('genericAttribute', host_item)
importer_run.add_edge('genericAttribute', port_item)
importer_run.add_edge('genericAttribute', max_number_item)
importer_run.add_edge('importer', importer)
importer_temp = GmailImporter.from_data()
importer_temp.run(importer_run=importer_run, pod_client=pod_client)
test = b"""\
Message-id: 1234\r
From: user1 <a@gmail.com>\r
To: user1 <b@gmail.com>\r
Reply-to: user1 <c@gmail.com>\r
Subject: the subject\r
Date: Mon, 04 May 2020 00:37:44 -0700\r
This is content"""
gmail_importer = GmailImporter()
mail_item = gmail_importer.create_item_from_mail(test, 'message_channel_id')
assert mail_item.externalId == '1234'
assert mail_item.sender[0].externalId == 'a@gmail.com'
assert mail_item.receiver[0].externalId == 'b@gmail.com'
assert mail_item.replyTo[0].externalId == 'c@gmail.com'
assert mail_item.subject == 'the subject'
assert mail_item.content == 'This is content'
assert mail_item.dateSent == gmail_importer.get_timestamp_from_message(email.message_from_bytes(test))
assert mail_item.messageChannel[0].externalId == 'message_channel_id'
message = email.message.EmailMessage()
message.set_content('aa')
message.add_attachment(b'bb', maintype='image', subtype='jpeg', filename='sample.jpg')
message.add_attachment(b'cc', maintype='image', subtype='jpeg', filename='sample2.jpg')
content, attachments = get_message_content(message)
assert content == 'aa\n'
assert attachments[0].get_content() == b'bb'
assert attachments[1].get_content() == b'cc'
# This cell is meant to be able to call the importer locally (simulating the front-end)
pod_client = PodClient(url='http://0.0.0.0:3030')
pod_client.create(importer_run)
pod_client.create(importer)
pod_client.create(host_item)
pod_client.create(port_item)
pod_client.create(max_number_item)
pod_client.create_edges(importer_run.get_all_edges())
print(importer_run.uid)
print(requests.post(f'http://0.0.0.0:3030/v2/{pod_client.owner_key}/run_importer',
json={'databaseKey':pod_client.database_key,
'payload':{
'uid':importer_run.uid,
'servicePayload':{
'databaseKey':pod_client.database_key,
'ownerKey':pod_client.owner_key
}
}}).content)