Skip to content

Instantly share code, notes, and snippets.

@ArthurSrz
Last active April 10, 2020 21:34
Show Gist options
  • Save ArthurSrz/6aa61a57da26d943fade5b654a39eed0 to your computer and use it in GitHub Desktop.
Save ArthurSrz/6aa61a57da26d943fade5b654a39eed0 to your computer and use it in GitHub Desktop.
MyFunnyData - Small programs to play with data
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bye Bye Gmail - Part 1 : filtering and checking emails\n",
"\n",
"Tired of being overwhelmed with emails and spending hours filetering your inboxes ? Check and send emails from your Jupyter environments ! "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check and delete emails\n",
"\n",
"To check and delete emails, first install the necessary modules..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install imapclient"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install pyzmail36"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"...and import them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import imapclient\n",
"import pyzmail"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, type your email and password into the variables below. And **you're all set** ! "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"email = \"type your email here\"\n",
"password = \"type your password here\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Check emails\n",
"\n",
"It can be quite tiring to try to read every single email you receive. Instead, filter which email you want to read: \n",
"* You only want to check emails received since a particular date : type _UIDs = conn.search('SINCE DD-MMM-YYYY')_. Note that the month is specified with its first three letters \n",
"* Check the emails containing a specific word in its subject : type _UIDs = conn.search('SUBJECT \"word\"')_. \n",
"* Combine searching conditions : type _UIDs = conn.search('search-key1 search-key2')_. \n",
"\n",
"All the search commands can be found [here](https://tools.ietf.org/html/rfc3501.html#section-6.4.4)\n",
"\n",
"In the end, you will get all the IDs of the emails you were looking for.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"conn = imapclient.IMAPClient('imap.gmail.com', ssl=True)\n",
"conn.login(email, password)\n",
"conn.select_folder('INBOX', readonly=False)\n",
"UIDs = conn.search('SINCE 08-Apr-2020') #Specify your research criteria in this line\n",
"UIDs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To check the subject and the address of the sender of the mails you selected, run the following code."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for ID in UIDs:\n",
" rawMessage = conn.fetch([ID], ['BODY[]', 'FLAGS'])\n",
" message = pyzmail.PyzMessage.factory(rawMessage[ID][b'BODY[]'])\n",
" print(ID)\n",
" print(message.get_subject())\n",
" print(message.get_addresses('from'))\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to check the text of a specific emails you are interested in or you want to reply to, type its ID in the variable below\n",
"\n",
"you first have to check what it the type of the content you are trying to read. There are 2 kinds : _text_ content or _html_ content. Run the code below to find out. \n",
"\n",
"By default, we set the type of content to \"test\". But if nothing appears, the type of content is HTML and you have to change the last line of the code to _message.html_part.charset_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ID = 42537 #Type here the Id of the email you want to check \n",
"message.text_part == None\n",
"message.html_part == None\n",
"message.text_part.charset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whatever appears, you have to type it in the code below"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"message.text_part.get_payload().decode('utf-8')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Here is the text your email**"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment