Skip to main content

email_listener

Build Status codecov Maintainability PyPI version PyPI - Python Version Wheel Status License: GPL v3

email_listener is a Python package for listening in an email folder and processing incoming emails by scraping them, and optionally processing them with a custom processing script. Additionally, the EmailResponder class is included as an easy way to send reply emails.

email_listener currently supports only Gmail, but other email services may work with minor tweaks.

Basic usage:

import email_listener

# Set your email, password, what folder you want to listen to, and where to save attachments
email = "example@gmail.com"
app_password = "password"
folder = "Inbox"
attachment_dir = "/path/to/attachments"
el = email_listener.EmailListener(email, app_password, folder, attachment_dir)

# Log into the IMAP server
el.login()

# Get the emails currently unread in the inbox
messages = el.scrape()
print(messages)

# Start listening to the inbox and timeout after an hour
timeout = 60
el.listen(timeout)

The output:

{'227_somebody@gmail.com': {'Subject': 'EmailListener Test', 'Plain_Text': 'This is the plain text message.\r\nThis is another line.\r\n', 'Plain_HTML': 'This is the HTML message.  \nThis is another line.  \n\n', 'HTML': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml">\r\n  <head>\r\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\r\n    <title>EmailListener Test</title>\r\n    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>\r\n  </head>\r\n  <body>\r\n    <p>This is the HTML message.<br/>This is another line.<br/></p>\r\n  </body>\r\n</html>', 'attachments': ['/home/pi/email_listener/tests/attachments/EmailListener_test.txt']}}

The output, but prettier:

{'227_somebody@gmail.com':
    {'Subject': 'EmailListener Test',
     'Plain_Text': 'This is the plain text message.\r\nThis is another line.\r\n',
     'Plain_HTML': 'This is the HTML message.  \nThis is another line.  \n\n',
     'HTML': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n
              <html xmlns="http://www.w3.org/1999/xhtml">\r\n
                <head>\r\n
                  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\r\n
                  <title>EmailListener Test</title>\r\n
                  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>\r\n
                </head>\r\n
                <body>\r\n
                  <p>This is the HTML message.<br/>This is another line.<br/></p>\r\n
                </body>\r\n
              </html>',
     'attachments': ['/home/pi/email_listener/tests/attachments/EmailListener_test.txt']
    }
}

More detailed examples can be found in each module.

How to Install

email_listener is available on pypi https://pypi.org/project/email-listener/

$ pip install email_listener

Package Requirements

  • IMAP must be enabled in the 'Forwarding and POP/IMAP' section of the Gmail settings.
  • A Google Account app password must be created, which can be done in the 'Security' section of your Google Account page.

Running Unit Tests

Once the unit test requirements are met, run the unit tests with the following command: python3 -m pytest

Unit Test Requirements

Unit tests require a valid gmail account, which requires a few additions:

  • A label (or folder) named 'email_listener' must be created
  • A filter must be created, which moves any emails sent to '+email_listener@gmail.com' to the email_listener label/folder

Along with these changes, the following environmental variables must be created: EL_EMAIL and EL_APW

For example, on the Raspian OS, this can be done by adding the following to /home/pi/.profile:

export EL_EMAIL="[Your Gmail email]"
export EL_APW="[Your Google Account app password]"

Metadata

Release files for email-listener 1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for email-listener 1.2
File Size Uploaded Provenance
email_listener-1.2.tar.gz 11.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for email-listener 1.2
File Interpreter ABI Platform
email_listener-1.2-py3-none-any.whl Python 3 none any Details

Total release size: 35.4 kB

Release files / email_listener-1.2.tar.gz

Download URL email_listener-1.2.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c1871baa078e9fdf400a7baba0d50cc32ca453e1ac4f66658edc392faebeeb42
BLAKE2b-256 checksum
How to use checksums
7ace1a2af5fc8cbe07fdaab835c2d0ef77f72187f2faebb6b4b179bb0e725956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

Release files / email_listener-1.2-py3-none-any.whl

Download URL email_listener-1.2-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ed69ac7beaf0cbd1e8eef281c672ea7d5bcd49699962cb1ab2f997fe67dd72c
BLAKE2b-256 checksum
How to use checksums
2492ad904144c7a49c9295cf4117b8e498a3e1fdaa768696bec3029a18b6a7bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

Security for email-listener 1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page