在电子邮件文件夹中收听并处理收到的电子邮件。
email-listener的Python项目详细描述
电子邮件侦听器
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]"
- 项目
标签: