Git Product home page Git Product logo

email_listener's Introduction

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 = "[email protected]"
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:

{'[email protected]': {'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:

{'[email protected]':
    {'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 protected]' 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]"

email_listener's People

Contributors

njdreikosen avatar dependabot[bot] avatar

Stargazers

Adel Hassan avatar  avatar  avatar  avatar J. Michael Burgess avatar  avatar  avatar William Brochensque Júnior avatar Nikolaus Schlemm avatar Ignis avatar Jacob Moore avatar Pradyumna Joshi avatar Kong TO avatar  avatar Thomas Michelat avatar Mikhail Pakhomov avatar Kağan IŞILDAK avatar Okza Pradhana avatar

Watchers

 avatar  avatar

email_listener's Issues

AttributeError: 'Header' object has no attribute 'strip'

Traceback (most recent call last):
  File "D:/Projects/mail-reader/main.py", line 14, in <module>
    messages = el.scrape()
  File "\Projects\mail-reader\venv\lib\site-packages\email_listener\__init__.py", line 144, in scrape
    val_dict["Subject"] = self.__get_subject(email_message).strip()
AttributeError: 'Header' object has no attribute 'strip'

Python 3.8.8 - Windows 10

i can't get variable message to print anything

Get the emails currently unread in the inbox

messages = el.scrape()
print(messages)

based on code's above, the terminal should print out variable massage which is :

{'[email protected]': {'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': '\r\n\r\n \r\n \r\n <title>EmailListener Test</title>\r\n \r\n \r\n \r\n

This is the HTML message.
This is another line.

\r\n \r\n', 'attachments': ['/home/pi/email_listener/tests/attachments/EmailListener_test.txt']}} 

the terminal output.

Connection is now in IDLE mode.
Server sent: [(40, b'EXISTS')]
PROCESSING: Email UID = 73 from [email protected]
Server sent: nothing
Server sent: nothing
Server sent: nothing

but the thing that i got only the result it is already downloaded in notepad. any ideas why is it happening ?

PyCharm Package name

In PyCharm the name of this package (when searching for packages to install) is email-listener instead of email_listener. As a result I had trouble finding the package.

Never ran into this before.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.