在电子邮件文件夹中收听并处理收到的电子邮件。

email-listener的Python项目详细描述


电子邮件侦听器

Build StatuscodecovMaintainabilityPyPI versionPyPI - Python VersionWheel StatusLicense: GPL v3

email_listener是一个Python包,用于监听电子邮件文件夹中的内容,并通过抓取它们来处理传入的电子邮件,还可以选择使用自定义处理脚本来处理它们。另外,EmailResponder类是作为发送回复电子邮件的一种简单方式而包含的。在

email_-listener目前只支持Gmail,但其他电子邮件服务可能会稍作调整。在

基本用法:

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)

输出:

^{pr2}$

输出,但更漂亮:

{'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']
    }
}

在每个模块中可以找到更详细的示例。在

如何安装

email_listener在pypi上可用 https://pypi.org/project/email-listener/

$ pip install email_listener

包装要求

  • 必须在Gmail设置的“转发和POP/IMAP”部分启用IMAP。在
  • 必须创建一个谷歌帐户应用程序密码,这可以在你的谷歌帐户页面的“安全”部分完成。在

运行单元测试

满足单元测试要求后,使用以下命令运行单元测试: python3 -m pytest

单元测试要求

单元测试需要一个有效的gmail帐户,这需要添加一些内容:

  • 必须创建名为“电子邮件侦听器”的标签(或文件夹)
  • 必须创建一个过滤器,它将发送到“+email_listener@gmail.com”的任何电子邮件移动到电子邮件侦听器标签/文件夹

随着这些变化,必须创建以下环境变量: EL_EMAIL和{}

例如,在Raspian操作系统上,可以通过将以下内容添加到/home/pi/.profile中来完成:

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用Spring和JSF向客户端授予临时权限   java除了Array/ArrayList之外,还有其他保存矩阵元素的方法吗   java BeanValidation不适用于单选按钮   通过java程序连接到配置单元数据库时出错   java如何使用maven解决二级依赖关系   JfreeChart/Java中带有图例的数据表   合并数组时发生java运行时错误   安全性如何在JavaEE中保护WebSocket端点?   java有没有一种方法可以使用insert方法为树插入值   java编程“静态”的另一种方式:代码可以吗?   java阅读文本文件时如何跳过3行   %04X在C中的含义以及如何在java中编写   java我如何验证一个方法不是仅在一个测试范围内对模拟调用的?   java如何在mac中使用启动appium desktop 1.6.1。球棒   仅匹配最多10位小数的java正则表达式